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:
parent
51d55dc4ce
commit
fd41d47025
3 changed files with 28 additions and 15 deletions
|
@ -7,6 +7,19 @@ module RestClient
|
||||||
end
|
end
|
||||||
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.
|
# A redirect was encountered; caught by execute to retry with the new url.
|
||||||
class Redirect < Exception
|
class Redirect < Exception
|
||||||
ErrorMessage = "Redirect"
|
ErrorMessage = "Redirect"
|
||||||
|
@ -18,12 +31,12 @@ module RestClient
|
||||||
end
|
end
|
||||||
|
|
||||||
# Authorization is required to access the resource specified.
|
# Authorization is required to access the resource specified.
|
||||||
class Unauthorized < Exception
|
class Unauthorized < ExceptionWithResponse
|
||||||
ErrorMessage = 'Unauthorized'
|
ErrorMessage = 'Unauthorized'
|
||||||
end
|
end
|
||||||
|
|
||||||
# No resource was found at the given URL.
|
# No resource was found at the given URL.
|
||||||
class ResourceNotFound < Exception
|
class ResourceNotFound < ExceptionWithResponse
|
||||||
ErrorMessage = 'Resource not found'
|
ErrorMessage = 'Resource not found'
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -46,17 +59,7 @@ module RestClient
|
||||||
# You can get the status code by e.http_code, or see anything about the
|
# 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
|
# response via e.response. For example, the entire result body (which is
|
||||||
# probably an HTML error page) is e.response.body.
|
# probably an HTML error page) is e.response.body.
|
||||||
class RequestFailed < Exception
|
class RequestFailed < ExceptionWithResponse
|
||||||
attr_accessor :response
|
|
||||||
|
|
||||||
def initialize(response=nil)
|
|
||||||
@response = response
|
|
||||||
end
|
|
||||||
|
|
||||||
def http_code
|
|
||||||
@response.code.to_i if @response
|
|
||||||
end
|
|
||||||
|
|
||||||
def message
|
def message
|
||||||
"HTTP status code #{http_code}"
|
"HTTP status code #{http_code}"
|
||||||
end
|
end
|
||||||
|
|
|
@ -201,9 +201,9 @@ module RestClient
|
||||||
|
|
||||||
raise Redirect, url
|
raise Redirect, url
|
||||||
elsif res.code == "401"
|
elsif res.code == "401"
|
||||||
raise Unauthorized
|
raise Unauthorized, res
|
||||||
elsif res.code == "404"
|
elsif res.code == "404"
|
||||||
raise ResourceNotFound
|
raise ResourceNotFound, res
|
||||||
else
|
else
|
||||||
raise RequestFailed, res
|
raise RequestFailed, res
|
||||||
end
|
end
|
||||||
|
|
|
@ -29,6 +29,16 @@ describe RestClient::RequestFailed do
|
||||||
end
|
end
|
||||||
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
|
describe "backwards compatibility" do
|
||||||
it "alias RestClient::Request::Redirect to RestClient::Redirect" do
|
it "alias RestClient::Request::Redirect to RestClient::Redirect" do
|
||||||
RestClient::Request::Redirect.should == RestClient::Redirect
|
RestClient::Request::Redirect.should == RestClient::Redirect
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue