-
-
Notifications
You must be signed in to change notification settings - Fork 203
/
SentryException.cs
92 lines (78 loc) · 2.88 KB
/
SentryException.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
using Sentry.Extensibility;
using Sentry.Internal.Extensions;
namespace Sentry.Protocol;
/// <summary>
/// Sentry Exception interface.
/// </summary>
/// <see href="https://develop.sentry.dev/sdk/event-payloads/exception"/>
public sealed class SentryException : IJsonSerializable
{
/// <summary>
/// Exception Type.
/// </summary>
public string? Type { get; set; }
/// <summary>
/// The exception value.
/// </summary>
public string? Value { get; set; }
/// <summary>
/// The optional module, or package which the exception type lives in.
/// </summary>
public string? Module { get; set; }
/// <summary>
/// An optional value which refers to a thread in the threads interface.
/// </summary>
/// <seealso href="https://develop.sentry.dev/sdk/event-payloads/threads/"/>
/// <seealso cref="SentryThread"/>
public int ThreadId { get; set; }
/// <summary>
/// Stack trace.
/// </summary>
/// <see href="https://develop.sentry.dev/sdk/event-payloads/stacktrace/"/>
public SentryStackTrace? Stacktrace { get; set; }
/// <summary>
/// An optional mechanism that created this exception.
/// </summary>
/// <see href="https://develop.sentry.dev/sdk/event-payloads/exception/#exception-mechanism"/>
public Mechanism? Mechanism { get; set; }
/// <inheritdoc />
public void WriteTo(Utf8JsonWriter writer, IDiagnosticLogger? logger)
{
writer.WriteStartObject();
writer.WriteStringIfNotWhiteSpace("type", Type);
writer.WriteStringIfNotWhiteSpace("value", Value);
writer.WriteStringIfNotWhiteSpace("module", Module);
writer.WriteNumberIfNotNull("thread_id", ThreadId.NullIfDefault());
writer.WriteSerializableIfNotNull("stacktrace", Stacktrace, logger);
if (Mechanism?.IsDefaultOrEmpty() == false)
{
writer.WriteSerializableIfNotNull("mechanism", Mechanism, logger);
}
writer.WriteEndObject();
}
/// <summary>
/// Parses from JSON.
/// </summary>
public static SentryException FromJson(JsonElement json)
{
var type = json.GetPropertyOrNull("type")?.GetString();
var value = json.GetPropertyOrNull("value")?.GetString();
var module = json.GetPropertyOrNull("module")?.GetString();
var threadId = json.GetPropertyOrNull("thread_id")?.GetInt32() ?? 0;
var stacktrace = json.GetPropertyOrNull("stacktrace")?.Pipe(SentryStackTrace.FromJson);
var mechanism = json.GetPropertyOrNull("mechanism")?.Pipe(Mechanism.FromJson);
if (mechanism?.IsDefaultOrEmpty() == true)
{
mechanism = null;
}
return new SentryException
{
Type = type,
Value = value,
Module = module,
ThreadId = threadId,
Stacktrace = stacktrace,
Mechanism = mechanism
};
}
}