User friendly error message from global event handler

Tom Troughton asked on December 11, 2015 11:02

I have followed the advice found here about throwing an exception in a global event handler in order to display a friendly error message to the user. I'm trying this on DocumentEvents.Update.Before. For the purposes of my testing I'm doing nothing in the event handler except throw an exception. However, the message I'm seeing in the UI is generic: An error occurred when saving data. Please see event log for more details.

If I check the event log then I see my custom exception message in the log item. So what am I doing wrong here?

Recent Answers


Dawid Jachnik answered on December 11, 2015 13:59

Hello,

could you provide your code here, than we can investigate it?

0 votesVote for this answer Mark as a Correct answer

Tom Troughton answered on December 14, 2015 10:18

As I mentioned, the code is very simple test code. Included below. I expected the exception message to be reported to the CMS user as per my linked advice.

public class DocumentEvents : CMSLoaderAttribute
{
    public override void Init()
    {
        DocumentEvents.Update.Before += Example;
    }

    private void Example(object sender, DocumentEventArgs e)
    {
        throw new System.Exception("Test message");
    }
}

Then a separate partial extension of CMSModuleLoader using my attribute

[DocumentEvents]
public partial class CMSModuleLoader
{
}
0 votesVote for this answer Mark as a Correct answer

Jan Šedo answered on May 13, 2016 12:13 (last edited on May 13, 2016 12:14)

The advice you followed will only work depending on the module which has to deal with the exception.

CMSDocumentManager in particular won't show the exception message because it has catch block implemented where some other internal actions take place and amongst others it sets label of error message:

string message = ResHelper.GetString("General.ErrorDuringSave", ResourceCulture);

So if you want to customize the message, look to customize General.ErrorDuringSave resource string. Cute thing you can do is to put HTML link to the Event log so it is easily reachable from the error message itself. In Localization application add key General.ErrorDuringSave and to it's text you can put something like this:

An error occurred. Please see <a href="~/CMSModules/EventLog/EventLog.aspx">Event Log</a> application.

If you change it, don't forget to clear application's cache and restart the application so your changes take effect.

1 votesVote for this answer Mark as a Correct answer

   Please, sign in to be able to submit a new answer.