Module: Kitchen::Error

Included in:
StandardError
Defined in:
lib/kitchen/errors.rb

Overview

All Kitchen errors and exceptions.

Author:

Class Method Summary collapse

Class Method Details

.formatted_backtrace(exception) ⇒ Object



63
64
65
66
67
68
69
70
71
72
73
# File 'lib/kitchen/errors.rb', line 63

def self.formatted_backtrace(exception)
  if exception.backtrace.nil?
    []
  else
    [
      "Backtrace".center(22, "-"),
      exception.backtrace,
      "End Backtrace".center(22, "-"),
    ]
  end
end

.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: Kitchen::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



90
91
92
93
94
95
96
97
# File 'lib/kitchen/errors.rb', line 90

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

.formatted_trace(exception, title = "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: Kitchen::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



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/kitchen/errors.rb', line 44

def self.formatted_trace(exception, title = "Exception")
  arr = formatted_exception(exception, title).dup
  arr += formatted_backtrace(exception)

  if exception.respond_to?(:original) && exception.original
    arr += if exception.original.is_a? Array
             exception.original.map do |composite_exception|
               formatted_trace(composite_exception, "Composite Exception").flatten
             end
           else
             [
               formatted_exception(exception.original, "Nested Exception"),
               formatted_backtrace(exception),
             ].flatten
           end
  end
  arr.flatten
end