Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improved error reporting when deserializing a non-existent record #685

Closed
wants to merge 2 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 8 additions & 7 deletions lib/delayed/psych_ext.rb
Original file line number Diff line number Diff line change
Expand Up @@ -43,16 +43,17 @@ def visit_Psych_Nodes_Mapping_with_class(object) # rubocop:disable CyclomaticCom
id = payload['attributes'][klass.primary_key]
begin
klass.unscoped.find(id)
rescue ActiveRecord::RecordNotFound
raise Delayed::DeserializationError
rescue ActiveRecord::RecordNotFound => error
raise(Delayed::DeserializationError.new("ActiveRecord::RecordNotFound, class: #{klass}, primary key: #{id} (#{error.message})"))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The second argument for raise is the message. Use that instead of the .new syntax.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I did (in the initial commit), but rubocop flagged it.
It expressly suggested the raise(Error.new("msg")) syntax, so I updated the PR.

I see that it's also used elsewhere in the project.

The other warnings from rubocop are not caused by changes introduced in this PR.

end
when /^!ruby\/Mongoid:(.+)$/
klass = resolve_class(Regexp.last_match[1])
payload = Hash[*object.children.collect { |c| accept c }]
id = payload['attributes']['_id']
begin
klass.find(payload['attributes']['_id'])
rescue Mongoid::Errors::DocumentNotFound
raise Delayed::DeserializationError
klass.find(id)
rescue Mongoid::Errors::DocumentNotFound => error
raise(Delayed::DeserializationError.new("Mongoid::Errors::DocumentNotFound, class: #{klass}, primary key: #{id} (#{error.message})"))
end
when /^!ruby\/DataMapper:(.+)$/
klass = resolve_class(Regexp.last_match[1])
Expand All @@ -61,8 +62,8 @@ def visit_Psych_Nodes_Mapping_with_class(object) # rubocop:disable CyclomaticCom
primary_keys = klass.properties.select { |p| p.key? }
key_names = primary_keys.collect { |p| p.name.to_s }
klass.get!(*key_names.collect { |k| payload['attributes'][k] })
rescue DataMapper::ObjectNotFoundError
raise Delayed::DeserializationError
rescue DataMapper::ObjectNotFoundError => error
raise(Delayed::DeserializationError.new("DataMapper::ObjectNotFoundError, class: #{klass} (#{error.message})"))
end
else
visit_Psych_Nodes_Mapping_without_class(object)
Expand Down