Skip to content

Commit

Permalink
Namespace ExceptionUtils module (#40)
Browse files Browse the repository at this point in the history
  • Loading branch information
frodsan authored Dec 3, 2024
1 parent c033b81 commit 507b9fb
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 35 deletions.
32 changes: 0 additions & 32 deletions lib/sidekiq/exception_utils.rb

This file was deleted.

36 changes: 36 additions & 0 deletions lib/sidekiq/logging/exception_utils.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
# frozen_string_literal: true

module Sidekiq
module Logging
# Utility that allows us to get a hash representation of an exception
module ExceptionUtils
module_function

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

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
end
end
end
6 changes: 3 additions & 3 deletions lib/sidekiq/logging/shared.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# frozen_string_literal: true

require 'sidekiq/exception_utils'
require 'sidekiq/logging/exception_utils'

module Sidekiq
module Logging
Expand Down Expand Up @@ -54,7 +54,7 @@ def log_job_exception(job, started_at, exc)

config = Sidekiq::Logstash.configuration
if config.log_job_exception_with_causes
payload['error'] = ExceptionUtils.get_exception_with_cause_hash(
payload['error'] = Sidekiq::Logging::ExceptionUtils.get_exception_with_cause_hash(
exc, max_depth_left: config.causes_logging_max_depth
)
else
Expand All @@ -66,7 +66,7 @@ def log_job_exception(job, started_at, exc)
payload['error_cause'] = {
'class' => cause.class.to_s,
'message' => cause.message,
'backtrace' => ExceptionUtils.backtrace_for(cause, exc.backtrace)
'backtrace' => Sidekiq::Logging::ExceptionUtils.backtrace_for(cause, exc.backtrace)
}
end
end
Expand Down

0 comments on commit 507b9fb

Please sign in to comment.