The Fortunate.ErrorHandling object utilizes the Fortunate.Applicaton object to display an appropriate error message to a person whether the application is a website or windows forms app. Please see the demonstration code at the bottom of this page.
The object has a ProcessError method which accepts a message to display to the user and the exception thrown. It uses the Fortunate.Application object to determine what environment the application is running in. If the application's environment is Development or LocalHost then the actual error message with all details is immediately displayed. If the environment is set to staging it the message thanks the user for helping to test the application, attempts to get the UserName of the user, and emails the error and UserName (if found) to the email address provided by the developer. If the environment is set to production the user is notified that the developer has been made aware of the error and it will be looked at and fixed as soon as possible.
I like to include an error label and the code for handling an exception right in my master page and make that code available to call from any page within my site.
Master Page:
Private Sub InitializeApplicationObject()
If _ApplicationObject Is Nothing Then
Me._ApplicationObject = New Fortunate.Application
'
'AboutFortunateApplicationObject
'
Me._ApplicationObject.ApplicationName = "About Fortunate Website"
Me._ApplicationObject.ApplicationType = Fortunate.Application.ApplicationTypes.Internet
Me._ApplicationObject.BaseFilePathDevelopment = ""
Me._ApplicationObject.BaseFilePathLocalhost = "C:\Users\Philosopher\Documents\Visual Studio 2005\Projects\AboutFortunate\AboutFortunateWeb"
Me._ApplicationObject.BaseFilePathProduction = ""
Me._ApplicationObject.BaseFilePathStaging = ""
Me._ApplicationObject.BaseUriDevelopment = "http://development.aboutfortunate.com"
Me._ApplicationObject.BaseUriLocalHost = "http://localhost/AboutFortunateWeb"
Me._ApplicationObject.BaseUriProduction = "http://www.aboutfortunate.com"
Me._ApplicationObject.BaseUriStaging = "http://staging.aboutfortunate.com"
End If
End Sub
Public Sub ProcessException(ByVal messageForUser As String, ByVal ex As Exception)
Call InitializeApplicationObject()
Dim ErrorObject As New Fortunate.ErrorHandling(_ApplicationObject)
With ErrorObject
.EmailServer = "localhost"
.ErrorEmailAddressDevelopment = Nothing
.ErrorEmailAddressProduction = "error@aboutfortunate.com"
.ErrorEmailAddressStaging = "error@aboutfortunate.com"
.UserDomain = "aboutfortunate.com"
.UseWebService = False
End With
Me.ErrorLabel.Text = ErrorObject.ProcessError(messageForUser, ex)
Me.ErrorLabel.Visible = True
End Sub
In order to be able to use the ProcessException Subroutine from a child page we have to type the master page.
<%@ Page Language="VB" MasterPageFile="~/MasterPage.master" EnableSessionState="True" AutoEventWireup="false" CodeFile="Default.aspx.vb" Inherits="_Default" title="Default" %>
<%@ MasterType VirtualPath="~/masterPage.master" %>
To catch all exceptions I wrap all my code within subroutines and functions with a try/catch and throw any exception back to the event handler that called the code. Within any event handler's method I also wrap all the code in a try/catch but I call the ProcessException subroutine within the catch.
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs)
Try
Call SetTitleAndHeader()
Catch ex As Exception
Master.ProcessException("There was an error loading the default page:", ex)
End Try
End Sub
Private Sub SetTitleAndHeader()
Try
'---Get Title and Header from database
Catch ex As Exception
Throw
End Try
EndSub
To see the various error messages displayed choose the environment and an error to create from the drop down lists below. The error message seen when the environment is set to Development or LocalHost is what is emailed to the developer when the environment is set to Production or Staging. The Message to Display is only displayed to the user when the environment is set to Production or Staging.
|