[backend/core] Properly handle errors after response has started
This commit is contained in:
parent
5e91bed599
commit
b7f89a0d97
1 changed files with 27 additions and 2 deletions
|
@ -23,8 +23,6 @@ public class ErrorHandlerMiddleware(
|
||||||
}
|
}
|
||||||
catch (Exception e)
|
catch (Exception e)
|
||||||
{
|
{
|
||||||
ctx.Response.ContentType = "application/json";
|
|
||||||
|
|
||||||
// Get the name of the class & function where the exception originated, falling back to this one
|
// 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!;
|
var type = e.TargetSite?.DeclaringType?.FullName ?? typeof(ErrorHandlerMiddleware).FullName!;
|
||||||
if (type.Contains('>'))
|
if (type.Contains('>'))
|
||||||
|
@ -33,6 +31,33 @@ public class ErrorHandlerMiddleware(
|
||||||
var logger = loggerFactory.CreateLogger(type);
|
var logger = loggerFactory.CreateLogger(type);
|
||||||
var verbosity = options.Value.ExceptionVerbosity;
|
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<MastodonApiControllerAttribute>() != null;
|
var isMastodon = ctx.GetEndpoint()?.Metadata.GetMetadata<MastodonApiControllerAttribute>() != null;
|
||||||
|
|
||||||
if (e is GracefulException ce)
|
if (e is GracefulException ce)
|
||||||
|
|
Loading…
Add table
Reference in a new issue