6
6
using System . Threading . Tasks ;
7
7
8
8
using DisCatSharp . Entities ;
9
+ using DisCatSharp . Exceptions ;
9
10
10
11
using Microsoft . Extensions . Logging ;
11
12
12
13
using Newtonsoft . Json ;
13
14
using Newtonsoft . Json . Linq ;
14
15
15
16
using Sentry ;
17
+ using Sentry . Protocol ;
18
+
19
+ using ErrorEventArgs = Newtonsoft . Json . Serialization . ErrorEventArgs ;
16
20
17
21
namespace DisCatSharp . Net . Serialization ;
18
22
@@ -89,6 +93,43 @@ private static string SerializeObjectInternal(object value, Type type, JsonSeria
89
93
return stringWriter . ToString ( ) ;
90
94
}
91
95
96
+ /// <summary>
97
+ /// Handles <see cref="DiscordJson"/> errors.
98
+ /// </summary>
99
+ /// <param name="sender">The sender.</param>
100
+ /// <param name="e">The error event args.</param>
101
+ /// <param name="discord">The discord client.</param>
102
+ private static void DiscordJsonErrorHandler ( object ? sender , ErrorEventArgs e , BaseDiscordClient ? discord )
103
+ {
104
+ if ( discord is null || e . ErrorContext . Error is not JsonReaderException jre )
105
+ return ;
106
+
107
+ var sentryMessage = "DiscordJson error on deserialization (" + ( sender ? . GetType ( ) . Name ?? "x" ) + ")\n \n " +
108
+ "Path: " + e . ErrorContext . Path + "\n " +
109
+ "Original Object" + e . ErrorContext . OriginalObject + "\n " +
110
+ "Current Object" + e . CurrentObject ;
111
+ SentryEvent sentryEvent = new ( new DiscordJsonException ( jre ) )
112
+ {
113
+ Level = SentryLevel . Error ,
114
+ Logger = nameof ( DiscordJson ) ,
115
+ Message = sentryMessage
116
+ } ;
117
+ sentryEvent . SetFingerprint ( BaseDiscordClient . GenerateSentryFingerPrint ( sentryEvent ) ) ;
118
+ if ( discord . Configuration . AttachUserInfo && discord . CurrentUser is not null )
119
+ sentryEvent . User = new ( )
120
+ {
121
+ Id = discord . CurrentUser . Id . ToString ( ) ,
122
+ Username = discord . CurrentUser . UsernameWithDiscriminator ,
123
+ Other = new Dictionary < string , string > ( )
124
+ {
125
+ { "developer" , discord . Configuration . DeveloperUserId ? . ToString ( ) ?? "not_given" } ,
126
+ { "email" , discord . Configuration . FeedbackEmail ?? "not_given" }
127
+ }
128
+ } ;
129
+ var sid = discord . Sentry . CaptureEvent ( sentryEvent ) ;
130
+ _ = Task . Run ( discord . Sentry . FlushAsync ) ;
131
+ }
132
+
92
133
/// <summary>
93
134
/// Deserializes the specified JSON string to an object.
94
135
/// </summary>
@@ -101,8 +142,10 @@ private static T DeserializeObjectInternal<T>(string? json, BaseDiscordClient? d
101
142
102
143
var obj = JsonConvert . DeserializeObject < T > ( json , new JsonSerializerSettings ( )
103
144
{
104
- ContractResolver = new OptionalJsonContractResolver ( )
145
+ ContractResolver = new OptionalJsonContractResolver ( ) ,
146
+ Error = ( s , e ) => DiscordJsonErrorHandler ( s , e , discord )
105
147
} ) ! ;
148
+
106
149
if ( discord is null )
107
150
return obj ;
108
151
@@ -176,7 +219,8 @@ private static T DeserializeIEnumerableObjectInternal<T>(string? json, BaseDisco
176
219
177
220
var obj = JsonConvert . DeserializeObject < T > ( json , new JsonSerializerSettings ( )
178
221
{
179
- ContractResolver = new OptionalJsonContractResolver ( )
222
+ ContractResolver = new OptionalJsonContractResolver ( ) ,
223
+ Error = ( s , e ) => DiscordJsonErrorHandler ( s , e , discord )
180
224
} ) ! ;
181
225
if ( discord is null )
182
226
return obj ;
0 commit comments