Module: Exceptions

Included in:
CachedNestedFileReader, MarkdownExec
Defined in:
lib/exceptions.rb

Class Method Summary collapse

Class Method Details

.error_handler(name = '', opts = {}, backtrace: $@, color_symbol: :red, format_string: "\nError: %{name} -- %{message}", show_backtrace: false, take_count: 16) ⇒ Object

Raises:

  • (ArgumentError)


8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/exceptions.rb', line 8

def self.error_handler(
  name = '',
  opts = {},
  backtrace: $@,
  color_symbol: :red,
  format_string: "\nError: %{name} -- %{message}",
  show_backtrace: false,
  take_count: 16
)
  warn(error = AnsiString.new(format(format_string,
                                     { name: name,
                                       message: $! })).send(color_symbol))
  if show_backtrace && backtrace
    warn(backtrace.select do |s|
           s.include? 'markdown_exec'
         end.reject { |s| s.include? 'vendor' }.take(take_count).map.with_index { |line, ind| " *   #{ind}: #{line}" })
  end

  binding.pry if $tap_enable
  raise ArgumentError, error unless opts.fetch(:abort, true)

  exit 1
end

.warn_format(message = '', opts = {}) ⇒ Object

Raises:

  • (ArgumentError)


32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/exceptions.rb', line 32

def self.warn_format(message = '', opts = {})
  warn(
    error = AnsiString.new(format(
                             opts.fetch(:format_string, "\nError: %{error}"),
                             { error: message }
                           )).send(opts.fetch(:color_symbol, :yellow))
  )
  # warn(caller.take(4).map.with_index { |line, ind| " *   #{ind}: #{line}" })

  binding.pry if $tap_enable
  raise ArgumentError, error unless opts.fetch(:abort, false)

  exit 1
end