Class: ToeTag::MessageSpec

Inherits:
ExceptionSpec show all
Defined in:
lib/toe_tag.rb

Overview

Wraps an exception class to allow matching against the message, too. Intended to be used in rescue clauses, for cases where one exception class (ActiveRecord::StatementInvalid, I’m looking at you) represents a host of underlying issues.

The recommended usage is to assign these to constants and treat them as a sort of meta-exception. This may be useful in combination with ExceptionCategory.

Examples:

BogusError = StandardError.with_message(/bogus/)

begin
  raise "bogus error man"
rescue BogusError => err
  p err
end

Instance Method Summary collapse

Methods included from ExceptionBehavior

#with_message

Constructor Details

#initialize(message, exception = StandardError) ⇒ MessageSpec

Returns a new instance of MessageSpec.



126
127
128
129
# File 'lib/toe_tag.rb', line 126

def initialize(message, exception = StandardError)
  self.exception = exception
  self.message   = message
end

Instance Method Details

#===(except) ⇒ Object



131
132
133
# File 'lib/toe_tag.rb', line 131

def ===(except)
  exception === except && message === except.message
end