Sha256: 7d58c600163cdbd8b1d3b815582b5b378499e8c1f04212bf5e5f2124d47f0de7

Contents?: true

Size: 1.23 KB

Versions: 8

Compression:

Stored size: 1.23 KB

Contents

# Encoding: utf-8

module Qless
  # 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

8 entries across 8 versions & 1 rubygems

Version Path
qless-0.12.0 lib/qless/failure_formatter.rb
qless-0.11.0 lib/qless/failure_formatter.rb
qless-0.10.5 lib/qless/failure_formatter.rb
qless-0.10.4 lib/qless/failure_formatter.rb
qless-0.10.3 lib/qless/failure_formatter.rb
qless-0.10.2 lib/qless/failure_formatter.rb
qless-0.10.1 lib/qless/failure_formatter.rb
qless-0.10.0 lib/qless/failure_formatter.rb