Reject with an HTTPError on non-200 responses - #50
Open
Tyagiquamar wants to merge 1 commit into
Open
Tyagiquamar wants to merge 1 commit into
Tyagiquamar wants to merge 1 commit into
Conversation
execute() and uploadImage() rejected with the raw response body string when SerpApi returned a non-200 status code. The rejection had no HTTP status code, was not an Error, and lost the stack trace, so callers could not branch on the failure kind or log where it came from. Add an HTTPError class exposing statusCode and the raw body, with the message set to the error field of a JSON response when present, and reject with it in both request paths. This matches the structured HTTP errors already exposed by the serpapi-python (HTTPError.status_code) and serpapi-ruby (SerpApiError#response_status) SDKs.
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
execute()anduploadImage()insrc/utils.tsreject with the raw response body string when SerpApi responds with a non-200 status code:A rejected bare string is not an
Error: it has no HTTP status code, no stack trace, and cannot be distinguished from other failures withinstanceof. This makes it hard for callers to branch on the failure kind (for example, a 401 invalid key versus a 429 rate limit) or to log where the rejection came from.Before:
Solution
Add an
HTTPErrorclass insrc/errors.tsexposingstatusCodeand the rawbody. The message is set to theerrorfield of a JSON response when present, and falls back to a status-code message for non-JSON bodies. Both request paths now reject with it.After:
This also matches the structured HTTP errors already exposed by the other SerpApi SDKs:
serpapi-python'sHTTPError.status_codeandserpapi-ruby'sSerpApiError#response_status.Validation
tests/errors_test.tscovers the class for JSON bodies with and without anerrorfield, non-JSON bodies, an emptyerrorfield, and an empty body.execute()has a new test intests/utils_test.tsthat hits the real/accountendpoint with an invalid API key (no account or paid access required) and asserts the rejection is anHTTPErrorwithstatusCode401 and the message from the JSONerrorfield.uploadImagestub test now asserts the new error contract.deno fmt --check,deno lint,deno task test, anddeno task npmpass.Notes
HTTPErroris exported frommod.tsand documented in the README under Configuration.