Uploaded image for project: 'Mongoid'
  1. Mongoid
  2. MONGOID-4863

destroy! callbacks are not displaying the user defined error messages

    • Type: Icon: Improvement Improvement
    • Resolution: Incomplete
    • Priority: Icon: Major - P3 Major - P3
    • None
    • Affects Version/s: 7.0.5
    • Labels:
      None
    • Environment:
      Everyone

      Firstly I would like to congrats you and your team for developing Mongoid. I loved this gem. It is completely revolutionary.
       
      I would like to notify you regarding a specific problem.
       
      When playing with callbacks (https://docs.mongodb.com/mongoid/current/tutorials/mongoid-callbacks/), sometimes we want to abort the action and display an exception with error messages.

      We can do this creating a callback, for example: "before_destroy :example_method" , then create the "example_method" method, add errors to the item via "errors.add" and then call "throw(:abort)".
       
      Then, after that, we can execute the "item.destroy!", method which would raise exceptions and display the error message we just put inside the errors hash.
       
      The problem is: The Mongoid is not displaying the error message exception that we just put inside the errors hash. It is just displaying a general error message, like this one:

      Mongoid::Errors::DocumentNotDestroyed:
      message:
        Group with id BSON::ObjectId('5e7d10b4b65339225219d66b') was not destroyed.
      summary:
        When calling Group#destroy! and a callback halts the destroy callback chain by returning a false value, the deletion will not actually occur.
      resolution:
        Check the before/after destroy callbacks to ensure that the return values are truthy for the chain to continue.
      from /Users/victor/.rvm/gems/ruby-2.6.5@pocket/gems/mongoid-7.0.5/lib/mongoid/persistable/destroyable.rb:31:in `destroy!'

       
      When playing with ActiveRecord, it displays the error message correctly, because it builds the error message based on the errors hash. However, Mongoid doesn't look at the item.errors when building the error message. It just put that general error message.
       
      Could you change this?
       
      Mongoid destroy!

      def destroy!(options = {})
        destroy || raise(Errors::DocumentNotDestroyed.new(id, self.class))
      end

       
      ActiveRecord destroy!

      def destroy!
        destroy || _raise_record_not_destroyed
      end
       
      def _raise_record_not_destroyed
        @_association_destroy_exception ||= nil
        raise @_association_destroy_exception || RecordNotDestroyed.new("Failed to destroy the record", self)
      ensure
        @_association_destroy_exception = nil
      end 

       
      Thank you and best regards!!

            Assignee:
            emily.giurleo@mongodb.com Emily Giurleo (Inactive)
            Reporter:
            rita.avelar.costa@gmail.com Rita Costa
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

              Created:
              Updated:
              Resolved: