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.



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

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.



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

def backtrace
  @backtrace
end

#classnameObject

Returns the value of attribute classname.



55
56
57
# File 'lib/error_to_communicate/exception_info.rb', line 55

def classname
  @classname
end

#exceptionObject



68
69
70
71
72
73
74
75
# File 'lib/error_to_communicate/exception_info.rb', line 68

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.



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

def message
  @message
end

Class Method Details

.parse(exception) ⇒ Object



81
82
83
84
85
86
87
# File 'lib/error_to_communicate/exception_info.rb', line 81

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

.parse_backtrace(backtrace) ⇒ Object



89
90
91
92
93
94
95
# File 'lib/error_to_communicate/exception_info.rb', line 89

def self.parse_backtrace(backtrace)
  # 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 }
  backtrace.each_cons(2) { |crnt, pred| crnt.pred, pred.succ = pred, crnt }
  backtrace
end

.parseable?(exception) ⇒ Boolean

Returns:

  • (Boolean)


77
78
79
# File 'lib/error_to_communicate/exception_info.rb', line 77

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