Класс ExceptionHandler:
Код: Выделить всё
public class ExceptionHandler : IExceptionHandler
{
public async ValueTask TryHandleAsync(HttpContext httpContext, Exception exception, CancellationToken cancellationToken)
{
switch (exception)
{
case ArgumentNullException:
await httpContext.Response.WriteAsJsonAsync(new ProblemDetails
{
Status = StatusCodes.Status400BadRequest,
Type = exception.GetType().Name,
Title = exception.Message,
Detail = exception.Message,
Instance = $"{httpContext.Request.Method} {httpContext.Request.Path}"
});
break;
default:
await httpContext.Response.WriteAsJsonAsync(new ProblemDetails
{
Status = StatusCodes.Status500InternalServerError,
Type = exception.GetType().Name,
Title = ("Internal server error"),
Detail = ("Internal server error"),
Instance = $"{httpContext.Request.Method} {httpContext.Request.Path}"
});
break;
}
return true;
}
}
Код: Выделить всё
builder.Services.AddExceptionHandler();
builder.Services.AddProblemDetails();
...
app.MapGet("/weatherforecast", () =>
{
throw new ArgumentNullException();
});
[img]https:// i.sstatic.net/oTMKvrVA.png[/img]
Похоже, мой ExceptionHandler не работает...
Пожалуйста, помогите мне решить эту проблему.
Подробнее здесь: https://stackoverflow.com/questions/784 ... tion-net-8