lib/timber/events/error.rb in timber-2.5.1 vs lib/timber/events/error.rb in timber-2.6.0.pre.beta1
- old
+ new
@@ -6,28 +6,28 @@
# 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
+ BACKTRACE_JSON_MAX_BYTES = 8192.freeze
+ MESSAGE_MAX_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
+ normalizer = Util::AttributeNormalizer.new(attributes)
+ @name = normalizer.fetch!(:name, :string)
+ @error_message = normalizer.fetch!(:error_message, :string, :limit => MESSAGE_MAX_BYTES)
+ @backtrace = normalizer.fetch(:backtrace, :array)
end
def to_hash
- {name: name, message: error_message, backtrace: backtrace}
+ @to_hash ||= Util::NonNilHashBuilder.build do |h|
+ h.add(:name, name)
+ h.add(:message, error_message)
+ h.add(:backtrace_json, backtrace, :json_encode => true, :limit => BACKTRACE_JSON_MAX_BYTES)
+ end
end
alias to_h to_hash
# Builds a hash representation containing simple objects, suitable for serialization (JSON).
def as_json(_options = {})
@@ -35,28 +35,8 @@
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
\ No newline at end of file