public static class GlobalExceptionExtensions
{
public static void ConfigureExceptionHandler(this WebApplication app)
{
var logger = app.Services.GetRequiredService<ILogger<Program>>();
app.UseExceptionHandler(appError =>
{
appError.Run(async context =>
{
context.Response.StatusCode = (int)HttpStatusCode.InternalServerError;
context.Response.ContentType = "application/json";
var contextFeature = context.Features.Get<IExceptionHandlerFeature>();
if (contextFeature != null)
{
logger.LogError($"Something went wrong: {contextFeature.Error}");
await context.Response.WriteAsync(new ErrorDetails()
{
StatusCode = context.Response.StatusCode,
Message = "Internal Server Error.",
}.ToString());
}
});
});
}
}
public static class GlobalExceptionExtensions
{
public static void ConfigureExceptionHandler(this WebApplication app)
{
var logger = app.Services.GetRequiredService<ILogger<Program>>();
app.UseExceptionHandler(appError =>
{
appError.Run(async context =>
{
context.Response.StatusCode = (int)HttpStatusCode.InternalServerError;
context.Response.ContentType = "application/json";
var contextFeature = context.Features.Get<IExceptionHandlerFeature>();
if (contextFeature != null)
{
logger.LogError($"Something went wrong: {contextFeature.Error}");
await context.Response.WriteAsync(new ErrorDetails()
{
StatusCode = context.Response.StatusCode,
Message = "Internal Server Error.",
}.ToString());
}
});
});
}
}