Class: ErrorToCommunicate::ExceptionInfo

Inherits:
Object
  • Object
show all
Defined in:
lib/error_to_communicate/exception_info.rb,
lib/error_to_communicate/exception_info.rb

Overview

Wraps an exception in our internal data structures So we can normalize the different things that come in, and you can use code that needs this info without going to the effort of raising exceptions

Defined Under Namespace

Classes: Location

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attributes) ⇒ ExceptionInfo

Returns a new instance of ExceptionInfo.



62
63
64
65
66
67
# File 'lib/error_to_communicate/exception_info.rb', line 62

def initialize(attributes)
  self.exception = attributes.fetch :exception, nil
  self.classname = attributes.fetch :classname
  self.message   = attributes.fetch :message
  self.backtrace = attributes.fetch :backtrace
end

Instance Attribute Details

#backtraceObject

Returns the value of attribute backtrace.



59
60
61
# File 'lib/error_to_communicate/exception_info.rb', line 59

def backtrace
  @backtrace
end

#bindingObject

Returns the value of attribute binding.



60
61
62
# File 'lib/error_to_communicate/exception_info.rb', line 60

def binding
  @binding
end

#classnameObject

Returns the value of attribute classname.



57
58
59
# File 'lib/error_to_communicate/exception_info.rb', line 57

def classname
  @classname
end

#exceptionObject



71
72
73
74
75
76
77
78
# File 'lib/error_to_communicate/exception_info.rb', line 71

def exception
  @warned_about_exception ||= begin
    warn "The exception is recorded for debugging purposes only.\n"
    warn "Don't write code that depends on it, or we can't generically use the ExceptionInfo structure."
    true
  end
  @exception
end

#messageObject

Returns the value of attribute message.



58
59
60
# File 'lib/error_to_communicate/exception_info.rb', line 58

def message
  @message
end

Class Method Details

.parse(exception, binding) ⇒ Object



84
85
86
87
88
89
90
# File 'lib/error_to_communicate/exception_info.rb', line 84

def self.parse(exception, binding)
  return exception if exception.kind_of? self
  new exception: exception,
      classname: exception.class.name,
      message:   exception.message,
      backtrace: parse_backtrace(exception.backtrace, binding)
end

.parse_backtrace(backtrace, binding) ⇒ Object



92
93
94
95
96
97
98
# File 'lib/error_to_communicate/exception_info.rb', line 92

def self.parse_backtrace(backtrace, binding)
  # Really, there are better methods, e.g. backtrace_locations,
  # but they're unevenly implemented across versions and implementations
  backtrace = (backtrace||[]).map { |line| Location.parse line, binding }
  backtrace.each_cons(2) { |crnt, pred| crnt.pred, pred.succ = pred, crnt }
  backtrace
end

.parseable?(exception, binding) ⇒ Boolean

Returns:

  • (Boolean)


80
81
82
# File 'lib/error_to_communicate/exception_info.rb', line 80

def self.parseable?(exception, binding)
  exception.respond_to?(:message) && exception.respond_to?(:backtrace)
end