Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions packages/autorest.python/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
},
"homepage": "https://github.com/Azure/autorest.python/blob/main/README.md",
"dependencies": {
"@typespec/http-client-python": "~0.3.12",
"@typespec/http-client-python": "https://artprodcus3.artifacts.visualstudio.com/A0fb41ef4-5012-48a9-bf39-4ee3de03ee35/29ec6040-b234-4e31-b139-33dc4287b756/_apis/artifact/cGlwZWxpbmVhcnRpZmFjdDovL2F6dXJlLXNkay9wcm9qZWN0SWQvMjllYzYwNDAtYjIzNC00ZTMxLWIxMzktMzNkYzQyODdiNzU2L2J1aWxkSWQvNDM4NzY3NS9hcnRpZmFjdE5hbWUvYnVpbGRfYXJ0aWZhY3RzX3B5dGhvbg2/content?format=file&subPath=%2Fpackages%2Ftypespec-http-client-python-0.3.12.tgz",
"@autorest/system-requirements": "~1.0.2",
"fs-extra": "~11.2.0",
"tsx": "~4.19.1"
Expand All @@ -47,4 +47,4 @@
"requirements.txt",
"generator/"
]
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
# Changes may cause incorrect behavior and will be lost if the code is regenerated.
# --------------------------------------------------------------------------
import sys
from typing import Any, Callable, Dict, Optional, Type, TypeVar, cast
from typing import Any, Callable, Dict, Optional, TypeVar

from azure.core.exceptions import (
ClientAuthenticationError,
Expand Down Expand Up @@ -68,14 +68,6 @@ async def get_pet_by_id(self, pet_id: str, **kwargs: Any) -> Optional[_models.Pe
401: ClientAuthenticationError,
409: ResourceExistsError,
304: ResourceNotModifiedError,
400: HttpResponseError,
404: cast(
Type[HttpResponseError],
lambda response: ResourceNotFoundError(
response=response, model=self._deserialize(_models.NotFoundErrorBase, response)
),
),
501: HttpResponseError,
}
error_map.update(kwargs.pop("error_map", {}) or {})

Expand All @@ -99,8 +91,15 @@ async def get_pet_by_id(self, pet_id: str, **kwargs: Any) -> Optional[_models.Pe
response = pipeline_response.http_response

if response.status_code not in [200, 202]:
map_error(status_code=response.status_code, response=response, error_map=error_map) # type: ignore
raise HttpResponseError(response=response)
map_error(status_code=response.status_code, response=response, error_map=error_map)
error = None
if response.status_code == 400:
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For 400/501, failsafe_deserialize will always return None so maybe we don't need the logic.

error = self._deserialize.failsafe_deserialize(str, pipeline_response)
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why the deserialize type is str here for 400 while int for 501?

elif response.status_code == 404:
error = self._deserialize.failsafe_deserialize(_models.NotFoundErrorBase, pipeline_response)
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
error = self._deserialize.failsafe_deserialize(_models.NotFoundErrorBase, pipeline_response)
error = self._deserialize.failsafe_deserialize(_models.NotFoundErrorBase, response)

I think it shall be response instead of pipeline_response here.

elif response.status_code == 501:
error = self._deserialize.failsafe_deserialize(int, pipeline_response)
raise HttpResponseError(response=response, model=error)
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For 404, old code raise ResourceNotFoundError while new logic always raise HttpResponseError.


deserialized = None
if response.status_code == 200:
Expand All @@ -126,12 +125,6 @@ async def do_something(self, what_action: str, **kwargs: Any) -> _models.PetActi
404: ResourceNotFoundError,
409: ResourceExistsError,
304: ResourceNotModifiedError,
500: cast(
Type[HttpResponseError],
lambda response: HttpResponseError(
response=response, model=self._deserialize(_models.PetActionError, response)
),
),
}
error_map.update(kwargs.pop("error_map", {}) or {})

Expand All @@ -156,7 +149,11 @@ async def do_something(self, what_action: str, **kwargs: Any) -> _models.PetActi

if response.status_code not in [200]:
map_error(status_code=response.status_code, response=response, error_map=error_map)
error = self._deserialize.failsafe_deserialize(_models.PetActionError, pipeline_response)
error = None
if response.status_code == 500:
error = self._deserialize.failsafe_deserialize(_models.PetActionError, pipeline_response)
else:
error = self._deserialize.failsafe_deserialize(_models.PetActionError, pipeline_response)
raise HttpResponseError(response=response, model=error)

deserialized = self._deserialize("PetAction", pipeline_response.http_response)
Expand All @@ -183,12 +180,6 @@ async def has_models_param(self, models: str = "value1", **kwargs: Any) -> None:
404: ResourceNotFoundError,
409: ResourceExistsError,
304: ResourceNotModifiedError,
500: cast(
Type[HttpResponseError],
lambda response: HttpResponseError(
response=response, model=self._deserialize(_models.PetActionError, response)
),
),
}
error_map.update(kwargs.pop("error_map", {}) or {})

Expand All @@ -213,7 +204,11 @@ async def has_models_param(self, models: str = "value1", **kwargs: Any) -> None:

if response.status_code not in [200]:
map_error(status_code=response.status_code, response=response, error_map=error_map)
error = self._deserialize.failsafe_deserialize(_models.PetActionError, pipeline_response)
error = None
if response.status_code == 500:
error = self._deserialize.failsafe_deserialize(_models.PetActionError, pipeline_response)
else:
error = self._deserialize.failsafe_deserialize(_models.PetActionError, pipeline_response)
raise HttpResponseError(response=response, model=error)

if cls:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
# Changes may cause incorrect behavior and will be lost if the code is regenerated.
# --------------------------------------------------------------------------
import sys
from typing import Any, Callable, Dict, Optional, Type, TypeVar, cast
from typing import Any, Callable, Dict, Optional, TypeVar

from azure.core.exceptions import (
ClientAuthenticationError,
Expand Down Expand Up @@ -125,14 +125,6 @@ def get_pet_by_id(self, pet_id: str, **kwargs: Any) -> Optional[_models.Pet]:
401: ClientAuthenticationError,
409: ResourceExistsError,
304: ResourceNotModifiedError,
400: HttpResponseError,
404: cast(
Type[HttpResponseError],
lambda response: ResourceNotFoundError(
response=response, model=self._deserialize(_models.NotFoundErrorBase, response)
),
),
501: HttpResponseError,
}
error_map.update(kwargs.pop("error_map", {}) or {})

Expand All @@ -156,8 +148,15 @@ def get_pet_by_id(self, pet_id: str, **kwargs: Any) -> Optional[_models.Pet]:
response = pipeline_response.http_response

if response.status_code not in [200, 202]:
map_error(status_code=response.status_code, response=response, error_map=error_map) # type: ignore
raise HttpResponseError(response=response)
map_error(status_code=response.status_code, response=response, error_map=error_map)
error = None
if response.status_code == 400:
error = self._deserialize.failsafe_deserialize(str, pipeline_response)
elif response.status_code == 404:
error = self._deserialize.failsafe_deserialize(_models.NotFoundErrorBase, pipeline_response)
elif response.status_code == 501:
error = self._deserialize.failsafe_deserialize(int, pipeline_response)
raise HttpResponseError(response=response, model=error)

deserialized = None
if response.status_code == 200:
Expand All @@ -183,12 +182,6 @@ def do_something(self, what_action: str, **kwargs: Any) -> _models.PetAction:
404: ResourceNotFoundError,
409: ResourceExistsError,
304: ResourceNotModifiedError,
500: cast(
Type[HttpResponseError],
lambda response: HttpResponseError(
response=response, model=self._deserialize(_models.PetActionError, response)
),
),
}
error_map.update(kwargs.pop("error_map", {}) or {})

Expand All @@ -213,7 +206,11 @@ def do_something(self, what_action: str, **kwargs: Any) -> _models.PetAction:

if response.status_code not in [200]:
map_error(status_code=response.status_code, response=response, error_map=error_map)
error = self._deserialize.failsafe_deserialize(_models.PetActionError, pipeline_response)
error = None
if response.status_code == 500:
error = self._deserialize.failsafe_deserialize(_models.PetActionError, pipeline_response)
else:
error = self._deserialize.failsafe_deserialize(_models.PetActionError, pipeline_response)
raise HttpResponseError(response=response, model=error)

deserialized = self._deserialize("PetAction", pipeline_response.http_response)
Expand Down Expand Up @@ -242,12 +239,6 @@ def has_models_param( # pylint: disable=inconsistent-return-statements
404: ResourceNotFoundError,
409: ResourceExistsError,
304: ResourceNotModifiedError,
500: cast(
Type[HttpResponseError],
lambda response: HttpResponseError(
response=response, model=self._deserialize(_models.PetActionError, response)
),
),
}
error_map.update(kwargs.pop("error_map", {}) or {})

Expand All @@ -272,7 +263,11 @@ def has_models_param( # pylint: disable=inconsistent-return-statements

if response.status_code not in [200]:
map_error(status_code=response.status_code, response=response, error_map=error_map)
error = self._deserialize.failsafe_deserialize(_models.PetActionError, pipeline_response)
error = None
if response.status_code == 500:
error = self._deserialize.failsafe_deserialize(_models.PetActionError, pipeline_response)
else:
error = self._deserialize.failsafe_deserialize(_models.PetActionError, pipeline_response)
raise HttpResponseError(response=response, model=error)

if cls:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
# Changes may cause incorrect behavior and will be lost if the code is regenerated.
# --------------------------------------------------------------------------
import sys
from typing import Any, Callable, Dict, Optional, Type, TypeVar, cast
from typing import Any, Callable, Dict, Optional, TypeVar, cast

from azure.core.exceptions import (
ClientAuthenticationError,
Expand Down Expand Up @@ -75,9 +75,6 @@ async def get_pet_by_id(self, pet_id: str, **kwargs: Any) -> Optional[JSON]:
401: ClientAuthenticationError,
409: ResourceExistsError,
304: ResourceNotModifiedError,
400: HttpResponseError,
404: cast(Type[HttpResponseError], lambda response: ResourceNotFoundError(response=response)),
501: HttpResponseError,
}
error_map.update(kwargs.pop("error_map", {}) or {})

Expand All @@ -101,7 +98,7 @@ async def get_pet_by_id(self, pet_id: str, **kwargs: Any) -> Optional[JSON]:
response = pipeline_response.http_response

if response.status_code not in [200, 202]:
map_error(status_code=response.status_code, response=response, error_map=error_map) # type: ignore
map_error(status_code=response.status_code, response=response, error_map=error_map)
raise HttpResponseError(response=response)

deserialized = None
Expand Down Expand Up @@ -139,7 +136,6 @@ async def do_something(self, what_action: str, **kwargs: Any) -> JSON:
404: ResourceNotFoundError,
409: ResourceExistsError,
304: ResourceNotModifiedError,
500: HttpResponseError,
}
error_map.update(kwargs.pop("error_map", {}) or {})

Expand Down Expand Up @@ -193,7 +189,6 @@ async def has_models_param(self, *, models: str = "value1", **kwargs: Any) -> No
404: ResourceNotFoundError,
409: ResourceExistsError,
304: ResourceNotModifiedError,
500: HttpResponseError,
}
error_map.update(kwargs.pop("error_map", {}) or {})

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
# Changes may cause incorrect behavior and will be lost if the code is regenerated.
# --------------------------------------------------------------------------
import sys
from typing import Any, Callable, Dict, Optional, Type, TypeVar, cast
from typing import Any, Callable, Dict, Optional, TypeVar, cast

from azure.core.exceptions import (
ClientAuthenticationError,
Expand Down Expand Up @@ -132,9 +132,6 @@ def get_pet_by_id(self, pet_id: str, **kwargs: Any) -> Optional[JSON]:
401: ClientAuthenticationError,
409: ResourceExistsError,
304: ResourceNotModifiedError,
400: HttpResponseError,
404: cast(Type[HttpResponseError], lambda response: ResourceNotFoundError(response=response)),
501: HttpResponseError,
}
error_map.update(kwargs.pop("error_map", {}) or {})

Expand All @@ -158,7 +155,7 @@ def get_pet_by_id(self, pet_id: str, **kwargs: Any) -> Optional[JSON]:
response = pipeline_response.http_response

if response.status_code not in [200, 202]:
map_error(status_code=response.status_code, response=response, error_map=error_map) # type: ignore
map_error(status_code=response.status_code, response=response, error_map=error_map)
raise HttpResponseError(response=response)

deserialized = None
Expand Down Expand Up @@ -196,7 +193,6 @@ def do_something(self, what_action: str, **kwargs: Any) -> JSON:
404: ResourceNotFoundError,
409: ResourceExistsError,
304: ResourceNotModifiedError,
500: HttpResponseError,
}
error_map.update(kwargs.pop("error_map", {}) or {})

Expand Down Expand Up @@ -252,7 +248,6 @@ def has_models_param( # pylint: disable=inconsistent-return-statements
404: ResourceNotFoundError,
409: ResourceExistsError,
304: ResourceNotModifiedError,
500: HttpResponseError,
}
error_map.update(kwargs.pop("error_map", {}) or {})

Expand Down
4 changes: 2 additions & 2 deletions packages/typespec-python/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@
"js-yaml": "~4.1.0",
"semver": "~7.6.2",
"tsx": "~4.19.1",
"@typespec/http-client-python": "~0.3.12",
"@typespec/http-client-python": "https://artprodcus3.artifacts.visualstudio.com/A0fb41ef4-5012-48a9-bf39-4ee3de03ee35/29ec6040-b234-4e31-b139-33dc4287b756/_apis/artifact/cGlwZWxpbmVhcnRpZmFjdDovL2F6dXJlLXNkay9wcm9qZWN0SWQvMjllYzYwNDAtYjIzNC00ZTMxLWIxMzktMzNkYzQyODdiNzU2L2J1aWxkSWQvNDM4NzY3NS9hcnRpZmFjdE5hbWUvYnVpbGRfYXJ0aWZhY3RzX3B5dGhvbg2/content?format=file&subPath=%2Fpackages%2Ftypespec-http-client-python-0.3.12.tgz",
"fs-extra": "~11.2.0"
},
"devDependencies": {
Expand Down Expand Up @@ -90,4 +90,4 @@
"chalk": "5.3.0",
"@types/fs-extra": "11.0.4"
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -894,6 +894,22 @@ def _deserialize(
return _deserialize_with_callable(deserializer, value)


def _failsafe_deserialize(
deserializer: typing.Any,
value: typing.Any,
module: typing.Optional[str] = None,
rf: typing.Optional["_RestField"] = None,
format: typing.Optional[str] = None,
) -> typing.Any:
try:
return _deserialize(deserializer, value, module, rf, format)
except DeserializationError:
_LOGGER.warning(
"Ran into a deserialization error. Ignoring since this is failsafe deserialization", exc_info=True
)
return None


class _RestField:
def __init__(
self,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
# Changes may cause incorrect behavior and will be lost if the code is regenerated.
# --------------------------------------------------------------------------
import sys
from typing import Any, Callable, Dict, Optional, Type, TypeVar, cast
from typing import Any, Callable, Dict, Optional, TypeVar

from azure.core.exceptions import (
ClientAuthenticationError,
Expand All @@ -22,7 +22,7 @@
from azure.core.utils import case_insensitive_dict

from .. import models as _models
from .._model_base import _deserialize
from .._model_base import _failsafe_deserialize
from .._serialization import Serializer
from .._vendor import ApiKeyClientMixinABC

Expand Down Expand Up @@ -117,12 +117,6 @@ def invalid(self, **kwargs: Any) -> None: # pylint: disable=inconsistent-return
404: ResourceNotFoundError,
409: ResourceExistsError,
304: ResourceNotModifiedError,
403: cast(
Type[HttpResponseError],
lambda response: HttpResponseError(
response=response, model=_deserialize(_models.InvalidAuth, response.json())
),
),
}
error_map.update(kwargs.pop("error_map", {}) or {})

Expand All @@ -149,7 +143,10 @@ def invalid(self, **kwargs: Any) -> None: # pylint: disable=inconsistent-return

if response.status_code not in [204]:
map_error(status_code=response.status_code, response=response, error_map=error_map)
raise HttpResponseError(response=response)
error = None
if response.status_code == 403:
error = _failsafe_deserialize(_models.InvalidAuth, response.json())
raise HttpResponseError(response=response, model=error)

if cls:
return cls(pipeline_response, None, {}) # type: ignore
Loading
Loading