Module: Sidekiq::Logging::ExceptionUtils

Defined in:
lib/sidekiq/logging/exception_utils.rb

Overview

Utility that allows us to get a hash representation of an exception

Class Method Summary collapse

Class Method Details

.backtrace_for(exception, parent_backtrace = nil) ⇒ Object



24
25
26
27
28
29
30
31
32
33
# File 'lib/sidekiq/logging/exception_utils.rb', line 24

def backtrace_for(exception, parent_backtrace = nil)
  backtrace = exception.backtrace || []
  if parent_backtrace
    common_lines = backtrace.reverse.zip(parent_backtrace.reverse).take_while { |a, b| a == b }

    backtrace = backtrace[0...-common_lines.length] if common_lines.any?
  end

  backtrace
end

.get_exception_with_cause_hash(exc, parent_backtrace = nil, max_depth_left: 1) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/sidekiq/logging/exception_utils.rb', line 9

def get_exception_with_cause_hash(exc, parent_backtrace = nil, max_depth_left: 1)
  error_hash = {
    'class' => exc.class.to_s,
    'message' => exc.message,
    'backtrace' => backtrace_for(exc, parent_backtrace)
  }

  if (cause = exc.cause) && max_depth_left.positive?
    # Pass the current backtrace as the parent_backtrace to the cause to shorten cause's backtrace list
    error_hash['cause'] = get_exception_with_cause_hash(cause, exc.backtrace, max_depth_left: max_depth_left - 1)
  end

  error_hash
end