1
0
Fork 0
mirror of https://github.com/rest-client/rest-client.git synced 2022-11-09 13:49:40 -05:00

RestClient::Exception#message should always return a String, never a NilClass.

RestClient::Exception subclasses RuntimeError, which is a subclass of Exception.  According to the Ruby documentation (http://www.ruby-doc.org/ruby-1.9/classes/Exception.html#M000100), the +message+ method is expected to return a string.  Before this commit, RestClient::Exception#message would return nil if a message was not set.

This presents a problem for any code that rescues a RestClient::Exception and expects a string in the +message+.

For example:

    begin
      raise RestClient::Exception
    rescue Exception => e
      puts e.message + "\n" + "More information"
    end

Or, for example, the real life code that uncovered this in production: a0d6cdb263/lib/delayed/job.rb (L72)

This fix makes RestClient::Exceptions better citizens of the Ruby ecosystem. :)
This commit is contained in:
Michael Klett 2010-11-20 11:50:10 -05:00 committed by Julien Kirch
parent 7cc9b86647
commit 2caea9b9f0
3 changed files with 30 additions and 2 deletions

View file

@ -6,6 +6,7 @@
- block passing in Resource#[] (patch provided by Niko Dittmann)
- cookies set in a response should be kept in a redirect
- HEAD requests should process parameters just like GET (patch provided by Rob Eanes)
- Exception message should never be nil (patch provided by Michael Klett)
# 1.6.0

View file

@ -80,7 +80,8 @@ module RestClient
# For example, the entire result body (which is
# probably an HTML error page) is e.response.
class Exception < RuntimeError
attr_accessor :message, :response
attr_accessor :response
attr_writer :message
def initialize response = nil, initial_response_code = nil
@response = response
@ -110,6 +111,10 @@ module RestClient
def to_s
inspect
end
def message
@message || self.class.name
end
end
@ -162,7 +167,10 @@ module RestClient
# this means it crashed, or sometimes that your network connection was
# severed before it could complete.
class ServerBrokeConnection < Exception
message = 'Server broke connection'
def initialize(message = 'Server broke connection')
super nil, nil
self.message = message
end
end
class SSLCertificateNotVerified < Exception

View file

@ -4,6 +4,18 @@ require 'webmock/rspec'
include WebMock
describe RestClient::Exception do
it "returns a 'message' equal to the class name if the message is not set, because 'message' should not be nil" do
e = RestClient::Exception.new
e.message.should == "RestClient::Exception"
end
it "returns the 'message' that was set" do
e = RestClient::Exception.new
message = "An explicitly set message"
e.message = message
e.message.should == message
end
it "sets the exception message to ErrorMessage" do
RestClient::ResourceNotFound.new.message.should == 'Resource Not Found'
end
@ -14,6 +26,13 @@ describe RestClient::Exception do
end
end
describe RestClient::ServerBrokeConnection do
it "should have a default message of 'Server broke connection'" do
e = RestClient::ServerBrokeConnection.new
e.message.should == 'Server broke connection'
end
end
describe RestClient::RequestFailed do
before do
@response = mock('HTTP Response', :code => '502')