Sha256: d06486f2c64e349a2b4176ce1c4afaa036f4078ee972cab13b5aac2501f07d81
Contents?: true
Size: 1.23 KB
Versions: 5
Compression:
Stored size: 1.23 KB
Contents
# Encoding: utf-8 module Reqless # A helper for formatting failure messages class FailureFormatter Failure = Struct.new(:group, :message) do # allow de-structring assignment def to_ary [group, message] end end def initialize @replacements = { Dir.pwd => '.' } @replacements[ENV['GEM_HOME']] = '<GEM_HOME>' if ENV.key?('GEM_HOME') end def format(job, error, lines_to_remove = caller(2)) group = "#{job.klass_name}:#{error.class}" message = "#{truncated_message(error)}\n\n" + "#{format_failure_backtrace(error.backtrace, lines_to_remove)}" Failure.new(group, message) end private # TODO: pull this out into a config option. MAX_ERROR_MESSAGE_SIZE = 10_000 def truncated_message(error) return error.message if error.message.length <= MAX_ERROR_MESSAGE_SIZE error.message.slice(0, MAX_ERROR_MESSAGE_SIZE) + "\n... (truncated due to length)" end def format_failure_backtrace(error_backtrace, lines_to_remove) (error_backtrace - lines_to_remove).map do |line| @replacements.reduce(line) do |formatted, (original, new)| formatted.sub(original, new) end end.join("\n") end end end
Version data entries
5 entries across 5 versions & 1 rubygems