79

I'm working on a Rails application using HTTParty to make HTTP requests. How can I handle HTTP errors with HTTParty? Specifically, I need to catch HTTP 502 & 503 and other errors like connection refused and timeout errors.

Eben Geer
  • 3,696
  • 3
  • 31
  • 34
preethinarayan
  • 803
  • 1
  • 6
  • 6

3 Answers3

100

An instance of HTTParty::Response has a code attribute which contains the status code of the HTTP response. It's given as an integer. So, something like this:

response = HTTParty.get('http://twitter.com/statuses/public_timeline.json')

case response.code
  when 200
    puts "All good!"
  when 404
    puts "O noes not found!"
  when 500...600
    puts "ZOMG ERROR #{response.code}"
end
Jordan Running
  • 102,619
  • 17
  • 182
  • 182
  • Thanks! That's what I was planning on doing. Was wondering if there was any other way to do error handling. – preethinarayan Oct 26 '11 at 23:20
  • 51
    This answer doesn't address connection failures. – gtd Apr 01 '13 at 10:19
  • 2
    As to preethinarayan's comment, if you wanted to to catch/rescue the errors instead, you could always do something like: raise blablahblah if response.code != 200 I'm actually going to be doing something similar... – user435779 May 22 '13 at 20:04
  • response.code always returns 200 if the error is handled in the application. How to tackle that?? – Infant Dev Jul 16 '13 at 11:57
49

This answer addresses connection failures. If a URL isn´t found the status code won´t help you. Rescue it like this:

 begin
   HTTParty.get('http://google.com')
 rescue HTTParty::Error
   # don´t do anything / whatever
 rescue StandardError
   # rescue instances of StandardError,
   # i.e. Timeout::Error, SocketError etc
 end

For more information see: this github issue

stephenmurdoch
  • 34,024
  • 29
  • 114
  • 189
mklb
  • 1,215
  • 12
  • 8
  • 12
    Never catch everything. You should catch `HTTParty`s error base class. – Linus Oleander Mar 27 '15 at 17:46
  • Maybe the idea is to catch errors in the HTTParty code. HTTParty could raise errors that are not `HTTParty::Errors`. – B Seven Jan 11 '16 at 21:48
  • 1
    If there is no network at all I get a `SocketError`, just something to be aware of. You can try unplugging your ethernet cable to see this happen. – Kris Aug 25 '17 at 10:14
  • 1
    I use `HTTP_ERRORS` from suspenders, https://github.com/thoughtbot/suspenders/blob/master/templates/errors.rb and `HTTParty::Error` https://www.rubydoc.info/github/jnunemaker/httparty/HTTParty/Error, e.g. `rescue HTTParty::Error, *HTTP_ERRORS` – localhostdotdev Mar 29 '19 at 14:07
28

You can also use such handy predicate methods as ok? or bad_gateway? like this:

response = HTTParty.post(uri, options)
response.success?

The full list of all the possible responses can be found under Rack::Utils::HTTP_STATUS_CODES constant.

Artur INTECH
  • 6,024
  • 2
  • 37
  • 34
  • 2
    I just read this comment and it helped me a lot also! https://github.com/jnunemaker/httparty/issues/456#issuecomment-498778386 – kangkyu Jun 16 '19 at 00:06