From 311516fc9f4ba4ac3137ccb99de30f49edef8ac9 Mon Sep 17 00:00:00 2001 From: Jonathan Jenne Date: Tue, 5 Mar 2019 14:47:10 +0100 Subject: [PATCH] Improve Error Handler --- EDMI_ClientSuite/ClassErrorHandler.vb | 42 +++++++++++++++++++++++---- 1 file changed, 37 insertions(+), 5 deletions(-) diff --git a/EDMI_ClientSuite/ClassErrorHandler.vb b/EDMI_ClientSuite/ClassErrorHandler.vb index 8f26dbcd..1a8dc024 100644 --- a/EDMI_ClientSuite/ClassErrorHandler.vb +++ b/EDMI_ClientSuite/ClassErrorHandler.vb @@ -1,22 +1,34 @@ -Imports DigitalData.Modules.Logging +Imports System.Reflection +Imports DigitalData.Modules.Logging Public Class ClassErrorHandler Private _Logger As Logger + Private Const UNKNOWN_METHOD = "Unknown Method" + Private Const UNKNOWN_FORM = "Unknown Form" + Public Sub New(Logger As Logger) _Logger = Logger End Sub Private Function GetMessage(Exception As Exception) Dim oTargetSite = Exception.TargetSite - Dim oMethodName = oTargetSite.ReflectedType.Name - Dim oFormName = oTargetSite.ReflectedType.ReflectedType.Name + Dim oMethodName = GetMethodName(Exception) + Dim oFormName = GetFormName(Exception) Dim oMessage As String = String.Empty oMessage &= $"Form: {oFormName}{vbNewLine}" oMessage &= $"Method: {oMethodName}{vbNewLine}" - oMessage &= $"{Exception.Message}{vbNewLine}" - oMessage &= $"{Exception.StackTrace}{vbNewLine}{vbNewLine}" + + If Not String.IsNullOrEmpty(Exception.StackTrace) Then + oMessage &= $"Message: {Exception.Message}{vbNewLine}{vbNewLine}" + End If + + If Not String.IsNullOrEmpty(Exception.StackTrace) Then + oMessage &= $"Stacktrace: {Exception.StackTrace}{vbNewLine}" + End If + + oMessage &= $"{vbNewLine}" oMessage &= $"Please report this error to error@digitaldata.works" Return oMessage @@ -26,4 +38,24 @@ Public Class ClassErrorHandler _Logger.Error(Exception) MsgBox(GetMessage(Exception), MsgBoxStyle.Critical, "Unexpected Error") End Sub + + Private Function GetMethodName(Exception As Exception) As String + Dim oMethodName = Exception.TargetSite?.ReflectedType?.Name + + If oMethodName Is Nothing Then + Return UNKNOWN_METHOD + Else + Return oMethodName + End If + End Function + + Private Function GetFormName(Exception As Exception) As String + Dim oFormName = Exception.TargetSite?.ReflectedType?.ReflectedType?.Name + + If oFormName Is Nothing Then + Return UNKNOWN_FORM + Else + Return oFormName + End If + End Function End Class