[ActiveJob] raise DeserializationError when got an error deserializing

This commit is contained in:
Cristian Bica 2014-08-17 23:48:44 +03:00 committed by Abdelkader Boudih
parent 9a34262201
commit 3faa61ede5
4 changed files with 27 additions and 0 deletions

View File

@ -1,4 +1,14 @@
module ActiveJob
class DeserializationError < StandardError
attr_reader :original_exception
def initialize(e)
super ("Error while trying to deserialize arguments: #{e.message}")
@original_exception = e
set_backtrace e.backtrace
end
end
module Arguments
extend self
TYPE_WHITELIST = [ NilClass, Fixnum, Float, String, TrueClass, FalseClass, Bignum ]
@ -36,6 +46,8 @@ module ActiveJob
else
GlobalID::Locator.locate(argument) || argument
end
rescue => e
raise DeserializationError.new(e)
end
def serialize_hash_key(key)

View File

@ -20,4 +20,11 @@ class RescueTest < ActiveSupport::TestCase
job.execute(SecureRandom.uuid, "other")
end
end
test 'rescue from deserialization errors' do
RescueJob.enqueue Person.new(404)
assert_includes JobBuffer.values, 'rescued from DeserializationError'
assert_includes JobBuffer.values, 'DeserializationError original exception was Person::RecordNotFound'
assert_not_includes JobBuffer.values, 'performed beautifully'
end
end

View File

@ -7,6 +7,11 @@ class RescueJob < ActiveJob::Base
retry_now
end
rescue_from(ActiveJob::DeserializationError) do |e|
JobBuffer.add('rescued from DeserializationError')
JobBuffer.add("DeserializationError original exception was #{e.original_exception.class.name}")
end
def perform(person = "david")
case person
when "david"

View File

@ -1,9 +1,12 @@
class Person
class RecordNotFound < StandardError; end
include GlobalID::Identification
attr_reader :id
def self.find(id)
raise RecordNotFound.new("Cannot find person with ID=404") if id.to_i==404
new(id)
end