diff --git a/Iceshrimp.Backend/Core/Middleware/ErrorHandlerMiddleware.cs b/Iceshrimp.Backend/Core/Middleware/ErrorHandlerMiddleware.cs index 03783a7c..576e71d0 100644 --- a/Iceshrimp.Backend/Core/Middleware/ErrorHandlerMiddleware.cs +++ b/Iceshrimp.Backend/Core/Middleware/ErrorHandlerMiddleware.cs @@ -23,8 +23,6 @@ public class ErrorHandlerMiddleware( } catch (Exception e) { - ctx.Response.ContentType = "application/json"; - // Get the name of the class & function where the exception originated, falling back to this one var type = e.TargetSite?.DeclaringType?.FullName ?? typeof(ErrorHandlerMiddleware).FullName!; if (type.Contains('>')) @@ -33,6 +31,33 @@ public class ErrorHandlerMiddleware( var logger = loggerFactory.CreateLogger(type); var verbosity = options.Value.ExceptionVerbosity; + if (ctx.Response.HasStarted) + { + if (e is GracefulException { SuppressLog: false } earlyCe) + { + if (earlyCe.Details != null) + { + logger.LogDebug("Request {id} was rejected with {statusCode} {error}: {message} - {details}", + ctx.TraceIdentifier, (int)earlyCe.StatusCode, earlyCe.Error, earlyCe.Message, + earlyCe.Details); + } + else + { + logger.LogDebug("Request {id} was rejected with {statusCode} {error}: {message}", + ctx.TraceIdentifier, (int)earlyCe.StatusCode, earlyCe.Error, earlyCe.Message); + } + } + else if (e is not GracefulException) + { + logger.LogError("Request {id} encountered an unexpected error: {exception}", ctx.TraceIdentifier, + e.ToString()); + } + + return; + } + + ctx.Response.ContentType = "application/json"; + var isMastodon = ctx.GetEndpoint()?.Metadata.GetMetadata() != null; if (e is GracefulException ce)