Skip to content

Commit

Permalink
read error response in try catch (#2684)
Browse files Browse the repository at this point in the history
* read error response in try catch

* add changeset

* fix and inv

---------

Co-authored-by: iscai-msft <[email protected]>
Co-authored-by: msyyc <[email protected]>
  • Loading branch information
3 people committed Jul 10, 2024
1 parent f276ded commit 55144a7
Show file tree
Hide file tree
Showing 228 changed files with 6,069 additions and 1,403 deletions.
8 changes: 8 additions & 0 deletions .chronus/changes/check_error_read-2024-6-9-15-39-6.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
---
changeKind: fix
packages:
- "@autorest/python"
- "@azure-tools/typespec-python"
---

don't throw if stream is already read or consumed when we load in the error body
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
ResourceExistsError,
ResourceNotFoundError,
ResourceNotModifiedError,
StreamClosedError,
StreamConsumedError,
map_error,
)
from azure.core.pipeline import PipelineResponse
Expand Down Expand Up @@ -119,7 +121,10 @@ def _basic_polling_initial(
response = pipeline_response.http_response

if response.status_code not in [200, 204]:
response.read() # Load the body in memory and close the socket
try:
response.read() # Load the body in memory and close the socket
except (StreamConsumedError, StreamClosedError):
pass
map_error(status_code=response.status_code, response=response, error_map=error_map)
raise HttpResponseError(response=response)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
ResourceExistsError,
ResourceNotFoundError,
ResourceNotModifiedError,
StreamClosedError,
StreamConsumedError,
map_error,
)
from azure.core.pipeline import PipelineResponse
Expand Down Expand Up @@ -90,7 +92,10 @@ async def _basic_polling_initial(
response = pipeline_response.http_response

if response.status_code not in [200, 204]:
await response.read() # Load the body in memory and close the socket
try:
await response.read() # Load the body in memory and close the socket
except (StreamConsumedError, StreamClosedError):
pass
map_error(status_code=response.status_code, response=response, error_map=error_map)
raise HttpResponseError(response=response)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
ResourceExistsError,
ResourceNotFoundError,
ResourceNotModifiedError,
StreamClosedError,
StreamConsumedError,
map_error,
)
from azure.core.pipeline import PipelineResponse
Expand Down Expand Up @@ -153,7 +155,10 @@ async def _test_lro_initial(
response = pipeline_response.http_response

if response.status_code not in [200, 204]:
await response.read() # Load the body in memory and close the socket
try:
await response.read() # Load the body in memory and close the socket
except (StreamConsumedError, StreamClosedError):
pass
map_error(status_code=response.status_code, response=response, error_map=error_map)
error = self._deserialize.failsafe_deserialize(_models.Error, pipeline_response)
raise HttpResponseError(response=response, model=error, error_format=ARMErrorFormat)
Expand Down Expand Up @@ -303,7 +308,10 @@ async def _test_lro_and_paging_initial(
response = pipeline_response.http_response

if response.status_code not in [200]:
await response.read() # Load the body in memory and close the socket
try:
await response.read() # Load the body in memory and close the socket
except (StreamConsumedError, StreamClosedError):
pass
map_error(status_code=response.status_code, response=response, error_map=error_map)
raise HttpResponseError(response=response, error_format=ARMErrorFormat)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
ResourceExistsError,
ResourceNotFoundError,
ResourceNotModifiedError,
StreamClosedError,
StreamConsumedError,
map_error,
)
from azure.core.paging import ItemPaged
Expand Down Expand Up @@ -231,7 +233,10 @@ def _test_lro_initial(
response = pipeline_response.http_response

if response.status_code not in [200, 204]:
response.read() # Load the body in memory and close the socket
try:
response.read() # Load the body in memory and close the socket
except (StreamConsumedError, StreamClosedError):
pass
map_error(status_code=response.status_code, response=response, error_map=error_map)
error = self._deserialize.failsafe_deserialize(_models.Error, pipeline_response)
raise HttpResponseError(response=response, model=error, error_format=ARMErrorFormat)
Expand Down Expand Up @@ -378,7 +383,10 @@ def _test_lro_and_paging_initial(
response = pipeline_response.http_response

if response.status_code not in [200]:
response.read() # Load the body in memory and close the socket
try:
response.read() # Load the body in memory and close the socket
except (StreamConsumedError, StreamClosedError):
pass
map_error(status_code=response.status_code, response=response, error_map=error_map)
raise HttpResponseError(response=response, error_format=ARMErrorFormat)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
ResourceExistsError,
ResourceNotFoundError,
ResourceNotModifiedError,
StreamClosedError,
StreamConsumedError,
map_error,
)
from azure.core.pipeline import PipelineResponse
Expand Down Expand Up @@ -1576,7 +1578,10 @@ async def _get_multiple_pages_lro_initial(
response = pipeline_response.http_response

if response.status_code not in [202]:
await response.read() # Load the body in memory and close the socket
try:
await response.read() # Load the body in memory and close the socket
except (StreamConsumedError, StreamClosedError):
pass
map_error(status_code=response.status_code, response=response, error_map=error_map)
raise HttpResponseError(response=response, error_format=ARMErrorFormat)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
ResourceExistsError,
ResourceNotFoundError,
ResourceNotModifiedError,
StreamClosedError,
StreamConsumedError,
map_error,
)
from azure.core.paging import ItemPaged
Expand Down Expand Up @@ -2035,7 +2037,10 @@ def _get_multiple_pages_lro_initial(
response = pipeline_response.http_response

if response.status_code not in [202]:
response.read() # Load the body in memory and close the socket
try:
response.read() # Load the body in memory and close the socket
except (StreamConsumedError, StreamClosedError):
pass
map_error(status_code=response.status_code, response=response, error_map=error_map)
raise HttpResponseError(response=response, error_format=ARMErrorFormat)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
ResourceExistsError,
ResourceNotFoundError,
ResourceNotModifiedError,
StreamClosedError,
StreamConsumedError,
map_error,
)
from azure.core.pipeline import PipelineResponse
Expand Down Expand Up @@ -107,7 +109,10 @@ async def _put_async_retry_succeeded_initial(
response = pipeline_response.http_response

if response.status_code not in [200]:
await response.read() # Load the body in memory and close the socket
try:
await response.read() # Load the body in memory and close the socket
except (StreamConsumedError, StreamClosedError):
pass
map_error(status_code=response.status_code, response=response, error_map=error_map)
raise HttpResponseError(response=response, error_format=ARMErrorFormat)

Expand Down Expand Up @@ -279,7 +284,10 @@ async def _put201_creating_succeeded200_initial(
response = pipeline_response.http_response

if response.status_code not in [200, 201]:
await response.read() # Load the body in memory and close the socket
try:
await response.read() # Load the body in memory and close the socket
except (StreamConsumedError, StreamClosedError):
pass
map_error(status_code=response.status_code, response=response, error_map=error_map)
raise HttpResponseError(response=response, error_format=ARMErrorFormat)

Expand Down Expand Up @@ -440,7 +448,10 @@ async def _post202_retry200_initial(
response = pipeline_response.http_response

if response.status_code not in [202]:
await response.read() # Load the body in memory and close the socket
try:
await response.read() # Load the body in memory and close the socket
except (StreamConsumedError, StreamClosedError):
pass
map_error(status_code=response.status_code, response=response, error_map=error_map)
raise HttpResponseError(response=response, error_format=ARMErrorFormat)

Expand Down Expand Up @@ -591,7 +602,10 @@ async def _post_async_retry_succeeded_initial(
response = pipeline_response.http_response

if response.status_code not in [202]:
await response.read() # Load the body in memory and close the socket
try:
await response.read() # Load the body in memory and close the socket
except (StreamConsumedError, StreamClosedError):
pass
map_error(status_code=response.status_code, response=response, error_map=error_map)
raise HttpResponseError(response=response, error_format=ARMErrorFormat)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
ResourceExistsError,
ResourceNotFoundError,
ResourceNotModifiedError,
StreamClosedError,
StreamConsumedError,
map_error,
)
from azure.core.pipeline import PipelineResponse
Expand Down Expand Up @@ -110,7 +112,10 @@ async def _put201_creating_succeeded200_initial(
response = pipeline_response.http_response

if response.status_code not in [200, 201]:
await response.read() # Load the body in memory and close the socket
try:
await response.read() # Load the body in memory and close the socket
except (StreamConsumedError, StreamClosedError):
pass
map_error(status_code=response.status_code, response=response, error_map=error_map)
raise HttpResponseError(response=response, error_format=ARMErrorFormat)

Expand Down Expand Up @@ -268,7 +273,10 @@ async def _put_async_relative_retry_succeeded_initial( # pylint: disable=name-t
response = pipeline_response.http_response

if response.status_code not in [200]:
await response.read() # Load the body in memory and close the socket
try:
await response.read() # Load the body in memory and close the socket
except (StreamConsumedError, StreamClosedError):
pass
map_error(status_code=response.status_code, response=response, error_map=error_map)
raise HttpResponseError(response=response, error_format=ARMErrorFormat)

Expand Down Expand Up @@ -422,7 +430,10 @@ async def _delete_provisioning202_accepted200_succeeded_initial( # pylint: disa
response = pipeline_response.http_response

if response.status_code not in [200, 202]:
await response.read() # Load the body in memory and close the socket
try:
await response.read() # Load the body in memory and close the socket
except (StreamConsumedError, StreamClosedError):
pass
map_error(status_code=response.status_code, response=response, error_map=error_map)
raise HttpResponseError(response=response, error_format=ARMErrorFormat)

Expand Down Expand Up @@ -520,7 +531,10 @@ async def _delete202_retry200_initial(self, **kwargs: Any) -> AsyncIterator[byte
response = pipeline_response.http_response

if response.status_code not in [202]:
await response.read() # Load the body in memory and close the socket
try:
await response.read() # Load the body in memory and close the socket
except (StreamConsumedError, StreamClosedError):
pass
map_error(status_code=response.status_code, response=response, error_map=error_map)
raise HttpResponseError(response=response, error_format=ARMErrorFormat)

Expand Down Expand Up @@ -608,7 +622,10 @@ async def _delete_async_relative_retry_succeeded_initial( # pylint: disable=nam
response = pipeline_response.http_response

if response.status_code not in [202]:
await response.read() # Load the body in memory and close the socket
try:
await response.read() # Load the body in memory and close the socket
except (StreamConsumedError, StreamClosedError):
pass
map_error(status_code=response.status_code, response=response, error_map=error_map)
raise HttpResponseError(response=response, error_format=ARMErrorFormat)

Expand Down Expand Up @@ -716,7 +733,10 @@ async def _post202_retry200_initial(
response = pipeline_response.http_response

if response.status_code not in [202]:
await response.read() # Load the body in memory and close the socket
try:
await response.read() # Load the body in memory and close the socket
except (StreamConsumedError, StreamClosedError):
pass
map_error(status_code=response.status_code, response=response, error_map=error_map)
raise HttpResponseError(response=response, error_format=ARMErrorFormat)

Expand Down Expand Up @@ -864,7 +884,10 @@ async def _post_async_relative_retry_succeeded_initial( # pylint: disable=name-
response = pipeline_response.http_response

if response.status_code not in [202]:
await response.read() # Load the body in memory and close the socket
try:
await response.read() # Load the body in memory and close the socket
except (StreamConsumedError, StreamClosedError):
pass
map_error(status_code=response.status_code, response=response, error_map=error_map)
raise HttpResponseError(response=response, error_format=ARMErrorFormat)

Expand Down
Loading

0 comments on commit 55144a7

Please sign in to comment.