Module: Polytrix::Error

Included in:
StandardError
Defined in:
lib/polytrix/error.rb

Overview

All Polytrix errors and exceptions.

Class Method Summary collapse

Class Method Details

.formatted_exception(exception, title = 'Exception') ⇒ Array<String>

Creates an array of strings, representing a formatted exception that can be viewed by a human. Thanks to MiniTest for the inspiration upon which this output has been designed.

For example:

------Exception-------
Class: Polytrix::StandardError
Message: I have failed you
----------------------

Parameters:

  • exception (::StandardError)

    an exception

  • title (String) (defaults to: 'Exception')

    a custom title for the message (default: ‘“Exception”`)

Returns:

  • (Array<String>)

    a formatted message



50
51
52
53
54
55
56
57
# File 'lib/polytrix/error.rb', line 50

def self.formatted_exception(exception, title = 'Exception')
  [
    title.center(22, '-'),
    "Class: #{exception.class}",
    "Message: #{exception.message}",
    ''.center(22, '-')
  ]
end

.formatted_trace(exception) ⇒ Array<String>

Creates an array of strings, representing a formatted exception, containing backtrace and nested exception info as necessary, that can be viewed by a human.

For example:

------Exception-------
Class: Polytrix::StandardError
Message: Failure starting the party
---Nested Exception---
Class: IOError
Message: not enough directories for a party
------Backtrace-------
nil
----------------------

Parameters:

  • exception (::StandardError)

    an exception

Returns:

  • (Array<String>)

    a formatted message



24
25
26
27
28
29
30
31
32
33
# File 'lib/polytrix/error.rb', line 24

def self.formatted_trace(exception)
  arr = formatted_exception(exception).dup
  last = arr.pop
  if exception.respond_to?(:original) && exception.original
    arr += formatted_exception(exception.original, 'Nested Exception')
    last = arr.pop
  end
  arr += ['Backtrace'.center(22, '-'), exception.backtrace, last].flatten
  arr
end