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) ⇒ Array<String>

Creates an array of strings representing an exception's backtrace, wrapped in header and footer lines.

Parameters:

  • exception (::Exception)

    an exception

Returns:

  • (Array<String>)

    a formatted backtrace, or an empty array when the exception has none



68
69
70
71
72
73
74
75
76
77
78
# File 'lib/kitchen/errors.rb', line 68

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



95
96
97
98
99
100
101
102
# File 'lib/kitchen/errors.rb', line 95

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.

Parameters:

  • lines (Array<String>)

    Array of lines that needs to be printed



110
111
112
113
114
115
# File 'lib/kitchen/errors.rb', line 110

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