class Toys::ContextualError

A wrapper exception used to provide user-oriented context for an exception

Attributes

banner[R]
cause[R]
config_line[RW]
config_path[RW]
tool_args[RW]
tool_name[RW]

Public Class Methods

capture(banner, **opts) { || ... } click to toggle source

@private

# File lib/toys/errors.rb, line 90
def capture(banner, **opts)
  yield
rescue ContextualError => e
  add_fields_if_missing(e, opts)
  raise e
rescue ::ScriptError, ::StandardError => e
  raise ContextualError.new(e, banner, **opts)
end
capture_path(banner, path, **opts) { || ... } click to toggle source

@private

# File lib/toys/errors.rb, line 70
def capture_path(banner, path, **opts)
  yield
rescue ContextualError => e
  add_fields_if_missing(e, opts)
  add_config_path_if_missing(e, path)
  raise e
rescue ::SyntaxError => e
  if (match = /#{::Regexp.escape(path)}:(\d+)/.match(e.message))
    opts = opts.merge(config_path: path, config_line: match[1].to_i)
    e = ContextualError.new(e, banner, **opts)
  end
  raise e
rescue ::ScriptError, ::StandardError => e
  e = ContextualError.new(e, banner)
  add_fields_if_missing(e, opts)
  add_config_path_if_missing(e, path)
  raise e
end
new(cause, banner, config_path: nil, config_line: nil, tool_name: nil, tool_args: nil) click to toggle source

@private

Calls superclass method
# File lib/toys/errors.rb, line 47
def initialize(cause, banner,
               config_path: nil, config_line: nil,
               tool_name: nil, tool_args: nil)
  super("#{banner} : #{cause.message} (#{cause.class})")
  @cause = cause
  @banner = banner
  @config_path = config_path
  @config_line = config_line
  @tool_name = tool_name
  @tool_args = tool_args
  set_backtrace(cause.backtrace)
end

Private Class Methods

add_config_path_if_missing(error, path) click to toggle source
# File lib/toys/errors.rb, line 107
def add_config_path_if_missing(error, path)
  if error.config_path.nil? && error.config_line.nil?
    l = (error.cause.backtrace_locations || []).find { |b| b.absolute_path == path }
    if l
      error.config_path = path
      error.config_line = l.lineno
    end
  end
end
add_fields_if_missing(error, opts) click to toggle source
# File lib/toys/errors.rb, line 101
def add_fields_if_missing(error, opts)
  opts.each do |k, v|
    error.send(:"#{k}=", v) if error.send(k).nil?
  end
end