Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

How to recover from 500 error #71

Closed
benilovj opened this Issue · 3 comments

3 participants

@benilovj

When paging through tickets using:

@client.search(query: query).each_page(&:block)

there is the occasional error:

{"error"=>"unavailable", "description"=>"Sorry, we could not complete your search query. Please try again in a moment."}

Stacktrace:

the server responded with status 500
/var/lib/gems/1.9.1/gems/faraday-0.8.4/lib/faraday/response/raise_error.rb:8:in `on_complete'
/var/lib/gems/1.9.1/gems/faraday-0.8.4/lib/faraday/response.rb:9:in `block in call'
/var/lib/gems/1.9.1/gems/faraday-0.8.4/lib/faraday/response.rb:63:in `on_complete'
/var/lib/gems/1.9.1/gems/faraday-0.8.4/lib/faraday/response.rb:8:in `call'
/var/lib/gems/1.9.1/gems/faraday-0.8.4/lib/faraday/request/authorization.rb:36:in `call'
/var/lib/gems/1.9.1/gems/faraday-0.8.4/lib/faraday/connection.rb:226:in `run_request'
/var/lib/gems/1.9.1/gems/faraday-0.8.4/lib/faraday/connection.rb:87:in `get'
/var/lib/gems/1.9.1/gems/zendesk_api-0.2.6/lib/zendesk_api/collection.rb:174:in `fetch'
/var/lib/gems/1.9.1/gems/zendesk_api-0.2.6/lib/zendesk_api/rescue.rb:43:in `block (2 levels) in rescue_client_error'
/var/lib/gems/1.9.1/gems/zendesk_api-0.2.6/lib/zendesk_api/collection.rb:201:in `to_a'
/var/lib/gems/1.9.1/gems/zendesk_api-0.2.6/lib/zendesk_api/collection.rb:285:in `method_missing'
/var/lib/gems/1.9.1/gems/zendesk_api-0.2.6/lib/zendesk_api/collection.rb:210:in `each_page'

How is it possible to recover and retry the fetch in those cases?

@3en
Collaborator

+1

@steved
Owner

Check out the latest master, it allows the pattern:

class SearchError < Exception; end

client.insert_callback do |env|
  if env[:status] == 500 && env[:url].request_uri =~ %r{/search}
    # Must not be a Faraday::Error::ClientError, but could be any standard ruby error
    raise SearchError
  end
end

search = client.search(query: query)

begin
  search.each_page(&:block)
rescue SearchError
  retry
end

This will raise a different error for search 500s and then retry the block (starting from the page that erred each time).
I'll release a new version shortly if this is acceptable.

@benilovj

Thanks for the quick turnaround on this, Steve. My feedback:

  1. It'd be good to have this documented somewhere (README?)
  2. I like being able to retry and continue from page that failed. I'm less keen on the "catching errors with callbacks" business (I've raised a separate issue about it: #72). Why can't #search just raise a SearchError without the onus being on the app developer to define it via a callback?
@steved steved closed this
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.