Skip to content

Commit abee430

Browse files
committed
Fix Sidekiq error handler not taking effect
1 parent 70722e4 commit abee430

File tree

2 files changed

+12
-22
lines changed

2 files changed

+12
-22
lines changed

lib/rails_semantic_logger/extensions/sidekiq/sidekiq.rb

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -120,25 +120,25 @@ def call(_exception, ctx)
120120
class Config
121121
remove_const :ERROR_HANDLER
122122

123-
ERROR_HANDLER = lambda { |_ex, ctx, _cfg = Sidekiq.default_configuration|
123+
ERROR_HANDLER = ->(ex, ctx, cfg = Sidekiq.default_configuration) do
124124
unless ctx.empty?
125125
job_hash = ctx[:job] || {}
126126
klass = job_hash["display_class"] || job_hash["wrapped"] || job_hash["class"]
127127
logger = klass ? SemanticLogger[klass] : Sidekiq.logger
128128
ctx[:context] ? logger.warn(ctx[:context], ctx) : logger.warn(ctx)
129129
end
130-
}
130+
end
131131
end
132132
else
133133
# Sidekiq >= 6.5
134-
# TODO: Not taking effect. See test/sidekiq_test.rb
135-
def self.default_error_handler(_exception, ctx)
136-
return if ctx.empty?
137-
138-
job_hash = ctx[:job] || {}
139-
klass = job_hash["display_class"] || job_hash["wrapped"] || job_hash["class"]
140-
logger = klass ? SemanticLogger[klass] : Sidekiq.logger
141-
ctx[:context] ? logger.warn(ctx[:context], ctx) : logger.warn(ctx)
134+
Sidekiq.error_handlers.delete(Sidekiq::DEFAULT_ERROR_HANDLER)
135+
Sidekiq.error_handlers << ->(ex, ctx) do
136+
unless ctx.empty?
137+
job_hash = ctx[:job] || {}
138+
klass = job_hash["display_class"] || job_hash["wrapped"] || job_hash["class"]
139+
logger = klass ? SemanticLogger[klass] : Sidekiq.logger
140+
ctx[:context] ? logger.warn(ctx[:context], ctx) : logger.warn(ctx)
141+
end
142142
end
143143
end
144144

test/sidekiq_test.rb

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -28,18 +28,8 @@ class SidekiqTest < Minitest::Test
2828
end
2929
elsif Sidekiq::VERSION.to_i == 6
3030
let(:processor) do
31-
# TODO: Sidekiq 6.5 default handler is not accepting the patch
32-
handler = lambda { |_ex, ctx|
33-
unless ctx.empty?
34-
job_hash = ctx[:job] || {}
35-
klass = job_hash["display_class"] || job_hash["wrapped"] || job_hash["class"]
36-
logger = klass ? SemanticLogger[klass] : Sidekiq.logger
37-
ctx[:context] ? logger.warn(ctx[:context], ctx) : logger.warn(ctx)
38-
end
39-
}
40-
config = Sidekiq
41-
config[:error_handlers] = [handler]
42-
config[:fetch] = Sidekiq::BasicFetch.new(config)
31+
config = Sidekiq
32+
config[:fetch] = Sidekiq::BasicFetch.new(config)
4333
Sidekiq::Processor.new(config) { |*args| }
4434
end
4535
elsif Sidekiq::VERSION.to_i < 7

0 commit comments

Comments
 (0)