fix: raise SerpApiException on an error in a 200 response body - #6
Merged
Conversation
SerpApi reports some failures inside the body of an HTTP 200 response,
for example the google_events engine:
HTTP 200
{"search_metadata":{"status":"Success"},
"search_information":{"events_results_state":"Fully empty"},
"error":"Google hasn't returned any results for this query."}
The client decided success purely from the status code, so search()
returned an object that was semantically an error. Callers then reached
for the key they expected and got a NullPointerException pointing at
their own code, with the explanation sitting unread in the error field.
This is what broke GoogleEventsTest.
Check for a body-level error in json() and location(), routing it
through the existing triggerSerpApiException so every SerpApi error
reaches the caller as a SerpApiException regardless of status code.
html() still returns its raw String unchecked; parsing arbitrary HTML
as JSON to look for an error field is not worth the risk.
Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
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
SerpApi reports some failures inside the body of an HTTP 200 response. The
google_eventsengine is currently doing exactly this:{ "search_metadata": { "status": "Success" }, "search_information": { "events_results_state": "Fully empty" }, "error": "Google hasn't returned any results for this query." }HTTP 200,
status: "Success", and anerrorfield.SerpApiHttp.get()decided success purely from the status code, sotriggerSerpApiExceptionwas never reached andsearch()returned an object that is semantically an error. The caller then did the natural thing:getAsJsonArrayreturnsnullfor an absent key, so this NPEs — pointing at the caller's own line, with the actual explanation sitting unread in theerrorfield. This is what brokeGoogleEventsTestin CI, and every consumer of this library hits the same edge.Change
Check for a body-level
errorinjson()andlocation(), routing it through the existingtriggerSerpApiExceptionso a SerpApi error always reaches the caller as aSerpApiException, regardless of status code.location()needs its own check because it bypassesjson()and parses an array — an error body is an object, so it previously died on the cast instead.Tests
New
ErrorResponseTeststubs the HTTP client, so it runs offline and needs noSERPAPI_KEY. Verified non-vacuous: with theSerpApi.javachange stashed, the three error-path tests fail and the two happy-path tests still pass.Notes for review
has("error")itself will now see an exception instead.error, so honoring that label seems right.html()still returns its raw String unchecked — parsing arbitrary HTML as JSON hunting for an error field isn't worth the risk.HomeDepotTestis failing separately on aHttpTimeoutException(three consecutive runs), untouched here.🤖 Generated with Claude Code