Class: Lookout::Exception

Inherits:
Object show all
Defined in:
lib/lookout-3.0/exception.rb

Overview

Provides non-failing access to some of Exception’s methods. Used in subsystems where unhandled failure isn’t an option, such as Inspect and Results::Error. Also uses Encode so that all strings are ready for output.

Defined Under Namespace

Classes: Backtrace, Unknown

Instance Method Summary collapse

Instance Method Details

#backtraceBacktrace

Returns A non-failing backtrace wrapper of the exception backtrace.

Returns:

  • (Backtrace)

    A non-failing backtrace wrapper of the exception backtrace



49
# File 'lib/lookout-3.0/exception.rb', line 49

def backtrace; @backtrace ||= Backtrace.from(exception) end

#formatString

Returns The #header and #backtrace separated by a newline.

Returns:



67
# File 'lib/lookout-3.0/exception.rb', line 67

def format; [header, "\n", backtrace].join('') end

#headerString

Returns A heuristically generated UTF-8-encoded exception message “header”, possibly containing the exception message and the exception’s class’ name.

Returns:

  • (String)

    A heuristically generated UTF-8-encoded exception message “header”, possibly containing the exception message and the exception’s class’ name



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/lookout-3.0/exception.rb', line 30

def header
  # Chomping off a \n here isn’t 100 percent compatible with how Ruby 1.9
  # does it, but it simplifies the code and also makes the output better if
  # the message is a lone \n.
  message = (Lookout::Encode.new(String(exception.message)).call rescue '').chomp("\n")
  if RuntimeError == type and message.empty?
    'unhandled exception'
  elsif message.empty?
    type_name
  elsif type_name.empty? or type_name.start_with? '#'
    message
  else
    before, newline, after = message.partition("\n")
    [before, ' (', type_name, ')', newline, after].join('')
  end
end

#messageString Also known as: to_s

Returns The UTF-8-encoded exception message or the UTF-8-encoded exception message of any exception that was raised while trying to retrieve it.

Returns:

  • (String)

    The UTF-8-encoded exception message or the UTF-8-encoded exception message of any exception that was raised while trying to retrieve it



15
16
17
18
19
20
21
22
23
# File 'lib/lookout-3.0/exception.rb', line 15

def message
  @message ||= encode(begin
                        String(exception.message)
                      rescue => e
                        'cannot retrieve error message: %s' %
                          encode((String(e) rescue
                                  'and cannot retrieve error message for error that generated that error either; giving up'))
                      end)
end

#type#name, #inspect

Returns Either the exception’s class or an Unknown wrapper around the exception that was raised while trying to determine the class.

Returns:

  • (#name, #inspect)

    Either the exception’s class or an Unknown wrapper around the exception that was raised while trying to determine the class



54
# File 'lib/lookout-3.0/exception.rb', line 54

def type; @type ||= begin exception.class; rescue => e; Unknown.new(e) end end

#type_nameString

Returns The UTF-8-encoded name of the exception’s class.

Returns:

  • (String)

    The UTF-8-encoded name of the exception’s class



57
58
59
60
61
62
63
64
# File 'lib/lookout-3.0/exception.rb', line 57

def type_name
  @type_name ||= begin
                    Lookout::Encode.new(type.name).call
                  rescue => e
                   'cannot determine name of class of exception: %s' %
                     self.class.new(e)
                  end
end