Method: Sym::App.error

Defined in:
lib/sym/app.rb

.error(config: {}, exception: nil, type: nil, details: nil, reason: nil, comments: nil, command: nil) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/sym/app.rb', line 32

def self.error(config: {},
  exception: nil,
  type: nil,
  details: nil,
  reason: nil,
  comments: nil,
  command: nil)

  lines = []

  error_type    = "#{(type || exception.class.name)}"
  error_details = (details || exception.message)

  if exception && (config && config[:trace] || reason == 'Unknown Error')
    lines << "#{error_type.red.underlined}: #{error_details.white.on.red}\n"
    lines << exception.backtrace.join("\n").red.bold if config[:trace]
    lines << "\n"
  end

  operation = command ? "to #{command.class.short_name.to_s.humanize.downcase}" : ''
  reason    = exception.message if reason.nil? && exception

  lines << " error #{operation}".white.on.red+ " #{reason}".bold.red if reason
  lines << "#{comments}" if comments

  error_report = lines.compact.join("\n") || 'Undefined error'

  self.out.puts(error_report) if error_report.present?
  self.exit_code = 1
end