-
Notifications
You must be signed in to change notification settings - Fork 21.4k
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
AR::RecordNotSaved & RecordNotDestroyed from save!/destroy! should include an error message #17824
AR::RecordNotSaved & RecordNotDestroyed from save!/destroy! should include an error message #17824
Conversation
@@ -139,7 +139,8 @@ def save(*) | |||
# Attributes marked as readonly are silently ignored if the record is | |||
# being updated. | |||
def save!(*) | |||
create_or_update || raise(RecordNotSaved.new(nil, self)) | |||
create_or_update || raise(RecordNotSaved.new("Failed to save the record " \ | |||
"because one of the before callbacks returned false", self)) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Talking about callbacks is confusing here since it can fail to save due to validations. Maybe just Failed to save the record
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If saving fails due to validations, ActiveRecord::RecordInvalid
will be raised. See line 121 in lib/active_record/persistence.rb.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Any word on this? I'm more than happy to change the massage to Failed to save the record. Perhaps one of the before callbacks returned false?
, but I strongly disagree with not mentioning why at all.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Talking about callbacks on the Persistence
module sounds weird, as it should not have any callback concern.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Error messages should always mention why to help understand what went wrong.
- You get
NameError
because its anundefined local variable or method
. - You get
ArgumentError
because ofwrong number of arguments (Y for X)
. - You get
ActiveRecord::RecordInvalid
becauseField can't be blank
. - You get
ActiveRecord::RecordNotSaved
becausebefore callback cancelled execution
.
What could be weird in the above examples? Don't you think the messages below are not weird? What's the point of having a error message?
- You get
ActiveRecord::RecordNotSaved
and saysfailed to save the record
. - You get
ActiveRecord::RecordInvalid
and saysrecord is invalid
. - You get
ArgumentError
and saysargument(s) is incorrect
. - You get
NameError
and sayswrong name
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
At master callbacks halt the execution using throw(:abort)
we need to update the message now.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ok, I'll change it to just Failed to save the record
.
9689daf
to
d33a80d
Compare
updated the message and rebased. Now ready for merging. |
LGTM, @rafaelfranca final thoughts? |
@rafaelfranca any words on this? |
Is there any reason this pull request can not be merged? |
d33a80d
to
ca94307
Compare
I ran into this issue today where the message from error = begin
User.find(id).destroy!
rescue ActiveRecord::RecordNotDestroyed => e
e
end
error.message
# => #<User:0x00000004e82f20> This is still happening on Rails 4.2 and 5.0. I don't think nothing stops from merging this request and it should be merged and backported. cc/ @rafaelfranca |
When `AR::Base.save!` or `AR::Base.destroy!` is called and an exception is raised, the exception doesn't have any error message or has a weird message like `#<FailedBulb:0x0000000907b4b8>`. Give a better message so we can easily understand why it's failing to save/destroy.
ca94307
to
ad5824b
Compare
…-destroyed-to-include-error-msg AR::RecordNotSaved & RecordNotDestroyed from save!/destroy! should include an error message
Backported at c61b17d |
…-destroyed-to-include-error-msg AR::RecordNotSaved & RecordNotDestroyed from save!/destroy! should include an error message
@rafaelfranca Thanks! ❤️ 💚 💛 |
When calling
AR::Base.save!
orAR::Base.destroy!
and raising one of the exceptions, give a meaningful error message so that people can easily understand why it's failing to save/destroy.cc @arthurnn @rafaelfranca