You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Commit 77edff3
Browse filesBrowse the repository at this point in the historyBrowse files
Return searches with empty results instead of raising
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#17
Co-authored-by: Claude Opus 5.5 (xhigh effort) <noreply@anthropic.com>
Copy file name to clipboardExpand all lines: CHANGELOG.md
+2Lines changed: 2 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,4 +1,6 @@
1
1
# Changelog
2
+
*[Unreleased]**BREAKING:** JSON responses with HTTP 200 and a top-level `error` key no longer raise `SerpApiError`. SerpApi returns searches with empty results this way (e.g. "Google hasn't returned any results for this query."), 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 #17
3
+
*[Unreleased] Set `SerpApiError#serpapi_error` and `#search_id` when using `symbolize_names: false`
2
4
*[Unreleased] Add native Extract API support via the `extract` method
3
5
*[Unreleased] Apply the `timeout` option to persistent connections
Copy file name to clipboardExpand all lines: README.md
+28Lines changed: 28 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -170,6 +170,34 @@ puts 'done'
170
170
171
171
This code shows a simple solution to batch searches asynchronously into a [queue](https://en.wikipedia.org/wiki/Queue_(abstract_data_type)). Each search may take up to a few seconds to complete. By the time the first element pops out of the queue, the search results might already be available in the archive. If not, the `search_archive` method blocks until the search results are available.
172
172
173
+
## Error handling
174
+
175
+
`SerpApi::SerpApiError` is raised when a request fails: the HTTP status is not `200` (for example, an invalid API key, a missing parameter, or no searches left) or the search finished with `search_metadata.status` set to `Error`. The error includes the details returned by SerpApi:
176
+
177
+
```ruby
178
+
begin
179
+
results = client.search(q:"coffee")
180
+
rescueSerpApi::SerpApiError => e
181
+
e.serpapi_error # => "Invalid API key. Your API key should be here: https://serpapi.com/manage-api-key"
182
+
e.response_status # => 401
183
+
e.search_id # => ID of the failed search, when SerpApi created one
184
+
end
185
+
```
186
+
187
+
A search with no results from the search engine is still successful, so it is returned like any other search, with the message in `results[:error]`:
188
+
189
+
```ruby
190
+
results = client.search(q:"a query without results")
191
+
results[:search_metadata][:status] # => "Success"
192
+
results[:error] # => "Google hasn't returned any results for this query."
193
+
```
194
+
195
+
The same applies to `search_archive`, so you can retrieve the metadata of archived searches with empty results.
196
+
197
+
> **Breaking change:** version 1.0.3 and earlier raised `SerpApiError` for any response with an `error` key, including searches with empty results. If you rescued `SerpApiError` to detect empty results, check `results[:error]` instead.
198
+
199
+
See [Status and Error Codes](https://serpapi.com/api-status-and-error-codes) for the full list of statuses.
200
+
173
201
## Examples
174
202
175
203
Here are some examples for some of our most popular APIs. You can find the full list of supported engines and parameters in our [documentation](https://serpapi.com/search-engine-apis).
0 commit comments