Build / Build (push) Successful in 36s
Every answer of the Mastodon API and the client API now has its JSON shape generated when the server is built, instead of worked out by reflection while it runs; the bytes sent are unchanged. The small answers that were anonymous objects (the instance documents, notification policies, reports, lists, tags, trends, errors and the empty answers) are named types. A type without generated metadata fails at once in development and on the pasture, and in production is logged and still answered. Three lists that were sent as lazy sequences (moderation reports, the published domain blocks and the languages) are now built first. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01W4YtEJrgxG12Pc39Fai8Bd
31 lines
1.3 KiB
C#
31 lines
1.3 KiB
C#
using System.Text.Json;
|
|
using PrivaPub.Api.Mastodon.Entities;
|
|
|
|
namespace PrivaPub.Api.Mastodon.Infrastructure
|
|
{
|
|
public static class MastodonErrors
|
|
{
|
|
public static IApplicationBuilder UseMastodonErrorBodies(this IApplicationBuilder app) =>
|
|
app.UseWhen(context => context.Request.Path.StartsWithSegments("/api"), api => api.UseStatusCodePages(async pages =>
|
|
{
|
|
var response = pages.HttpContext.Response;
|
|
response.ContentType = "application/json; charset=utf-8";
|
|
await response.WriteAsync(JsonSerializer.Serialize(new ErrorEntity { Error = Message(response.StatusCode) }, MastodonJson.Options));
|
|
}));
|
|
|
|
public static string Message(int status) => status switch
|
|
{
|
|
StatusCodes.Status401Unauthorized => "The access token is invalid",
|
|
StatusCodes.Status403Forbidden => "This action is outside the authorized scopes",
|
|
StatusCodes.Status404NotFound => "Record not found",
|
|
StatusCodes.Status405MethodNotAllowed => "Method not allowed",
|
|
StatusCodes.Status413PayloadTooLarge => "Payload too large",
|
|
StatusCodes.Status415UnsupportedMediaType => "Unsupported media type",
|
|
StatusCodes.Status422UnprocessableEntity => "Validation failed",
|
|
StatusCodes.Status429TooManyRequests => "Too many requests",
|
|
>= 500 => "Something went wrong.",
|
|
_ => "Bad request"
|
|
};
|
|
}
|
|
}
|