mirror of
https://github.com/rest-client/rest-client.git
synced 2022-11-09 13:49:40 -05:00
added a base exception class for all RestClient related exceptions
This commit is contained in:
parent
95236bc6d2
commit
f62708401b
4 changed files with 42 additions and 8 deletions
|
@ -1,20 +1,43 @@
|
|||
require 'rexml/document'
|
||||
|
||||
module RestClient
|
||||
# This is the base RestClient exception class. Rescue it if you want to
|
||||
# catch any exception that your request might raise
|
||||
class Exception < RuntimeError
|
||||
def message(default=nil)
|
||||
self.class::ErrorMessage
|
||||
end
|
||||
end
|
||||
|
||||
# A redirect was encountered; caught by execute to retry with the new url.
|
||||
class Redirect < RuntimeError; end
|
||||
class Redirect < Exception
|
||||
ErrorMessage = "Redirect"
|
||||
|
||||
attr_accessor :url
|
||||
def initialize(url)
|
||||
@url = url
|
||||
end
|
||||
end
|
||||
|
||||
# Authorization is required to access the resource specified.
|
||||
class Unauthorized < RuntimeError; end
|
||||
class Unauthorized < Exception
|
||||
ErrorMessage = 'Unauthorized'
|
||||
end
|
||||
|
||||
# No resource was found at the given URL.
|
||||
class ResourceNotFound < RuntimeError; end
|
||||
class ResourceNotFound < Exception
|
||||
ErrorMessage = 'Resource not found'
|
||||
end
|
||||
|
||||
# The server broke the connection prior to the request completing.
|
||||
class ServerBrokeConnection < RuntimeError; end
|
||||
class ServerBrokeConnection < Exception
|
||||
ErrorMessage = 'Server broke connection'
|
||||
end
|
||||
|
||||
# The server took too long to respond.
|
||||
class RequestTimeout < RuntimeError; end
|
||||
class RequestTimeout < Exception
|
||||
ErrorMessage = 'Request timed out'
|
||||
end
|
||||
|
||||
# The request failed, meaning the remote HTTP server returned a code other
|
||||
# than success, unauthorized, or redirect.
|
||||
|
@ -25,7 +48,7 @@ module RestClient
|
|||
# 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.
|
||||
class RequestFailed < RuntimeError
|
||||
class RequestFailed < Exception
|
||||
attr_accessor :response
|
||||
|
||||
def initialize(response=nil)
|
||||
|
|
|
@ -52,7 +52,7 @@ module RestClient
|
|||
def execute
|
||||
execute_inner
|
||||
rescue Redirect => e
|
||||
@url = e.message
|
||||
@url = e.url
|
||||
execute
|
||||
end
|
||||
|
||||
|
|
|
@ -1,5 +1,16 @@
|
|||
require File.dirname(__FILE__) + '/base'
|
||||
|
||||
describe RestClient::Exception do
|
||||
it "sets the exception message to ErrorMessage" do
|
||||
RestClient::ResourceNotFound.new.message.should == 'Resource not found'
|
||||
end
|
||||
|
||||
it "contains exceptions in RestClient" do
|
||||
RestClient::Unauthorized.new.should be_a_kind_of(RestClient::Exception)
|
||||
RestClient::ServerBrokeConnection.new.should be_a_kind_of(RestClient::Exception)
|
||||
end
|
||||
end
|
||||
|
||||
describe RestClient::RequestFailed do
|
||||
before do
|
||||
@error = RestClient::RequestFailed.new
|
||||
|
|
|
@ -181,7 +181,7 @@ describe RestClient do
|
|||
|
||||
it "raises a Redirect with the new location when the response is in the 30x range" do
|
||||
res = mock('response', :code => '301', :header => { 'Location' => 'http://new/resource' })
|
||||
lambda { @request.process_result(res) }.should raise_error(RestClient::Redirect, 'http://new/resource')
|
||||
lambda { @request.process_result(res) }.should raise_error(RestClient::Redirect) { |e| e.url.should == 'http://new/resource'}
|
||||
end
|
||||
|
||||
it "raises Unauthorized when the response is 401" do
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue