migrate baseerrorhandler from clientsuite
This commit is contained in:
103
GUIs.Common/Base/BaseErrorHandler.vb
Normal file
103
GUIs.Common/Base/BaseErrorHandler.vb
Normal file
@@ -0,0 +1,103 @@
|
||||
Imports System.Reflection
|
||||
Imports System.Windows.Forms
|
||||
Imports DigitalData.Modules.Logging
|
||||
|
||||
Namespace Base
|
||||
Public Class BaseErrorHandler
|
||||
Private _LogConfig As LogConfig
|
||||
Private _Logger As Logger
|
||||
Private _Form As Form
|
||||
|
||||
Private Const UNKNOWN_METHOD = "Unknown Method"
|
||||
Private Const UNKNOWN_FORM = "Unknown Form"
|
||||
|
||||
Public Sub New(LogConfig As LogConfig, Logger As Logger, Form As Form)
|
||||
_LogConfig = LogConfig
|
||||
_Logger = Logger
|
||||
_Form = Form
|
||||
End Sub
|
||||
|
||||
Public Sub ShowErrorMessage(Exception As Exception)
|
||||
_Logger.Error(Exception)
|
||||
MessageBox.Show(GetMessage(Exception), _Form.Text, MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
|
||||
End Sub
|
||||
|
||||
Public Sub ShowInfoMessage(Text As String)
|
||||
_Logger.Info(Text)
|
||||
MessageBox.Show(Text, _Form.Text, MessageBoxButtons.OK, MessageBoxIcon.Information)
|
||||
End Sub
|
||||
|
||||
Private Function GetMessage(Exception As Exception) As String
|
||||
Dim oTargetSite = Exception.TargetSite
|
||||
Dim oMethodName = GetMethodName(Exception)
|
||||
Dim oFormName = GetFormName(Exception)
|
||||
Dim oMessage As String = String.Empty
|
||||
|
||||
If TypeOf Exception Is SqlClient.SqlException Then
|
||||
Dim oNumber As Integer = DirectCast(Exception, SqlClient.SqlException).Number
|
||||
Select Case oNumber
|
||||
Case 2627
|
||||
oMessage = "Es wurde ein Doppelter Wert in eine Spalte eingetragen, die einzigartig sein muss."
|
||||
End Select
|
||||
ElseIf TypeOf Exception Is NoNullAllowedException Then
|
||||
oMessage = "Einige benötigte Felder wurde nicht ausgefüllt."
|
||||
Else
|
||||
oMessage = $"Es ist ein unerwarteter Fehler in {oFormName}/{oMethodName} aufgetreten. Mehr Informationen finden Sie im 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 & "Stacktrace:"
|
||||
oMessage &= vbNewLine & Exception.StackTrace
|
||||
End If
|
||||
|
||||
Return oMessage
|
||||
End Function
|
||||
|
||||
'Private Function GetMessage(Exception As Exception) As String
|
||||
' Dim oTargetSite = Exception.TargetSite
|
||||
' Dim oMethodName = GetMethodName(Exception)
|
||||
' Dim oFormName = GetFormName(Exception)
|
||||
' Dim oMessage As String = String.Empty
|
||||
|
||||
' oMessage &= $"Form: {oFormName}{vbNewLine}"
|
||||
' oMessage &= $"Method: {oMethodName}{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
|
||||
'End Function
|
||||
|
||||
Private Function GetMethodName(Exception As Exception) As String
|
||||
Dim oMethodName = Exception.TargetSite?.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?.Name
|
||||
|
||||
If oFormName Is Nothing Then
|
||||
Return UNKNOWN_FORM
|
||||
Else
|
||||
Return oFormName
|
||||
End If
|
||||
End Function
|
||||
End Class
|
||||
|
||||
End Namespace
|
||||
@@ -1,72 +0,0 @@
|
||||
Imports System.Reflection
|
||||
Imports System.Windows.Forms
|
||||
Imports DigitalData.Modules.Logging
|
||||
|
||||
Namespace Base
|
||||
Public Class BaseErrorHandler
|
||||
Private _Logger As Logger
|
||||
Private _Form As Form
|
||||
|
||||
Private Const UNKNOWN_METHOD = "Unknown Method"
|
||||
Private Const UNKNOWN_FORM = "Unknown Form"
|
||||
|
||||
Public Sub New(Logger As Logger, Form As Form)
|
||||
_Logger = Logger
|
||||
_Form = Form
|
||||
End Sub
|
||||
|
||||
Public Sub ShowErrorMessage(Exception As Exception)
|
||||
_Logger.Error(Exception)
|
||||
MessageBox.Show(GetMessage(Exception), _Form.Text, MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
|
||||
End Sub
|
||||
|
||||
Public Sub ShowInfoMessage(Text As String)
|
||||
_Logger.Info(Text)
|
||||
MessageBox.Show(Text, _Form.Text, MessageBoxButtons.OK, MessageBoxIcon.Information)
|
||||
End Sub
|
||||
|
||||
Private Function GetMessage(Exception As Exception) As String
|
||||
Dim oTargetSite = Exception.TargetSite
|
||||
Dim oMethodName = GetMethodName(Exception)
|
||||
Dim oFormName = GetFormName(Exception)
|
||||
Dim oMessage As String = String.Empty
|
||||
|
||||
oMessage &= $"Form: {oFormName}{vbNewLine}"
|
||||
oMessage &= $"Method: {oMethodName}{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
|
||||
End Function
|
||||
|
||||
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
|
||||
|
||||
End Namespace
|
||||
@@ -12,8 +12,6 @@ Namespace Base
|
||||
'''
|
||||
''' ...
|
||||
''' End Class
|
||||
'''
|
||||
''' Only BaseRibbonForms can have panels attached to it!
|
||||
''' </summary>
|
||||
Public Class BaseRibbonForm
|
||||
Inherits RibbonForm
|
||||
@@ -27,57 +25,31 @@ Namespace Base
|
||||
End Get
|
||||
End Property
|
||||
|
||||
''' <summary>
|
||||
''' Sets or gets the ribbon Page that will be shown when the window is visible
|
||||
''' </summary>
|
||||
''' <returns></returns>
|
||||
Public Property DefaultRibbonPage As RibbonPage
|
||||
|
||||
Protected Overrides Sub OnLoad(e As EventArgs)
|
||||
MyBase.OnLoad(e)
|
||||
|
||||
If DefaultRibbonPage IsNot Nothing Then
|
||||
Ribbon.SelectPage(DefaultRibbonPage)
|
||||
End If
|
||||
Public Sub New()
|
||||
End Sub
|
||||
|
||||
Protected Overrides Sub OnVisibleChanged(e As EventArgs)
|
||||
MyBase.OnVisibleChanged(e)
|
||||
|
||||
If Visible And DefaultRibbonPage IsNot Nothing Then
|
||||
Ribbon.SelectPage(DefaultRibbonPage)
|
||||
End If
|
||||
End Sub
|
||||
|
||||
Protected Overrides Sub OnActivated(e As EventArgs)
|
||||
MyBase.OnVisibleChanged(e)
|
||||
|
||||
If Visible And DefaultRibbonPage IsNot Nothing Then
|
||||
Ribbon.SelectPage(DefaultRibbonPage)
|
||||
End If
|
||||
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
|
||||
' My.LogConfig is undefined in the designer
|
||||
_Logger = LogConfig?.GetLogger(oClassName)
|
||||
_ErrorHandler = New BaseErrorHandler(_Logger, Me)
|
||||
_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 ShowErrorMessage(Exception As Exception)
|
||||
_ErrorHandler.ShowErrorMessage(Exception)
|
||||
End Sub
|
||||
|
||||
Public Sub ShowErrorMessage(ErrorMessage As String)
|
||||
_ErrorHandler.ShowErrorMessage(New Exception(ErrorMessage))
|
||||
_ErrorHandler.ShowErrorMessage(New ApplicationException(ErrorMessage))
|
||||
End Sub
|
||||
End Class
|
||||
End Namespace
|
||||
|
||||
Reference in New Issue
Block a user