Sha256: e4b46bc9c37238aefa14529da439f6a9ba4d7ed9bf7d4895345b13476d7631f1

Contents?: true

Size: 1.78 KB

Versions: 21

Compression:

Stored size: 1.78 KB

Contents

require "timber/event"
require "timber/util"

module Timber
  module Events
    # The error event is used to track errors and exceptions.
    #
    # @note This event should be installed automatically through integrations,
    #   such as the {Integrations::ActionDispatch::DebugExceptions} integration.
    class Error < Timber::Event
      MAX_MESSAGE_BYTES = 8192.freeze

      attr_reader :name, :error_message, :backtrace

      def initialize(attributes)
        @name = attributes[:name] || raise(ArgumentError.new(":name is required"))

        @error_message = attributes[:error_message] || raise(ArgumentError.new(":error_message is required"))
        @error_message = @error_message.byteslice(0, MAX_MESSAGE_BYTES)

        backtrace = attributes[:backtrace]
        if !backtrace.nil? && backtrace != []
          @backtrace = backtrace[0..9].collect { |line| parse_backtrace_line(line) }
        end
      end

      def to_hash
        {name: name, message: error_message, backtrace: backtrace}
      end
      alias to_h to_hash

      # Builds a hash representation containing simple objects, suitable for serialization (JSON).
      def as_json(_options = {})
        {:error => to_hash}
      end

      def message
        "#{name} (#{error_message})"
      end

      private
        def parse_backtrace_line(line)
          # using split for performance reasons
          file, line, function_part = line.split(":", 3)

          parsed_line = {file: file}

          if line
            parsed_line[:line] = line.to_i
          end

          if function_part
            _prefix, function_pre = function_part.split("`", 2)
            function = Util::Object.try(function_pre, :chomp, "'")
            parsed_line[:function] = function
          end

          parsed_line
        end
    end
  end
end

Version data entries

21 entries across 21 versions & 1 rubygems

Version Path
timber-2.5.1 lib/timber/events/error.rb
timber-2.5.0 lib/timber/events/error.rb
timber-2.4.0 lib/timber/events/error.rb
timber-2.3.4 lib/timber/events/error.rb
timber-2.3.3 lib/timber/events/error.rb
timber-2.3.2 lib/timber/events/error.rb
timber-2.3.1 lib/timber/events/error.rb
timber-2.3.0 lib/timber/events/error.rb
timber-2.2.3 lib/timber/events/error.rb
timber-2.2.2 lib/timber/events/error.rb
timber-2.2.1 lib/timber/events/error.rb
timber-2.2.0 lib/timber/events/error.rb
timber-2.1.10 lib/timber/events/error.rb
timber-2.1.9 lib/timber/events/error.rb
timber-2.1.8 lib/timber/events/error.rb
timber-2.1.7 lib/timber/events/error.rb
timber-2.1.6 lib/timber/events/error.rb
timber-2.1.5 lib/timber/events/error.rb
timber-2.1.4 lib/timber/events/error.rb
timber-2.1.3 lib/timber/events/error.rb