Project

General

Profile

How to raise an error from controller_issues_edit_before_save hook?

Added by Albrecht Dreß about 1 year ago

I wrote a plugin which shall in the controller_issues_edit_before_save hook apply some modifications to the issue depending upon its properties, and perform sanity checks on the user's input which may raise an exception. The exception shall be displayed like other errors (e.g. XXX cannot be blank etc.) in the red box in the UI, blocking the issue changes from being saved.

The following stripped down code works fine, but I didn't find a documentation how in the case of an error the message can be displayed in the UI:

module MyExtension
  class MyHooks < Redmine::Hook::ViewListener
    def controller_issues_edit_before_save(context = {})
      issue = context[:issue]
      begin
        # the following function is rather complex, and may raise MyExtension::Errors::MyError...
        issue.my_modifications_an_checks
      rescue MyExtension::Errors::MyError => e
        # ...which is caught properly here...
        Rails.logger.info "# my issue sanity check failed with #{e.message}" 
        # ...but how can I display the error in the UI and cancel saving the issue at this point?
      end
    end
  end
end

Any insight will be highly appreciated!

Thanks in advance, Albrecht.


Replies (2)

RE: How to raise an error from controller_issues_edit_before_save hook? - Added by jannet barb about 1 year ago

Reach out to the administration panel for raising the error.

RE: How to raise an error from controller_issues_edit_before_save hook? - Added by Albrecht Dreß about 1 year ago

Thanks for your reply, but I must admit that I don't understand what reach out to the administration panel means… Can you please point me to an example or give more details how the code should look like in the rescue block above?

And maybe my description above was not precise: in the hook, whenever a MyError is raised, the issue shall not be saved, but an error message similar to

(of course containing e.message) shall be displayed above the edit form.

Thanks again, Albrecht.

    (1-2/2)