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


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

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


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

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


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

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

.warn_on_stderr(lines) ⇒ Object

Log a warn message on STDERR device. This will help to distinguish between the errors and output when parsing the output from the commands like kitchen diagnose.


104
105
106
107
108
109
# File 'lib/kitchen/errors.rb', line 104

def self.warn_on_stderr(lines)
  Array(lines).each do |line|
    line = Color.colorize(line, :blue) if Kitchen.tty?
    $stderr.puts(line)
  end
end