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
|
||||
# This is the base RestClient exception class. Rescue it if you want to
|
||||
# catch any exception that your request might raise
|
||||
|
@ -59,14 +57,8 @@ module RestClient
|
|||
@response.code.to_i if @response
|
||||
end
|
||||
|
||||
def message(default="Unknown error, HTTP status code #{http_code}")
|
||||
return default unless @response
|
||||
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(" / ")
|
||||
def message
|
||||
"HTTP status code #{http_code}"
|
||||
end
|
||||
|
||||
def to_s
|
||||
|
|
|
@ -12,28 +12,20 @@ describe RestClient::Exception do
|
|||
end
|
||||
|
||||
describe RestClient::RequestFailed do
|
||||
before do
|
||||
@error = RestClient::RequestFailed.new
|
||||
it "stores the http response on the exception" do
|
||||
begin
|
||||
raise RestClient::RequestFailed, :response
|
||||
rescue RestClient::RequestFailed => e
|
||||
e.response.should == :response
|
||||
end
|
||||
end
|
||||
|
||||
it "extracts the error message from xml" do
|
||||
@error.response = mock('response', :code => '422', :body => '<errors><error>Error 1</error><error>Error 2</error></errors>')
|
||||
@error.message.should == 'Error 1 / Error 2'
|
||||
it "http_code convenience method for fetching the code as an integer" do
|
||||
RestClient::RequestFailed.new(mock('res', :code => '502')).http_code.should == 502
|
||||
end
|
||||
|
||||
it "ignores responses without xml since they might contain sensitive data" do
|
||||
@error.response = mock('response', :code => '500', :body => 'Syntax error in SQL query: SELECT * FROM ...')
|
||||
@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'
|
||||
it "shows the status code in the message" do
|
||||
RestClient::RequestFailed.new(mock('res', :code => '502')).to_s.should match(/502/)
|
||||
end
|
||||
end
|
||||
|
||||
|
|
Loading…
Reference in a new issue