diff --git a/lib/request_errors.rb b/lib/request_errors.rb index 39beaa2..cf84d5a 100644 --- a/lib/request_errors.rb +++ b/lib/request_errors.rb @@ -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 diff --git a/spec/request_errors_spec.rb b/spec/request_errors_spec.rb index b9391c3..7cc640e 100644 --- a/spec/request_errors_spec.rb +++ b/spec/request_errors_spec.rb @@ -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 => 'Error 1Error 2') - @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 => 'Specific error message') - @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