Skip to content

Commit

Permalink
Add raw response to DaprInternalError (#628)
Browse files Browse the repository at this point in the history
Signed-off-by: Bernd Verst <[email protected]>
  • Loading branch information
berndverst authored Nov 1, 2023
1 parent d0007a9 commit e7ad277
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 5 deletions.
5 changes: 4 additions & 1 deletion dapr/clients/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,15 @@ class DaprInternalError(Exception):
"""DaprInternalError encapsulates all Dapr exceptions"""
def __init__(
self, message: Optional[str],
error_code: Optional[str] = ERROR_CODE_UNKNOWN):
error_code: Optional[str] = ERROR_CODE_UNKNOWN,
raw_response_bytes: Optional[bytes] = None):
self._message = message
self._error_code = error_code
self._raw_response_bytes = raw_response_bytes

def as_dict(self):
return {
'message': self._message,
'errorCode': self._error_code,
'raw_response_bytes': self._raw_response_bytes
}
10 changes: 6 additions & 4 deletions dapr/clients/http/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,22 +98,24 @@ async def send_bytes(

raise (await self.convert_to_error(r))

async def convert_to_error(self, response) -> DaprInternalError:
async def convert_to_error(self, response: aiohttp.ClientResponse) -> DaprInternalError:
error_info = None
try:
error_body = await response.read()
if (error_body is None or len(error_body) == 0) and response.status == 404:
return DaprInternalError("Not Found", ERROR_CODE_DOES_NOT_EXIST)
error_info = self._serializer.deserialize(error_body)
except Exception:
return DaprInternalError(f'Unknown Dapr Error. HTTP status code: {response.status}')
return DaprInternalError(f'Unknown Dapr Error. HTTP status code: {response.status}',
raw_response_bytes=error_body)

if error_info and isinstance(error_info, dict):
message = error_info.get('message')
error_code = error_info.get('errorCode') or ERROR_CODE_UNKNOWN
return DaprInternalError(message, error_code)
return DaprInternalError(message, error_code, raw_response_bytes=error_body)

return DaprInternalError(f'Unknown Dapr Error. HTTP status code: {response.status}')
return DaprInternalError(f'Unknown Dapr Error. HTTP status code: {response.status}',
raw_response_bytes=error_body)

def get_ssl_context(self):
# This method is used (overwritten) from tests
Expand Down

0 comments on commit e7ad277

Please sign in to comment.