Exception: Exception

Defined in:
lib/squash/ruby/exception_additions.rb

Overview

Reopens the Exception class to add a convenient way of appending user data to an exception at the time of the raise.

Examples:

raise ArgumentError.new("value must be a number", :value => value) unless value.kind_of?(Fixnum)

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.new(message, user_data = {}) ⇒ Exception

Creates a new exception instance, optionally with user data.

Raises:

  • (ArgumentError)

    If data contains the keys mesg or bt.



31
32
33
34
35
36
37
38
# File 'lib/squash/ruby/exception_additions.rb', line 31

def self.new(*args)
  user_data = if args.last.is_a?(Hash)
                args.pop
              else
                {}
              end
  super(*args).user_data(user_data)
end

Instance Method Details

#user_data(data) ⇒ Exception

Annotates this exception with user data. Merges in any new data with existing user data.

Raises:

  • (ArgumentError)

    If data contains the keys mesg or bt.



47
48
49
50
51
52
53
# File 'lib/squash/ruby/exception_additions.rb', line 47

def user_data(data)
  Squash::Ruby.check_user_data data
  data.each do |ivar, value|
    instance_variable_set :"@#{ivar}", value
  end
  self
end