Repository navigation
fix(messaging): preserve per-message results in send_each_async - #1006
EnesKartal wants to merge 1 commit into
Conversation
|
Thanks for your pull request! It looks like this may be your first contribution to a Google open source project. Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA). View this failed invocation of the CLA check for more information. For the most up to date status, view the checks section at the bottom of the pull request. |
There was a problem hiding this comment.
Code Review
This pull request adds generic exception handling to send_data in firebase_admin/messaging.py to catch unexpected exceptions during remote service calls and wrap them in exceptions.UnknownError, along with a corresponding unit test. The review feedback points out that resp.json() is called outside of the try-except block, which could still raise an unhandled exception if the response contains invalid JSON. It is recommended to wrap this call in a try-except block as well to ensure robust error handling.
| except Exception as exception: | ||
| return SendResponse( | ||
| resp=None, | ||
| exception=exceptions.UnknownError( | ||
| message=f'Unknown error while making remote service calls: {exception}', | ||
| cause=exception, | ||
| ), | ||
| ) | ||
| return SendResponse(resp.json(), exception=None) |
There was a problem hiding this comment.
While catching unexpected exceptions during the remote call is a great improvement, calling resp.json() at line 611 is currently outside of any try-except block. If the response body is empty or contains invalid JSON, resp.json() will raise a ValueError (or JSONDecodeError), which will propagate up and abort the entire asyncio.gather() call, losing all other successful results in the batch.\n\nTo prevent this, we should wrap the resp.json() call in a try-except block as well, wrapping any parsing exceptions in an UnknownError to preserve the other results.
except Exception as exception:\n return SendResponse(\n resp=None,\n exception=exceptions.UnknownError(\n message=f'Unknown error while making remote service calls: {exception}',\n cause=exception,\n ),\n )\n try:\n return SendResponse(resp.json(), exception=None)\n except Exception as exception:\n return SendResponse(\n resp=None,\n exception=exceptions.UnknownError(\n message=f'Unknown error while parsing remote service response: {exception}',\n cause=exception,\n ),\n )
Fixes #995.
httpx.HTTPError/ non-RequestExceptionexceptions (such asKeyError(stream_id)fromhttpcoreHTTP/2 transport errors) insidesend_data()insend_each_asyncand wraps them inSendResponse(resp=None, exception=exceptions.UnknownError(...)).asyncio.gather()and losing the per-messageSendResponseresults of all other messages in the batch.test_send_each_async_unexpected_exception_preserves_other_resultsintests/test_messaging.py.