Skip to content

Commit a91d846

Browse files
committed
Backtrace unahndle exception stack trace
1 parent c67de20 commit a91d846

File tree

4 files changed

+81
-10
lines changed

4 files changed

+81
-10
lines changed

src/BacktraceClient.cs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,15 @@ public void Send(BacktraceReport report, Action<BacktraceResult> sendCallback =
165165
{
166166
var record = Database?.Add(report, Attributes, MiniDumpType);
167167
//create a JSON payload instance
168-
var data = record?.BacktraceData ?? report.ToBacktraceData(Attributes);
168+
BacktraceData data = null;
169+
try
170+
{
171+
data = record?.BacktraceData ?? report.ToBacktraceData(Attributes);
172+
}
173+
catch (Exception e)
174+
{
175+
Debug.Log(e);
176+
}
169177

170178
//valid user custom events
171179
data = BeforeSend?.Invoke(data) ?? data;

src/Model/BacktraceData.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -79,8 +79,8 @@ public class BacktraceData
7979
[JsonProperty(PropertyName = "classifiers", NullValueHandling = NullValueHandling.Ignore)]
8080
public string[] Classifier;
8181

82-
[JsonProperty(PropertyName = "sourceCode", NullValueHandling = NullValueHandling.Ignore)]
83-
internal Dictionary<string, SourceCodeData.SourceCode> SourceCode;
82+
//[JsonProperty(PropertyName = "sourceCode", NullValueHandling = NullValueHandling.Ignore)]
83+
//internal Dictionary<string, SourceCodeData.SourceCode> SourceCode;
8484

8585
/// <summary>
8686
/// Get a path to report attachments
@@ -163,8 +163,8 @@ private void SetThreadInformations()
163163
_threadData = new ThreadData(Report.DiagnosticStack);
164164
ThreadInformations = _threadData.ThreadInformations;
165165
MainThread = _threadData.MainThread;
166-
var sourceCodeData = new SourceCodeData(Report.DiagnosticStack);
167-
SourceCode = sourceCodeData.data.Any() ? sourceCodeData.data : null;
166+
//var sourceCodeData = new SourceCodeData(Report.DiagnosticStack);
167+
//SourceCode = sourceCodeData.data.Any() ? sourceCodeData.data : null;
168168
}
169169

170170
private void SetAttributes(Dictionary<string, object> clientAttributes)

src/Model/BacktraceStackTrace.cs

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,11 +36,18 @@ private void Initialize()
3636
SetStacktraceInformation(frames, generateExceptionInformation);
3737
if (_exception != null)
3838
{
39-
40-
var exceptionStackTrace = new StackTrace(_exception, true);
39+
if (_exception is BacktraceUnhandledException)
40+
{
41+
var current = _exception as BacktraceUnhandledException;
42+
StackFrames.InsertRange(0,current.StackFrames);
43+
}
44+
else
45+
{
46+
var exceptionStackTrace = new StackTrace(_exception, true);
4147

42-
var exceptionFrames = exceptionStackTrace.GetFrames();
43-
SetStacktraceInformation(exceptionFrames, true);
48+
var exceptionFrames = exceptionStackTrace.GetFrames();
49+
SetStacktraceInformation(exceptionFrames, true);
50+
}
4451
}
4552
}
4653

src/Model/BacktraceUnhandledException.cs

Lines changed: 57 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
using System;
2+
using System.Collections.Generic;
23
using System.Diagnostics;
4+
using System.IO;
35

46
namespace Backtrace.Unity.Model
57
{
@@ -23,15 +25,69 @@ public override string StackTrace
2325
}
2426
}
2527

28+
public List<BacktraceStackFrame> StackFrames { get; private set; } = new List<BacktraceStackFrame>();
29+
2630
public BacktraceUnhandledException(string message, string stacktrace)
2731
{
2832
if (string.IsNullOrEmpty(stacktrace))
2933
{
30-
stacktrace = new StackTrace(true).ToString();
34+
stacktrace = new StackTrace(0, true).ToString();
3135
}
3236
_stacktrace = stacktrace;
3337
_message = message;
38+
ConvertStackFrames();
3439
}
3540

41+
private void ConvertStackFrames()
42+
{
43+
// frame format:
44+
// ClassName.MethodName () (at source/path/file.cs:fileLine)
45+
var frames = _stacktrace.Trim().Split('\n');
46+
foreach (var frameString in frames)
47+
{
48+
using (var outputFile = new StreamWriter(Path.Combine(@"C:\Users\konra\source\BacktraceLogs\", $"errors-unhandled-stacktrace.txt"), true))
49+
{
50+
try
51+
{
52+
outputFile.WriteLine("STACK FRAME : " + frameString.ToString());
53+
int methodNameEndIndex = frameString.IndexOf(')');
54+
//methodname index should be greater than 0 AND '(' should be before ')'
55+
if (methodNameEndIndex < 1 && frameString[methodNameEndIndex - 1] != '(')
56+
{
57+
//invalid stack frame
58+
return;
59+
}
60+
string routingPaths = frameString.Substring(0, methodNameEndIndex - 1);
61+
var routingParams = routingPaths.Trim().Split('.');
62+
string methodPath = string.Empty;
63+
int fileLine = 0;
64+
65+
int sourceInformationStartIndex = frameString.IndexOf('(', methodNameEndIndex + 1);
66+
if (sourceInformationStartIndex > -1)
67+
{
68+
// -1 because we don't want additional ')' in the end of the string
69+
int sourceStringLength = frameString.Length - sourceInformationStartIndex - 1;
70+
string sourceString =
71+
frameString.Substring(sourceInformationStartIndex, sourceStringLength);
72+
var sourceInfo = sourceString.Split(':');
73+
methodPath = sourceInfo[0].Substring(sourceInfo[0].IndexOf(' '));
74+
int.TryParse(sourceInfo[1], out fileLine);
75+
}
76+
StackFrames.Add(new BacktraceStackFrame()
77+
{
78+
FunctionName = routingParams[1],
79+
Library = routingParams[0],
80+
Line = fileLine,
81+
SourceCode = methodPath
82+
});
83+
}
84+
catch(Exception e)
85+
{
86+
outputFile.WriteLine(e.ToString());
87+
}
88+
}
89+
90+
}
91+
}
3692
}
3793
}

0 commit comments

Comments
 (0)