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

AR::RecordNotSaved & RecordNotDestroyed from save!/destroy! should include an error message #17824

Conversation

yuki24
Copy link
Contributor

@yuki24 yuki24 commented Nov 28, 2014

When calling AR::Base.save! or AR::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

@@ -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))
Copy link
Member

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?

Copy link
Contributor Author

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.

Copy link
Contributor Author

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.

Copy link
Member

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.

Copy link
Contributor Author

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 an undefined local variable or method.
  • You get ArgumentError because of wrong number of arguments (Y for X).
  • You get ActiveRecord::RecordInvalid because Field can't be blank.
  • You get ActiveRecord::RecordNotSaved because before 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 says failed to save the record.
  • You get ActiveRecord::RecordInvalid and says record is invalid.
  • You get ArgumentError and says argument(s) is incorrect.
  • You get NameError and says wrong name.

Copy link
Member

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.

Copy link
Contributor Author

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.

@yuki24 yuki24 force-pushed the change-record-not-saved-and-not-destroyed-to-include-error-msg branch 2 times, most recently from 9689daf to d33a80d Compare January 6, 2015 13:14
@yuki24
Copy link
Contributor Author

yuki24 commented Jan 6, 2015

updated the message and rebased. Now ready for merging.

@arthurnn
Copy link
Member

LGTM, @rafaelfranca final thoughts?

@yuki24
Copy link
Contributor Author

yuki24 commented Jan 17, 2015

@rafaelfranca any words on this?

@yuki24
Copy link
Contributor Author

yuki24 commented Feb 1, 2015

Is there any reason this pull request can not be merged?

@yuki24 yuki24 force-pushed the change-record-not-saved-and-not-destroyed-to-include-error-msg branch from d33a80d to ca94307 Compare May 2, 2015 01:51
@yuki24
Copy link
Contributor Author

yuki24 commented May 2, 2015

I ran into this issue today where the message from ActiveRecord::RecordNotDestroyed is competely broken.

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.
@yuki24 yuki24 force-pushed the change-record-not-saved-and-not-destroyed-to-include-error-msg branch from ca94307 to ad5824b Compare May 2, 2015 02:01
rafaelfranca added a commit that referenced this pull request May 3, 2015
…-destroyed-to-include-error-msg

AR::RecordNotSaved & RecordNotDestroyed from save!/destroy! should include an error message
@rafaelfranca rafaelfranca merged commit a278668 into rails:master May 3, 2015
@rafaelfranca
Copy link
Member

Backported at c61b17d

rafaelfranca added a commit that referenced this pull request May 3, 2015
…-destroyed-to-include-error-msg

AR::RecordNotSaved & RecordNotDestroyed from save!/destroy! should include an error message
@yuki24
Copy link
Contributor Author

yuki24 commented May 3, 2015

@rafaelfranca Thanks! ❤️ 💚 💛

@yuki24 yuki24 deleted the change-record-not-saved-and-not-destroyed-to-include-error-msg branch May 3, 2015 22:22
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants