mirror of
https://github.com/mperham/sidekiq.git
synced 2022-11-09 13:52:34 -05:00
Rescue StandardErrors raised from exception's message (#4089)
* Rescue standard errors raised from exception's message * Set a default message value if it raises an error * Update default message value * Add comments for exception_message
This commit is contained in:
parent
3d8147cfaa
commit
23e6a5f756
2 changed files with 32 additions and 3 deletions
|
@ -140,9 +140,7 @@ module Sidekiq
|
|||
queue
|
||||
end
|
||||
|
||||
# App code can stuff all sorts of crazy binary data into the error message
|
||||
# that won't convert to JSON.
|
||||
m = exception.message.to_s[0, 10_000]
|
||||
m = exception_message(exception).dup
|
||||
if m.respond_to?(:scrub!)
|
||||
m.force_encoding("utf-8")
|
||||
m.scrub!
|
||||
|
@ -247,5 +245,17 @@ module Sidekiq
|
|||
exception_caused_by_shutdown?(e.cause, checked_causes)
|
||||
end
|
||||
|
||||
# Extract message from exception.
|
||||
# Set a default if the message raises an error
|
||||
def exception_message(exception)
|
||||
begin
|
||||
# App code can stuff all sorts of crazy binary data into the error message
|
||||
# that won't convert to JSON.
|
||||
exception.message.to_s[0, 10_000]
|
||||
rescue StandardError => e
|
||||
"!!! ERROR MESSAGE THREW AN ERROR !!!"
|
||||
end
|
||||
end
|
||||
|
||||
end
|
||||
end
|
||||
|
|
|
@ -10,6 +10,12 @@ class TestRetry < Minitest::Test
|
|||
include Sidekiq::Worker
|
||||
end
|
||||
|
||||
class BadErrorMessage < StandardError
|
||||
def message
|
||||
raise "Ahhh, this isn't supposed to happen"
|
||||
end
|
||||
end
|
||||
|
||||
before do
|
||||
Sidekiq.redis {|c| c.flushdb }
|
||||
end
|
||||
|
@ -75,6 +81,19 @@ class TestRetry < Minitest::Test
|
|||
assert_equal "kerblammo! <20>", job["error_message"]
|
||||
end
|
||||
|
||||
# In the rare event that an error message raises an error itself,
|
||||
# allow the job to retry. This will likely only happen for custom
|
||||
# error classes that override #message
|
||||
it 'handles error message that raises an error' do
|
||||
assert_raises RuntimeError do
|
||||
handler.local(worker, job, 'default') do
|
||||
raise BadErrorMessage.new
|
||||
end
|
||||
end
|
||||
|
||||
assert_equal 1, Sidekiq::RetrySet.new.size
|
||||
refute_nil job["error_message"]
|
||||
end
|
||||
|
||||
it 'allows a max_retries option in initializer' do
|
||||
max_retries = 7
|
||||
|
|
Loading…
Add table
Reference in a new issue