-
-
Notifications
You must be signed in to change notification settings - Fork 317
Expand file tree
/
Copy pathDefaultClientErrorHandler.cs
More file actions
87 lines (75 loc) · 2.38 KB
/
DefaultClientErrorHandler.cs
File metadata and controls
87 lines (75 loc) · 2.38 KB
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
using System;
using System.ComponentModel;
using System.Threading.Tasks;
using DSharpPlus.Exceptions;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
namespace DSharpPlus;
/// <summary>
/// Represents the default implementation of <see cref="IClientErrorHandler"/>
/// </summary>
public sealed class DefaultClientErrorHandler : IClientErrorHandler
{
private readonly ILogger logger;
/// <summary>
/// Creates a new instance of this type.
/// </summary>
[ActivatorUtilitiesConstructor]
public DefaultClientErrorHandler(ILogger<IClientErrorHandler> logger)
=> this.logger = logger;
/// <summary>
/// Don't use this.
/// </summary>
// glue code for legacy handling
[EditorBrowsable(EditorBrowsableState.Never)]
public DefaultClientErrorHandler(ILogger logger)
=> this.logger = logger;
/// <inheritdoc/>
public ValueTask HandleEventHandlerError
(
string name,
Exception exception,
Delegate invokedDelegate,
object sender,
object args
)
{
if (exception is BadRequestException badRequest)
{
this.logger.LogError
(
"Event handler exception for event {Event} thrown from {Method} (defined in {DeclaryingType}):\n" +
"A request was rejected by the Discord API.\n" +
" Errors: {Errors}\n" +
" Message: {JsonMessage}\n" +
" Stack trace: {Stacktrace}",
name,
invokedDelegate.Method,
invokedDelegate.Method.DeclaringType,
badRequest.Errors,
badRequest.JsonMessage,
badRequest.StackTrace
);
return ValueTask.CompletedTask;
}
this.logger.LogError
(
exception,
"Event handler exception for event {Event} thrown from {Method} (defined in {DeclaryingType}).",
name,
invokedDelegate.Method,
invokedDelegate.Method.DeclaringType
);
return ValueTask.CompletedTask;
}
/// <inheritdoc/>
public ValueTask HandleGatewayError(Exception exception)
{
this.logger.LogError
(
exception,
"An error occurred in the DSharpPlus gateway."
);
return ValueTask.CompletedTask;
}
}