diff --git a/lib/request_errors.rb b/lib/request_errors.rb index f0b6328..de592d3 100644 --- a/lib/request_errors.rb +++ b/lib/request_errors.rb @@ -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 \ No newline at end of file diff --git a/spec/request_errors_spec.rb b/spec/request_errors_spec.rb index 83e533a..ee0acaf 100644 --- a/spec/request_errors_spec.rb +++ b/spec/request_errors_spec.rb @@ -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 => 'Specific error message') + @error.message('Custom default message').should == 'Specific error message' + end end \ No newline at end of file