Repository navigation
fix(response): keep status code and url when an error body is not JSON - #429
Open
RishavTiwari25 wants to merge 1 commit into
Open
RishavTiwari25 wants to merge 1 commit into
RishavTiwari25 wants to merge 1 commit into
Conversation
verify_response decoded every error body as JSON. When it was not JSON (an HTML 502/503 page from a load balancer or proxy, or an empty body), the decoding error escaped instead: a LagoApiError with status_code 500, url None and the orjson message as detail, so callers could not tell a 502 from a 404 or see which request failed. The raw body is now kept as the error's response and the real status code and url are reported.
Welcome, @RishavTiwari25!Thanks for your first contribution! Before we proceed with the review, please sign the Fiduciary License Agreement: Once signed, this PR will be automatically updated. |
Thanks, @RishavTiwari25! 🎉Your CLA has been signed and is now on file. We'll proceed with the review shortly. |
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.
Problem
When the API (or anything in front of it) answers an error with a body that is not JSON, the client reports the wrong error:
An empty error body gives the same thing (
detail='Input is a zero-length, empty document'). This happens for HTML 502/503/504 pages from load balancers and reverse proxies (self-hosted Lago behind nginx, or a gateway timeout), so code that branches onexc.status_code(retry on 502/503, treat 404 as missing) sees a 500, and the error does not say which request failed.Root cause
verify_responsecallsfrom_json(response)on every non-2xx response before raising.from_jsonraises its ownLagoApiError(status_code=500, url=None, ...)when decoding fails, and that error escapes instead of the one built from the response.Fix
Decode the error body inside a
try; if it is not JSON, keep the raw text (orNonefor an empty body) as the error'sresponse. The raisedLagoApiErrorthen always carries the real status code, URL and headers. JSON error bodies are handled as before.Tests
test_verify_response_with_non_json_error_body(HTML 502, plain-text 503, empty 404): asserts status code, url and raw body. All three fail onmain.test_verify_response_with_json_error_body: JSON error bodies are still decoded.python -m pytest tests: 468 passed.ruff check .andruff format --check .clean.