Monorepo/GUIs.Common/Base/BaseMessageBox.vb
Developer02 Digital Data 1047f6152a work on resultlists
2019-10-16 17:09:40 +02:00

73 lines
2.4 KiB
VB.net

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