1
0
Fork 0
mirror of https://github.com/rest-client/rest-client.git synced 2022-11-09 13:49:40 -05:00
rest-client--rest-client/lib/restclient/exceptions.rb

109 lines
2.8 KiB
Ruby
Raw Normal View History

module RestClient
2010-01-18 22:15:15 +01:00
2009-12-29 18:27:39 +01:00
# This is the base RestClient exception class. Rescue it if you want to
# catch any exception that your request might raise
class Exception < RuntimeError
2010-01-18 22:15:15 +01:00
attr_accessor :message, :response
2010-01-18 22:15:15 +01:00
def initialize response = nil
2009-12-29 18:27:39 +01:00
@response = response
end
2009-12-29 18:27:39 +01:00
def http_code
2010-01-18 22:15:15 +01:00
@response.code if @response
2009-12-29 18:27:39 +01:00
end
2009-12-29 18:27:39 +01:00
def http_body
2010-01-18 22:15:15 +01:00
@response
end
def inspect
"#{self.class} : #{http_code} #{message}"
end
end
# We will a create an exception for each status code, see http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html
module Exceptions
# Map http status codes to the corresponding exception class
EXCEPTIONS_MAP = {}
end
2010-01-20 18:38:20 +01:00
{304 => 'Not Modified',
305 => 'Use Proxy',
400 => 'Bad Request',
401 => 'Unauthorized',
403 => 'Forbidden',
404 => 'Resource Not Found',
405 => 'Method Not Allowed',
406 => 'Not Acceptable',
407 => 'Proxy Authentication Required',
408 => 'Request Timeout',
409 => 'Conflict',
410 => 'Gone',
411 => 'Length Required',
412 => 'Precondition Failed',
413 => 'Request Entity Too Large',
414 => 'Request-URI Too Long',
415 => 'Unsupported Media Type',
416 => 'Requested Range Not Satisfiable',
2010-01-18 22:15:15 +01:00
417 => 'Expectation Failed',
2010-01-20 18:38:20 +01:00
500 => 'Internal Server Error',
501 => 'Not Implemented',
502 => 'Bad Gateway',
503 => 'Service Unavailable',
504 => 'Gateway Timeout',
505 => 'HTTP Version Not Supported'}.each_pair do |code, message|
2010-01-18 22:15:15 +01:00
klass = Class.new(Exception) do
send :define_method, :message, Proc.new{message}
2009-12-29 18:27:39 +01:00
end
2010-01-18 22:15:15 +01:00
klass = const_set message.gsub(/ /, '').gsub(/-/, ''), klass
Exceptions::EXCEPTIONS_MAP[code] = klass
2009-12-29 18:27:39 +01:00
end
2009-12-29 18:27:39 +01:00
# A redirect was encountered; caught by execute to retry with the new url.
class Redirect < Exception
2010-01-18 22:15:15 +01:00
message = 'Redirect'
2009-12-29 18:27:39 +01:00
attr_accessor :url
2009-12-29 18:27:39 +01:00
def initialize(url)
@url = url
end
end
2008-10-14 23:38:13 +09:00
2009-12-29 18:27:39 +01:00
# The server broke the connection prior to the request completing. Usually
# this means it crashed, or sometimes that your network connection was
# severed before it could complete.
class ServerBrokeConnection < Exception
2010-01-18 22:15:15 +01:00
message = 'Server broke connection'
2009-12-29 18:27:39 +01:00
end
2009-12-29 18:27:39 +01:00
# The request failed, meaning the remote HTTP server returned a code other
# than success, unauthorized, or redirect.
#
# You can get the status code by e.http_code, or see anything about the
# response via e.response. For example, the entire result body (which is
# probably an HTML error page) is e.response.body.
2010-01-18 22:15:15 +01:00
class RequestFailed < Exception
2009-12-29 18:27:39 +01:00
def message
"HTTP status code #{http_code}"
end
def to_s
message
end
end
2010-01-18 22:15:15 +01:00
end
2008-06-20 19:09:57 -07:00
# backwards compatibility
class RestClient::Request
2009-12-29 18:27:39 +01:00
Redirect = RestClient::Redirect
Unauthorized = RestClient::Unauthorized
RequestFailed = RestClient::RequestFailed
end