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



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/sym/app.rb', line 38

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)

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

  if exception && (config && config[:trace] || reason == 'Unknown Error')
    lines << "#{error_type.bold.red}:\n#{error_details.red.italic}\n" + ''.normal
    lines << exception.backtrace.join("\n").red.bold if config[:trace]
    lines << "\n"
  else
    lines << " ✖ Sym Error #{operation}:".bold.red + (reason ? " #{reason} ".red.italic: " #{error_details}")[0..70] + ' '.normal + "\n"
    lines << "#{comments}" if comments
  end

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

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