mirror of
https://github.com/rest-client/rest-client.git
synced 2022-11-09 13:49:40 -05:00
support default error messages in RequestFailed to avoid checking if message == "Unknown ..."
This commit is contained in:
parent
39f3513dae
commit
88660a038b
2 changed files with 12 additions and 4 deletions
|
@ -19,16 +19,14 @@ module RestClient
|
|||
@response.code.to_i
|
||||
end
|
||||
|
||||
def message
|
||||
def message(default = "Unknown error")
|
||||
return "Resource not found" if http_code == 404
|
||||
parse_error_xml
|
||||
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(" / ")
|
||||
rescue
|
||||
"Unknown error"
|
||||
end
|
||||
end
|
||||
end
|
|
@ -14,4 +14,14 @@ describe RestClient::RequestFailed do
|
|||
@error.response = mock('response', :code => '500', :body => 'Syntax error in SQL query: SELECT * FROM ...')
|
||||
@error.message.should == 'Unknown error'
|
||||
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
|
Loading…
Reference in a new issue