Display a Nice Error Popup Window - Extended
Last time I blogged about how to display a nice error message to the user using only one line of declarative code. Today I want to show you how to extend that. Usually when you develop a Website you need to deal with exception either system or application specific exceptions. Pattern & Practices team have already provided us with a great exception handling application block which is part of Enterprise Library. I’m not going to write my own here, but rather integrate the Error Pop window with theirs.
Prerequisite readings:
In general when you get an error while processing HTTP request you either do
- Display error message to the user e.g. credit card number is invalid.
- Display error message to the user & log the exception e.g. failed to update item due to concurrency problem.
- Don’t display error message to the user but log the exception. e.g. internal errors such as failed to send a welcome email.
- Transfer the user to error page, and log the exception. e.g. fatal errors such as failed to complete user transaction.
Of course there are other scenarios, but these are very common. Now lets write the procedure that will take care of the above scenarios. At the end we need only to invoke a single routine with a specific error severity level.
Now lets define the exception severity level based on the definition we did earlier.
Now each severity defines a Policy. These policies implement the contract for each severity type. Obviously, we need to define an interface that will define that contract that must be implemented by each policy. such as
The above interface has one method on it. Which defines the policy execution of which all policies must implement. In the attachment you will find all four (Exception Severity) policies implemented. I will only show MediumPolicyException as example. As we stated above, the medium exception policy is:
** Display error message to the user & log the exception e.g. failed to update item due to concurrency problem.
So we will display a nice error message to the user as pop error message (For more details on this one check Display a Nice Error Popup Message). In Addition to, publish the exception using Microsoft Pattern & Practices Exception Application Block (For more information on how to use The Exception Application Block).
The following is the MediumPolicyException implementation for the IExceptionPolicy interface.
How to use it?
You will find in the attachment a complete Demo + Source Code that show you how use all the scenarios. But here I will show only how to possibly invoke the MediumPolicyException implementation using the Medium Exception severity.ExceptionManager class is responsible for resolving the Policy implementation (Enhancement: Use IOC: Unity framework). The Handle method takes two parameters ExceptionDetail which contains all details needed to display and publish the error. Second parameter, is the severity level.
Demo:
You will find in the attachment a demo application that show how to use the code in this post:
This is the main screen, and as you can see all the four levels are shown.
Now if you click on LowException button, according to our definition
- Display error message to the user e.g. credit card number is invalid.
Now what happen when we click on “HighException” Button. Again based on our definition
- Transfer the user to error page, and log the exception. e.g. fatal errors such as failed to complete user transaction
When we click on the highException. The library will automatically will direct the user to the defined Error page, and publish the exception. Publishing the exception through the exception handling application block. In the demo I have created to listeners FileExceptionPublisher and XMLExceptionPublisher.
After you click the “HighException” button you will see error log in APP_Data/Logs/ ErrorLog.txt with output similar to
General Information
Additonal Info:
ExceptionManager.MachineName: **************
ExceptionManager.TimeStamp: 29/11/2008 17:43:39
ExceptionManager.FullName: Microsoft.ApplicationBlocks.ExceptionManagement, Version=1.0.2928.16878, Culture=neutral, PublicKeyToken=d8e970896b1fa9ac
ExceptionManager.AppDomainName: d70380a-1-128724430245207500
ExceptionManager.ThreadIdentity: *************\nawaf
ExceptionManager.WindowsIdentity: *************\nawaf
Exception Information:
ExceptionManagement.AppException: This is a high Exception
at ErrorPopup.Web._Default.Button_HighException_Click(Object sender, EventArgs e) in C:\Documents and Settings\nawaf\My Documents\Visual Studio 2008\Projects\ErrorModalPopUp2\ErrorPopup.Web\Default.aspx.cs:line 74
Also an email will be sent to the Site admin if it’s enabled. You can check the demo for more details.
Download the source code and the app demo and give it a try.
Hope this helps guys :)