提交 ba400c7e 编写于 作者: K Kasper Timm Hansen

Merge pull request #19877 from fny/job-global-id-error-improvement

Improve error message when serializing unsaved records for jobs, Fixes #19861
......@@ -16,13 +16,13 @@ def initialize(e) #:nodoc:
end
end
# Raised when an unsupported argument type is being set as job argument. We
# Raised when an unsupported argument type is set as a 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
# Bignum and objects that can be represented as GlobalIDs (ex: Active Record).
# Raised if you set the key for a Hash something else than a string or
# a symbol. Also raised when trying to serialize an object which can't be
# identified with a Global ID - such as an unpersisted Active Record model.
class SerializationError < ArgumentError; end
module Arguments
extend self
......@@ -59,7 +59,7 @@ def serialize_argument(argument)
when *TYPE_WHITELIST
argument
when GlobalID::Identification
{ GLOBALID_KEY => argument.to_global_id.to_s }
convert_to_global_id_hash(argument)
when Array
argument.map { |arg| serialize_argument(arg) }
when ActiveSupport::HashWithIndifferentAccess
......@@ -147,5 +147,12 @@ def transform_symbol_keys(hash, symbol_keys)
end
end
end
def convert_to_global_id_hash(argument)
{ GLOBALID_KEY => argument.to_global_id.to_s }
rescue URI::GID::MissingModelIdError
raise SerializationError, "Unable to serialize #{argument.class} " \
"without an id. (Maybe you forgot to call save?)"
end
end
end
......@@ -88,6 +88,13 @@ class ArgumentSerializationTest < ActiveSupport::TestCase
assert_equal "Job with argument: 2", JobBuffer.last_value
end
test 'raises a friendly SerializationError for records without ids' do
err = assert_raises ActiveJob::SerializationError do
ActiveJob::Arguments.serialize [Person.new(nil)]
end
assert_match 'Unable to serialize Person without an id.', err.message
end
private
def assert_arguments_unchanged(*args)
assert_arguments_roundtrip args
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册