From 18786bee338c40cb5fd00879a2dc8cf4ed091ae8 Mon Sep 17 00:00:00 2001 From: Jonathan Jenne Date: Mon, 26 Apr 2021 16:31:41 +0200 Subject: [PATCH] Common: Improve BaseForms and ErrorHandler --- GUIs.Common/Base/BaseErrorHandler.vb | 20 ++++++----- GUIs.Common/Base/BaseForm.vb | 51 ++++++++++++++++++++++++++++ GUIs.Common/Base/BaseRibbonForm.vb | 15 +++++--- GUIs.Common/Common.vbproj | 3 ++ 4 files changed, 77 insertions(+), 12 deletions(-) create mode 100644 GUIs.Common/Base/BaseForm.vb diff --git a/GUIs.Common/Base/BaseErrorHandler.vb b/GUIs.Common/Base/BaseErrorHandler.vb index 85034d9f..87a06c70 100644 --- a/GUIs.Common/Base/BaseErrorHandler.vb +++ b/GUIs.Common/Base/BaseErrorHandler.vb @@ -14,9 +14,14 @@ Namespace Base _Form = Form End Sub - Public Sub ShowErrorMessage(Exception As Exception) + Public Sub ShowErrorMessage(Exception As Exception, Origin As String) + Dim oMessage = GetMessage(Exception, Origin) + ShowErrorMessage(Exception, Origin, oMessage) + End Sub + + Public Sub ShowErrorMessage(Exception As Exception, Origin As String, Message As String) _Logger.Error(Exception) - MessageBox.Show(GetMessage(Exception), _Form.Text, MessageBoxButtons.OK, MessageBoxIcon.Exclamation) + MessageBox.Show(Message, _Form.Text, MessageBoxButtons.OK, MessageBoxIcon.Error) End Sub Public Sub ShowInfoMessage(Text As String) @@ -24,8 +29,7 @@ Namespace Base MessageBox.Show(Text, _Form.Text, MessageBoxButtons.OK, MessageBoxIcon.Information) End Sub - Private Function GetMessage(Exception As Exception) As String - Dim oCallingClass = LogConfig.GetClassFullName(IncludeMethodNames:=True, Parts:=2) + Private Function GetMessage(Exception As Exception, Origin As String) As String Dim oMessage As String = String.Empty If TypeOf Exception Is SqlClient.SqlException Then @@ -37,13 +41,13 @@ Namespace Base ElseIf TypeOf Exception Is NoNullAllowedException Then oMessage = "Einige benötigte Felder wurde nicht ausgefüllt." Else - oMessage = $"Es ist ein unerwarteter Fehler in {oCallingClass} aufgetreten. Mehr Informationen finden Sie im Log." + oMessage = $"An unhandled exception occurred in {Origin}: " & vbNewLine & + Exception.Message & vbNewLine & vbNewLine & + "More information can be found in the Application Log." End If If _LogConfig.Debug Then - oMessage &= vbNewLine & vbNewLine & "== Debug Informationen ==" - oMessage &= vbNewLine & vbNewLine & "Die ursprüngliche Fehlermeldung lautet:" - oMessage &= vbNewLine & Exception.Message + oMessage &= vbNewLine & vbNewLine & "=== Debug Information ===" oMessage &= vbNewLine & vbNewLine & "Stacktrace:" oMessage &= vbNewLine & Exception.StackTrace End If diff --git a/GUIs.Common/Base/BaseForm.vb b/GUIs.Common/Base/BaseForm.vb new file mode 100644 index 00000000..ed66dec4 --- /dev/null +++ b/GUIs.Common/Base/BaseForm.vb @@ -0,0 +1,51 @@ + +Imports DevExpress.XtraEditors +Imports DigitalData.Modules.Logging + +Namespace Base + Public Class BaseForm + Inherits XtraForm + + Private ReadOnly _Logger As Logger + Private ReadOnly _ErrorHandler As BaseErrorHandler + + Protected ReadOnly Property Logger As Logger + Get + Return _Logger + End Get + End Property + + Public Sub New() + End Sub + + Public Sub New(LogConfig As LogConfig) + ' Get the full name of the inheriting form + ' so the log messages have the right classname + Dim oClassName = [GetType]().FullName + + ' My.LogConfig is undefined in the designer + _Logger = LogConfig.GetLogger(oClassName) + _ErrorHandler = New BaseErrorHandler(LogConfig, _Logger, Me) + + ' When you add something, be careful if it + ' depends on a global var like My.LogConfig + ' you might need to check for its existence with ? + End Sub + + ''' ============== PUBLIC METHODS ============== + Public Sub ShowInfoMessage(Message As String) + _ErrorHandler.ShowInfoMessage(Message) + End Sub + + Public Sub ShowErrorMessage(Exception As Exception) + Dim oCallingClass = LogConfig.GetClassFullName(IncludeMethodNames:=True, Parts:=2) + _ErrorHandler.ShowErrorMessage(Exception, oCallingClass) + End Sub + + Public Sub ShowErrorMessage(ErrorMessage As String) + Dim oCallingClass = LogConfig.GetClassFullName(IncludeMethodNames:=True, Parts:=2) + _ErrorHandler.ShowErrorMessage(New ApplicationException(ErrorMessage), oCallingClass) + End Sub + End Class +End Namespace + diff --git a/GUIs.Common/Base/BaseRibbonForm.vb b/GUIs.Common/Base/BaseRibbonForm.vb index 31498888..67b023c7 100644 --- a/GUIs.Common/Base/BaseRibbonForm.vb +++ b/GUIs.Common/Base/BaseRibbonForm.vb @@ -5,14 +5,16 @@ Imports DigitalData.Modules.Logging Namespace Base ''' ''' This BaseClass is used to provide common functionality like the Logger or ErrorHandler to all Forms - ''' To use it, create a form and change the `Inherits` statement in FormName.Designer.vb to this form, eg.: + ''' + ''' + ''' To use it, create a form and change the `Inherits` statement in FormName.Designer.vb to this form. ''' ''' Partial Class frmExample ''' Inherits BaseRibbonForm ''' ''' ... ''' End Class - ''' + ''' Public Class BaseRibbonForm Inherits RibbonForm @@ -43,13 +45,18 @@ Namespace Base End Sub ''' ============== PUBLIC METHODS ============== + Public Sub ShowInfoMessage(Message As String) + _ErrorHandler.ShowInfoMessage(Message) + End Sub Public Sub ShowErrorMessage(Exception As Exception) - _ErrorHandler.ShowErrorMessage(Exception) + Dim oCallingClass = LogConfig.GetClassFullName(IncludeMethodNames:=True, Parts:=2) + _ErrorHandler.ShowErrorMessage(Exception, oCallingClass) End Sub Public Sub ShowErrorMessage(ErrorMessage As String) - _ErrorHandler.ShowErrorMessage(New ApplicationException(ErrorMessage)) + Dim oCallingClass = LogConfig.GetClassFullName(IncludeMethodNames:=True, Parts:=2) + _ErrorHandler.ShowErrorMessage(New ApplicationException(ErrorMessage), oCallingClass) End Sub End Class End Namespace diff --git a/GUIs.Common/Common.vbproj b/GUIs.Common/Common.vbproj index 33c6ff7d..c0b4b222 100644 --- a/GUIs.Common/Common.vbproj +++ b/GUIs.Common/Common.vbproj @@ -89,6 +89,9 @@ + + Form + Form