fix(functions): keep edge function failures as FunctionsHttpError - #1576
Open
tushardev-365 wants to merge 1 commit into
Open
fix(functions): keep edge function failures as FunctionsHttpError#1576tushardev-365 wants to merge 1 commit into
tushardev-365 wants to merge 1 commit into
Conversation
The error handler read the failure body with response.json().get("error")
inside except HTTPError. An edge function that fails at the gateway does
not necessarily answer with a JSON object: a 502 or 504 typically returns
HTML, and a body can be a valid JSON array or string. In those cases the
handler raised JSONDecodeError or AttributeError, so callers wrapping the
call in except FunctionsHttpError never saw the failure, and the status
code and body were lost.
The existing fallback message was unreachable for exactly the case it was
written for, because it could only be selected once .json() had already
succeeded and returned a dict.
Read the body through a small helper that returns None when it is not a
JSON object, so the fallback works as intended. Same handling for the
relay error path, which had the identical call.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What
_requestreads the failure body inside itsexcept HTTPErrorhandler:A failing edge function does not always answer with a JSON object. A 502/504 from the gateway returns HTML, and a body can be a valid JSON array or string. So:
The caller gets that raw exception instead of
FunctionsHttpError, soexcept FunctionsHttpErroraroundinvoke()does not catch the failure, and the status code and body are lost.There is a second effect worth calling out: the
or "An error occurred ..."fallback was unreachable in exactly the situation it was written for. It could only be reached once.json()had already succeeded and returned a dict — i.e. never for the malformed bodies it was meant to cover.The relay path had the identical call:
Fix
Read the body through a small
get_error_messagehelper inutils.pythat returnsNonewhen the body is not JSON or not an object, so the existing fallback message does its job. Applied to both sites.Tests
Two cases per flavour (async + sync): a non-JSON error body and a JSON body that is not an object. Both assert
FunctionsHttpError. Reverting only the client call sites makes all four fail withJSONDecodeError/AttributeError.Full functions suite: 69 passed, 1 skipped.
_syncis generated, so the change was made in_asyncand regenerated withmake build-sync.