Skip to content

BREAKING: Return searches with empty results instead of raising - #41

Closed
strzibny wants to merge 1 commit into
serpapi:masterfrom
strzibny:no-raise-on-empty-results
Closed

strzibny wants to merge 1 commit into
serpapi:masterfrom
strzibny:no-raise-on-empty-results

Conversation

@strzibny

Copy link
Copy Markdown
Contributor

Fixes #17.

Breaking change

JSON responses with HTTP 200 and a top-level error key no longer raise SerpApi::SerpApiError. This affects search, search_archive, and extract.

SerpApi treats a search where the search engine returned no results as successful: HTTP 200, search_metadata.status: "Success", and an error message such as "Google hasn't returned any results for this query." (see Status and Error Codes). The client treated any error key as a failure, so these searches raised HTTP request failed with status: 200 OK. Because the error drops the response body, there was also no way to read archived searches with empty results through search_archive, which is what #17 reports.

Response 1.0.3 and earlier This PR
200, status: "Success", error (empty results) raises SerpApiError returns the Hash, with the message in results[:error]
200, status: "Error" raises raises (Search failed with status: Error error: ...)
Non-200 (400, 401, 429, 503, ...) raises raises (unchanged)

Migration

Code that rescued SerpApiError to detect empty results must check results[:error] instead:

# 1.0.3 and earlier
begin
  results = client.search(q: query)
rescue SerpApi::SerpApiError => e
  # also reached for searches with empty results
end

# now
results = client.search(q: query)
if results[:error]
  # the search succeeded, but the search engine returned no results
end

SerpApiError is still raised for failed requests and failed searches, so existing rescue blocks keep working for real errors.

Since this is breaking, the next release should be a major version (2.0.0). version.rb isn't bumped in this PR.

Changes

  • lib/serpapi/client.rb: raise only for a non-200 status or search_metadata.status == "Error". The status check also catches failed searches if the Search Archive API returns them with HTTP 200.
  • SerpApiError#serpapi_error and #search_id are now also set with symbolize_names: false. Before, they were always nil there.
  • README: new "Error handling" section with a breaking change note.
  • CHANGELOG: **BREAKING:** entry.

Testing

  • New spec/serpapi/client/error_handling_spec.rb with stubbed HTTP responses. It covers empty results from search and search_archive, string keys, failed searches returned with HTTP 200, and non-200 responses. 4 of its 7 examples fail against master.
  • bundle exec rake lint passes.
  • I didn't run the live API specs locally because I had no API key.

BREAKING CHANGE: JSON responses with HTTP 200 and a top-level error key no longer raise SerpApiError. SerpApi returns searches with empty results this way (search_metadata.status Success), so search, search_archive, and extract now return them as a Hash with the message in results[:error]. SerpApiError is raised only for non-200 responses or when search_metadata.status is Error.

Code that rescued SerpApiError to detect empty results must check results[:error] instead.

Also set SerpApiError#serpapi_error and #search_id when using symbolize_names: false.

Fixes serpapi#17

Co-authored-by: Claude Opus 5.5 (xhigh effort) <[email protected]>
@strzibny

Copy link
Copy Markdown
Contributor Author

Superseded by #42, opened from a branch on this repository.

@strzibny strzibny closed this Nov 13, 2026
@strzibny
strzibny deleted the no-raise-on-empty-results branch November 13, 2026 14:54
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Do not raise error for search archive results

1 participant