-
-
Notifications
You must be signed in to change notification settings - Fork 318
Expand file tree
/
Copy pathBadRequestException.cs
More file actions
61 lines (52 loc) · 1.69 KB
/
Copy pathBadRequestException.cs
File metadata and controls
61 lines (52 loc) · 1.69 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
using System.Net.Http;
using System.Text.Json;
namespace DSharpPlus.Exceptions;
/// <summary>
/// Represents an exception thrown when a malformed request is sent.
/// </summary>
public class BadRequestException : DiscordException
{
/// <summary>
/// Gets the error code for this exception.
/// </summary>
public int Code { get; internal set; }
/// <summary>
/// Gets the form error responses in JSON format.
/// </summary>
public string? Errors { get; internal set; }
internal BadRequestException(HttpRequestMessage request, HttpResponseMessage response, string content)
: base("Bad request: " + response.StatusCode)
{
this.Request = request;
this.Response = response;
try
{
using JsonDocument document = JsonDocument.Parse(content);
JsonElement responseModel = document.RootElement;
if
(
responseModel.TryGetProperty("code", out JsonElement code)
&& code.ValueKind == JsonValueKind.Number
)
{
this.Code = code.GetInt32();
}
if
(
responseModel.TryGetProperty("message", out JsonElement message)
&& message.ValueKind == JsonValueKind.String
)
{
this.JsonMessage = message.GetString();
}
if
(
responseModel.TryGetProperty("errors", out JsonElement errors)
)
{
this.Errors = JsonSerializer.Serialize(errors);
}
}
catch { }
}
}