mirror of
https://github.com/rest-client/rest-client.git
synced 2022-11-09 13:49:40 -05:00
remove xml parsing on failed requests
This commit is contained in:
parent
bd53adf1d7
commit
662972ce1a
2 changed files with 12 additions and 28 deletions
|
@ -1,5 +1,3 @@
|
||||||
require 'rexml/document'
|
|
||||||
|
|
||||||
module RestClient
|
module RestClient
|
||||||
# This is the base RestClient exception class. Rescue it if you want to
|
# This is the base RestClient exception class. Rescue it if you want to
|
||||||
# catch any exception that your request might raise
|
# catch any exception that your request might raise
|
||||||
|
@ -59,14 +57,8 @@ module RestClient
|
||||||
@response.code.to_i if @response
|
@response.code.to_i if @response
|
||||||
end
|
end
|
||||||
|
|
||||||
def message(default="Unknown error, HTTP status code #{http_code}")
|
def message
|
||||||
return default unless @response
|
"HTTP status code #{http_code}"
|
||||||
parse_error_xml rescue default
|
|
||||||
end
|
|
||||||
|
|
||||||
def parse_error_xml
|
|
||||||
xml_errors = REXML::Document.new(@response.body).elements.to_a("//errors/error")
|
|
||||||
xml_errors.empty? ? raise : xml_errors.map { |a| a.text }.join(" / ")
|
|
||||||
end
|
end
|
||||||
|
|
||||||
def to_s
|
def to_s
|
||||||
|
|
|
@ -12,28 +12,20 @@ describe RestClient::Exception do
|
||||||
end
|
end
|
||||||
|
|
||||||
describe RestClient::RequestFailed do
|
describe RestClient::RequestFailed do
|
||||||
before do
|
it "stores the http response on the exception" do
|
||||||
@error = RestClient::RequestFailed.new
|
begin
|
||||||
|
raise RestClient::RequestFailed, :response
|
||||||
|
rescue RestClient::RequestFailed => e
|
||||||
|
e.response.should == :response
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
it "extracts the error message from xml" do
|
it "http_code convenience method for fetching the code as an integer" do
|
||||||
@error.response = mock('response', :code => '422', :body => '<errors><error>Error 1</error><error>Error 2</error></errors>')
|
RestClient::RequestFailed.new(mock('res', :code => '502')).http_code.should == 502
|
||||||
@error.message.should == 'Error 1 / Error 2'
|
|
||||||
end
|
end
|
||||||
|
|
||||||
it "ignores responses without xml since they might contain sensitive data" do
|
it "shows the status code in the message" do
|
||||||
@error.response = mock('response', :code => '500', :body => 'Syntax error in SQL query: SELECT * FROM ...')
|
RestClient::RequestFailed.new(mock('res', :code => '502')).to_s.should match(/502/)
|
||||||
@error.message.should == 'Unknown error, HTTP status code 500'
|
|
||||||
end
|
|
||||||
|
|
||||||
it "accepts a default error message" do
|
|
||||||
@error.response = mock('response', :code => '500', :body => 'Internal Server Error')
|
|
||||||
@error.message('Custom default message').should == 'Custom default message'
|
|
||||||
end
|
|
||||||
|
|
||||||
it "doesn't show the default error message when there's something in the xml" do
|
|
||||||
@error.response = mock('response', :code => '422', :body => '<errors><error>Specific error message</error></errors>')
|
|
||||||
@error.message('Custom default message').should == 'Specific error message'
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue