aboutsummaryrefslogtreecommitdiffstats
path: root/activejob/lib/active_job
diff options
context:
space:
mode:
authorKasper Timm Hansen <kaspth@gmail.com>2015-07-05 13:58:44 +0200
committerKasper Timm Hansen <kaspth@gmail.com>2015-07-05 13:58:44 +0200
commitba400c7e88329ee70fd62bcd060389abb7dcebb2 (patch)
treee07ff2c37f789f68324bfef7be82f7f85d915987 /activejob/lib/active_job
parent798ad25f76bf6c954eb2923adb0452137b8f9925 (diff)
parentfd8122a87859ef191ca849dc6a1141beef0b4cee (diff)
downloadrails-ba400c7e88329ee70fd62bcd060389abb7dcebb2.tar.gz
rails-ba400c7e88329ee70fd62bcd060389abb7dcebb2.tar.bz2
rails-ba400c7e88329ee70fd62bcd060389abb7dcebb2.zip
Merge pull request #19877 from fny/job-global-id-error-improvement
Improve error message when serializing unsaved records for jobs, Fixes #19861
Diffstat (limited to 'activejob/lib/active_job')
-rw-r--r--activejob/lib/active_job/arguments.rb21
1 files changed, 14 insertions, 7 deletions
diff --git a/activejob/lib/active_job/arguments.rb b/activejob/lib/active_job/arguments.rb
index cdb37879b6..8e462bfe5d 100644
--- a/activejob/lib/active_job/arguments.rb
+++ b/activejob/lib/active_job/arguments.rb
@@ -16,13 +16,13 @@ module ActiveJob
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 @@ module ActiveJob
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 @@ module ActiveJob
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