Skip to content

Enhance SerpApiError information #15

Description

@ron-at-swgy

Provide a search_id for failed searches when available.

The SerpApiError type does not provide any information. Ideally, there would be an attribute for storing the search ID that would then be assigned before raising the error. Additionally, information is included in the error message that is not included in the error message itself:

          if data.instance_of?(Hash) && data.key?(:error)
            raise SerpApiError, "HTTP request failed with error: #{data[:error]} from url: https://#{BACKEND}#{endpoint}, params: #{params}, decoder: #{decoder}, response status: #{response.status} "
          elsif response.status != 200
            raise SerpApiError, "HTTP request failed with response status: #{response.status} reponse: #{data} on get url: https://#{BACKEND}#{endpoint}, params: #{params}, decoder: #{decoder}"
          end

In practice, I have a case where a search returns an empty set - fine - but the client treats this as an error condition. Without saving the search ID in the error, I cannot automatically collect failed search IDs. Instead I'm forced to cross reference the search parameters with my list of historical searches.

I'd also suggest that we not consider empty searches to be an error simply by virtue of an empty result. Regardless, code using the client cannot differentiate between an error involving network connectivity and an empty result set.

Activity

  1. self-assigned this
    on Dec 6, 2025
  2. jvmvik commented on Dec 22, 2025

    @jvmvik
    Contributor

    Really good point. With the large number of endpoints implemented by SerpApi.com, SerpApiError class should be enhanced to store search parameters and response metadata which includes search ID.
    I guess all the library should be improve with the same behavior.

    For the empty result, the backend service do the work of figure out any error, and provide an error field easy to consume.
    We don't want to wire any complex logic in the library to figure the pontention problem with the 100+ API endpoints.

  3. jvmvik commented on Dec 22, 2025

    @jvmvik
    Contributor

    can you work on the implementation fix on a branch ?

    for example: code changes in the library

    message = "HTTP request failed with error: #{data[:error]} from url: https://#{BACKEND}#{endpoint}, params: #{params}, decoder: #{decoder}, response status: #{response.status} "
    raise SerpApiError(message, result) 

    them the user can obtain the raw results from the exception.

    begin
      results = client.search(q: 'coffee')
    rescue SerpApiError => e
     search_id = e.results[:search_metadata][:id]
     puts "search failed with search_id=#{search_id} with results="
     pp e.results
    end
  4. jvmvik commented on Dec 29, 2025

    @jvmvik
    Contributor

    starting to review #16

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions