Exception
Exceptions (see http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html)
- for result codes between
200and207, aRestMan::Responsewill be returned - for result codes
301,302or307, the redirection will be followed if the request is aGETor aHEAD - for result code
303, the redirection will be followed and the request transformed into aGET - for other cases, a
RestMan::ExceptionWithResponseholding the Response will be raised; a specific exception class will be thrown for known error codes - call
.responseon the exception to get the server’s response
Catch errors
Most exceptions are collected under RestMan::RequestFaield.
begin
RestMan.get "http://example.com/test"
rescue => RestMan::RequestFailed => e
e.response #=> RestMan::Response
end
Catch socket errors
RestMan will propagate up socket errors without modification:
RestMan.get 'http://example.com/test'
#=> Exception: Errno::ECONNREFUSED: Connection refused - connect(2) for "localhost" port 12345
Catch EOFError
RestMan::ServerBrokeConnection is translated from EOFError to give a better error message.
begin
RestMan.get 'http://example.com/test'
rescue RestMan::ServerBrokeConnection => error
error.message #=> Server broke connection
end
Catch specific HTTP errors
It may be more straightforward to use exceptions to handle different HTTP error response cases.
begin
response = RestMan.get('http://example.com/resource')
rescue RestMan::Unauthorized, RestMan::Forbidden => error
puts 'Access denied'
return error.response
rescue RestMan::ImATeapot =>error
puts 'The server is a teapot! # RFC 2324'
return error.response
else
puts 'It worked!'
return response
end