2026-10-03 10:46:20 +02:00
|
|
|
using System.Text.Json;
|
2026-11-10 07:28:02 +01:00
|
|
|
using PrivaPub.Api.Mastodon.Entities;
|
2026-10-03 10:46:20 +02:00
|
|
|
|
|
|
|
|
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";
|
2026-11-10 07:28:02 +01:00
|
|
|
await response.WriteAsync(JsonSerializer.Serialize(new ErrorEntity { Error = Message(response.StatusCode) }, MastodonJson.Options));
|
2026-10-03 10:46:20 +02:00
|
|
|
}));
|
|
|
|
|
|
|
|
|
|
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"
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
}
|