Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion NetCord/Rest/RestError.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

namespace NetCord.Rest;

public class RestError(int code, string message, IRestErrorGroup? error)
public sealed class RestError(int code, string message, IRestErrorGroup? error)
{
[JsonPropertyName("code")]
public int Code { get; } = code;
Expand All @@ -14,6 +14,8 @@ public class RestError(int code, string message, IRestErrorGroup? error)
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)]
[JsonPropertyName("errors")]
public IRestErrorGroup? Error { get; } = error;

public override string ToString() => JsonSerializer.Serialize(this, Serialization.Default.RestError);
}

[JsonConverter(typeof(IRestErrorGroupConverter))]
Expand Down
48 changes: 48 additions & 0 deletions NetCord/Rest/RestException.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,4 +46,52 @@ private static string GetMessage(HttpStatusCode statusCode, string? reasonPhrase
public string? ReasonPhrase { get; }

public RestError? Error { get; }

public override string ToString()

Copilot AI Jun 13, 2025

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[nitpick] This manual span-based construction is performant but complex; consider adding a comment or using a simpler StringBuilder approach if performance is not critical to improve readability and future maintenance.

Copilot uses AI. Check for mistakes.
{
const string ClassName = "NetCord.Rest.RestException";

var message = Message;
var stackTrace = StackTrace;
string? jsonError;

int length = ClassName.Length + 2 + message.Length;

if (Error is { } error)
{
jsonError = error.ToString();
length += Environment.NewLine.Length + jsonError.Length;
}
else
jsonError = null;

if (stackTrace is not null)
length += Environment.NewLine.Length + stackTrace.Length;

return string.Create(length, (Message: message, StackTrace: stackTrace, JsonError: jsonError), static (span, state) =>
{
Write(ClassName, ref span);

Write(": ", ref span);
Write(state.Message, ref span);

if (state.JsonError is { } jsonError)
{
Write(Environment.NewLine, ref span);
Write(jsonError, ref span);
}

if (state.StackTrace is { } stackTrace)
{
Write(Environment.NewLine, ref span);
Write(stackTrace, ref span);
}
});

static void Write(string source, ref Span<char> dest)
{
source.CopyTo(dest);
dest = dest[source.Length..];
}
}
}
6 changes: 2 additions & 4 deletions Tests/NetCord.Test/Program.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
using System.Reflection;
using System.Runtime.InteropServices;
using System.Text.Json;

using Microsoft.Extensions.DependencyInjection;

Expand Down Expand Up @@ -148,10 +147,9 @@ private static async Task Main()
{
await manager.CreateCommandsAsync(_client.Rest, _client.Id, true);
}
catch (RestException ex)
catch (Exception ex)
{
var error = ex.Error;
Console.WriteLine(error is null ? "No error returned." : JsonSerializer.Serialize(error, Discord.SerializerOptions));
Console.WriteLine(ex);
}

await Task.Delay(-1);
Expand Down