microsoft-visualbasic-runtime/ApplicationServices/Debugger/Exception/ExceptionData.vb

121 lines
4.1 KiB
VB.net
Raw Normal View History

2019-07-29 02:05:05 +08:00
#Region "Microsoft.VisualBasic::626d2b64093e22f3527641427fdc424e, Microsoft.VisualBasic.Core\ApplicationServices\Debugger\Exception\ExceptionData.vb"
' Author:
'
' asuka (amethyst.asuka@gcmodeller.org)
' xie (genetics@smrucc.org)
' xieguigang (xie.guigang@live.com)
'
' Copyright (c) 2018 GPL3 Licensed
'
'
' GNU GENERAL PUBLIC LICENSE (GPL3)
'
'
' This program is free software: you can redistribute it and/or modify
' it under the terms of the GNU General Public License as published by
' the Free Software Foundation, either version 3 of the License, or
' (at your option) any later version.
'
' This program is distributed in the hope that it will be useful,
' but WITHOUT ANY WARRANTY; without even the implied warranty of
' MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
' GNU General Public License for more details.
'
' You should have received a copy of the GNU General Public License
' along with this program. If not, see <http://www.gnu.org/licenses/>.
' /********************************************************************************/
' Summaries:
' Class ExceptionData
'
' Properties: Message, StackTrace, TypeFullName
'
' Function: CreateFromObject, CreateInstance, Exception, GetCurrentStackTrace, getMessages
' ParseStackTrace, ToString
'
'
' /********************************************************************************/
2018-08-02 20:14:48 +08:00
#End Region
2019-07-24 01:37:01 +08:00
Imports Microsoft.VisualBasic.Text
2018-08-02 20:14:48 +08:00
Namespace ApplicationServices.Debugging.Diagnostics
Public Class ExceptionData
Public Property TypeFullName As String
Public Property Message As String()
Public Property StackTrace As StackFrame()
2019-07-24 01:37:01 +08:00
''' <summary>
''' Create exception object for throw exception expression
''' </summary>
''' <returns></returns>
Public Function Exception() As Exception
Return New Exception(Message.Select(Function(msg, i) $"{i}. {msg}").JoinBy(ASCII.LF)) With {
.Source = StackTrace.JoinBy(ASCII.LF)
}
End Function
2018-08-02 20:14:48 +08:00
Public Overrides Function ToString() As String
Return TypeFullName
End Function
2019-07-24 01:37:01 +08:00
Private Shared Iterator Function getMessages(ex As Exception) As IEnumerable(Of String)
Do While True
Yield ex.Message
If Not ex.InnerException Is Nothing Then
ex = ex.InnerException
Else
Exit Do
End If
Loop
End Function
Public Shared Function CreateFromObject(ex As Exception) As ExceptionData
Return New ExceptionData With {
.Message = getMessages(ex).ToArray,
.TypeFullName = ex.GetType.FullName,
.StackTrace = ParseStackTrace(ex.StackTrace)
}
End Function
2018-08-02 20:14:48 +08:00
Public Shared Function CreateInstance(messages$(), stackTrace$(), type$) As ExceptionData
Return New ExceptionData With {
.TypeFullName = type,
.Message = messages,
.StackTrace = stackTrace _
.Select(AddressOf StackFrame.Parser) _
.ToArray
}
End Function
2019-07-24 01:37:01 +08:00
Public Shared Function ParseStackTrace(stackTrace As String) As StackFrame()
Return stackTrace _
2018-08-02 20:14:48 +08:00
.LineTokens _
.Where(Function(s) Not s.StringEmpty) _
.Skip(3) _
.Select(Function(s)
s = Mid(s, 6).Trim
Return StackFrame.Parser(s)
End Function) _
.ToArray
End Function
''' <summary>
2019-07-24 01:37:01 +08:00
''' Parsing <see cref="Environment.StackTrace"/>, gets current stack trace information.
2018-08-02 20:14:48 +08:00
''' </summary>
''' <returns></returns>
2019-07-24 01:37:01 +08:00
Public Shared Function GetCurrentStackTrace() As StackFrame()
Return ParseStackTrace(Environment.StackTrace)
2018-08-02 20:14:48 +08:00
End Function
End Class
End Namespace