Pronunciation / adjective
Bringing something good and unforseen.
    Skip Navigation Links > Component Library > Error Handling Object
   

The Fortunate.ErrorHandling object automatically checks what mode an application is running in (Development, Localhost, Production, or Staging) and chooses the proper error message to show. The Fortunate.ErrorHandling object then emails the details the exception to a given email address.



 

Fortunate.ErrorHandling

Download the Fortunate Library Here:

Fortunate Library 32bit - Stand-Alone Install

Fortunate Library 64bit - Stand-Alone Install

Fortunate Library Solution - Source Code

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:
  1. Private Sub InitializeApplicationObject()
  2. If _ApplicationObject Is Nothing Then
  3. Me._ApplicationObject = New Fortunate.Application
  4. '
  5. 'AboutFortunateApplicationObject
  6. '
  7. Me._ApplicationObject.ApplicationName = "About Fortunate Website"
  8. Me._ApplicationObject.ApplicationType = Fortunate.Application.ApplicationTypes.Internet
  9. Me._ApplicationObject.BaseFilePathDevelopment = ""
  10. Me._ApplicationObject.BaseFilePathLocalhost = "C:\Users\Philosopher\Documents\Visual Studio 2005\Projects\AboutFortunate\AboutFortunateWeb"
  11. Me._ApplicationObject.BaseFilePathProduction = ""
  12. Me._ApplicationObject.BaseFilePathStaging = ""
  13. Me._ApplicationObject.BaseUriDevelopment = "http://development.aboutfortunate.com"
  14. Me._ApplicationObject.BaseUriLocalHost = "http://localhost/AboutFortunateWeb"
  15. Me._ApplicationObject.BaseUriProduction = "http://www.aboutfortunate.com"
  16. Me._ApplicationObject.BaseUriStaging = "http://staging.aboutfortunate.com"
  17. End If
  18. End Sub
  19. Public Sub ProcessException(ByVal messageForUser As String, ByVal ex As Exception)
  20. Call InitializeApplicationObject()
  21. Dim ErrorObject As New Fortunate.ErrorHandling(_ApplicationObject)
  22. With ErrorObject
  23. .EmailServer = "localhost"
  24. .ErrorEmailAddressDevelopment = Nothing
  25. .ErrorEmailAddressProduction = "error@aboutfortunate.com"
  26. .ErrorEmailAddressStaging = "error@aboutfortunate.com"
  27. .UserDomain = "aboutfortunate.com"
  28. .UseWebService = False
  29. End With
  30. Me.ErrorLabel.Text = ErrorObject.ProcessError(messageForUser, ex)
  31. Me.ErrorLabel.Visible = True
  32. End Sub

In order to be able to use the ProcessException Subroutine from a child page we have to type the master page.
  1. <%@ Page Language="VB" MasterPageFile="~/MasterPage.master" EnableSessionState="True" AutoEventWireup="false" CodeFile="Default.aspx.vb" Inherits="_Default" title="Default" %>
  2. <%@ 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.
  1. Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs)
  2. Try
  3. Call SetTitleAndHeader()
  4. Catch ex As Exception
  5. Master.ProcessException("There was an error loading the default page:", ex)
  6. End Try
  7. End Sub
  8. Private Sub SetTitleAndHeader()
  9. Try
  10. '---Get Title and Header from database
  11. Catch ex As Exception
  12. Throw
  13. End Try
  14. 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.

Message to Display:
Application Environment:
Error:
Microsoft Certified Professional   © 2017 Fortunate.  All rights reserved.
contact: justin@aboutfortunate.com