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

add the http response to Unauthorized and ResourceNotFound too, in case the user wants to parse the body

This commit is contained in:
Pedro Belo 2008-11-26 17:42:16 -08:00
parent 51d55dc4ce
commit fd41d47025
3 changed files with 28 additions and 15 deletions

View file

@ -7,6 +7,19 @@ module RestClient
end
end
# Base RestClient exception when there's a response available
class ExceptionWithResponse < Exception
attr_accessor :response
def initialize(response=nil)
@response = response
end
def http_code
@response.code.to_i if @response
end
end
# A redirect was encountered; caught by execute to retry with the new url.
class Redirect < Exception
ErrorMessage = "Redirect"
@ -18,12 +31,12 @@ module RestClient
end
# Authorization is required to access the resource specified.
class Unauthorized < Exception
class Unauthorized < ExceptionWithResponse
ErrorMessage = 'Unauthorized'
end
# No resource was found at the given URL.
class ResourceNotFound < Exception
class ResourceNotFound < ExceptionWithResponse
ErrorMessage = 'Resource not found'
end
@ -46,17 +59,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 < Exception
attr_accessor :response
def initialize(response=nil)
@response = response
end
def http_code
@response.code.to_i if @response
end
class RequestFailed < ExceptionWithResponse
def message
"HTTP status code #{http_code}"
end

View file

@ -201,9 +201,9 @@ module RestClient
raise Redirect, url
elsif res.code == "401"
raise Unauthorized
raise Unauthorized, res
elsif res.code == "404"
raise ResourceNotFound
raise ResourceNotFound, res
else
raise RequestFailed, res
end

View file

@ -29,6 +29,16 @@ describe RestClient::RequestFailed do
end
end
describe RestClient::ResourceNotFound do
it "also has the http response attached" do
begin
raise RestClient::ResourceNotFound, :response
rescue RestClient::ResourceNotFound => e
e.response.should == :response
end
end
end
describe "backwards compatibility" do
it "alias RestClient::Request::Redirect to RestClient::Redirect" do
RestClient::Request::Redirect.should == RestClient::Redirect