mirror of
https://github.com/rails/rails.git
synced 2022-11-09 12:12:34 -05:00
Merge pull request #16671 from cristianbica/aj-raise-serialization-error
Raise ActiveJob::SerializationError when cannot serialize job arguments
This commit is contained in:
commit
1f1cc0faee
2 changed files with 16 additions and 5 deletions
|
@ -1,4 +1,7 @@
|
|||
module ActiveJob
|
||||
# Raised when an exception is raised during job arguments deserialization.
|
||||
#
|
||||
# Wraps the original exception raised as +original_exception+.
|
||||
class DeserializationError < StandardError
|
||||
attr_reader :original_exception
|
||||
|
||||
|
@ -9,6 +12,14 @@ module ActiveJob
|
|||
end
|
||||
end
|
||||
|
||||
# Raised when an unsupporter argument type is being set as job argument. We
|
||||
# currently support NilClass, Fixnum, Float, String, TrueClass, FalseClass,
|
||||
# Bignum and object that can be represented as GlobalIDs (ex: Active Record).
|
||||
# Also raised if you set the key for a Hash something else than a string or
|
||||
# a symbol.
|
||||
class SerializationError < ArgumentError
|
||||
end
|
||||
|
||||
module Arguments
|
||||
extend self
|
||||
TYPE_WHITELIST = [ NilClass, Fixnum, Float, String, TrueClass, FalseClass, Bignum ]
|
||||
|
@ -33,7 +44,7 @@ module ActiveJob
|
|||
when Hash
|
||||
Hash[ argument.map { |key, value| [ serialize_hash_key(key), serialize_argument(value) ] } ]
|
||||
else
|
||||
raise "Unsupported argument type: #{argument.class.name}"
|
||||
raise SerializationError.new("Unsupported argument type: #{argument.class.name}")
|
||||
end
|
||||
end
|
||||
|
||||
|
@ -55,7 +66,7 @@ module ActiveJob
|
|||
when String, Symbol
|
||||
key.to_s
|
||||
else
|
||||
raise "Unsupported hash key type: #{key.class.name}"
|
||||
raise SerializationError.new("Unsupported hash key type: #{key.class.name}")
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
@ -19,7 +19,7 @@ class ParameterSerializationTest < ActiveSupport::TestCase
|
|||
assert_equal [ [ 1 ] ], ActiveJob::Arguments.serialize([ [ 1 ] ])
|
||||
assert_equal [ 1_000_000_000_000_000_000_000 ], ActiveJob::Arguments.serialize([ 1_000_000_000_000_000_000_000 ])
|
||||
|
||||
err = assert_raises RuntimeError do
|
||||
err = assert_raises ActiveJob::SerializationError do
|
||||
ActiveJob::Arguments.serialize([ 1, self ])
|
||||
end
|
||||
assert_equal "Unsupported argument type: #{self.class.name}", err.message
|
||||
|
@ -31,14 +31,14 @@ class ParameterSerializationTest < ActiveSupport::TestCase
|
|||
end
|
||||
|
||||
test 'should dive deep into arrays or hashes and raise exception on complex objects' do
|
||||
err = assert_raises RuntimeError do
|
||||
err = assert_raises ActiveJob::SerializationError do
|
||||
ActiveJob::Arguments.serialize([ 1, [self] ])
|
||||
end
|
||||
assert_equal "Unsupported argument type: #{self.class.name}", err.message
|
||||
end
|
||||
|
||||
test 'shoud dive deep into hashes and allow raise exception on not string/symbol keys' do
|
||||
err = assert_raises RuntimeError do
|
||||
err = assert_raises ActiveJob::SerializationError do
|
||||
ActiveJob::Arguments.serialize([ [ { 1 => 2 } ] ])
|
||||
end
|
||||
assert_equal "Unsupported hash key type: Fixnum", err.message
|
||||
|
|
Loading…
Reference in a new issue