class Reading::AppError

Public Class Methods

new(msg = nil, label: "Error") click to toggle source
Calls superclass method
# File lib/reading/errors.rb, line 11
def initialize(msg = nil, label: "Error")
  super(label + colon_before?(msg) + (msg || ""))
end

Public Instance Methods

handle(source: nil, config:) click to toggle source

source is e.g. the CSV line where an invalid Item comes from.

# File lib/reading/errors.rb, line 16
def handle(source: nil, config:)
  handle = config.fetch(:errors).fetch(:handle_error)
  if source.nil?
    handle.call(self)
  else
    handle.call(styled_with_source(source, config: config))
  end
end

Protected Instance Methods

colon_before?(msg) click to toggle source
# File lib/reading/errors.rb, line 31
def colon_before?(msg)
  msg.nil? ? "" : ": "
end
color() click to toggle source
# File lib/reading/errors.rb, line 27
def color
  :red
end
styled(str, config) click to toggle source
# File lib/reading/errors.rb, line 49
def styled(str, config)
  case config.fetch(:errors).fetch(:style_mode)
  when :terminal
    Colors.send("bright_#{color}").bold(str)
  when :html
    "<rl-error class=\"#{color}\">#{str}</rl-error>"
  end
end
styled_with_source(source, config:) click to toggle source
# File lib/reading/errors.rb, line 35
def styled_with_source(source, config:)
  truncated_source = truncate(source,
                              config.fetch(:errors).fetch(:max_length),
                              padding: message.length)
  self.class.new(truncated_source,
                  label: styled(message, config))
end
truncate(str, max, padding: 0, min: 30) click to toggle source
# File lib/reading/errors.rb, line 43
def truncate(str, max, padding: 0, min: 30)
  end_index = max - padding
  end_index = min if end_index < min
  str.length + padding > max ? "#{str[0...end_index]}..." : str
end