I have the following DTO:
public record CreatorRequestTo(
[property: JsonPropertyName("id")]
long Id,
[property: JsonPropertyName("login")]
[StringLength(64, MinimumLength = 2)]
string Login,
[property: JsonPropertyName("password")]
[StringLength(128, MinimumLength = 8)]
string Password,
[property: JsonPropertyName("firstname")]
[StringLength(64, MinimumLength = 2)]
string FirstName,
[property: JsonPropertyName("lastname")]
[StringLength(64, MinimumLength = 2)]
string LastName
);
When I try to send POST request with invalid data (password less than 8 characters), it throws ValidationException, which, for some reason, doesn't get handled by Exception Handler below:
public class GlobalExceptionHandler : IExceptionHandler
{
public async ValueTask<bool> TryHandleAsync(HttpContext httpContext, Exception exception, CancellationToken cancellationToken)
{
var errorCode = exception switch
{
EntityNotFoundException => (int)HttpStatusCode.BadRequest + "01",
ValidationException => (int)HttpStatusCode.BadRequest + "02",
_ => (int)HttpStatusCode.InternalServerError + "00"
};
var response = new ErrorResponseTo(exception.Message, errorCode);
await httpContext.Response.WriteAsJsonAsync(response, cancellationToken);
return true;
}
}
What could be the reason? I want to send my own response, not ProblemDetails one with unnecessary data.
I get this as a response:
{
"type": "https://tools.ietf.org/html/rfc9110#section-15.5.1",
"title": "One or more validation errors occurred.",
"status": 400,
"errors": {
"Password": [
"The field Password must be a string with a minimum length of 8 and a maximum length of 128."
]
},
"traceId": "00-af83651d89556659fef605e3689da98f-cffd2070a3d9bc8f-00"
}
I want to get this as a response:
{
"errorMessage": <exception message>,
"errorCode": 40002
}
Program.cs file contains:
...
builder.Services.AddExceptionHandler<GlobalExceptionHandler>();
builder.Services.AddProblemDetails();
...
var app = builder.Build();
app.UseExceptionHandler();
The ExceptionHandler deals with exception , which is usually used for throw Exception(). Your need is a model-validation issue , and the validation runs before your exception , thus it is never hit.
The document has provided several solutions https://learn.microsoft.com/en-us/aspnet/core/fundamentals/error-handling?view=aspnetcore-8.0#exception-handler-page
Program.cs
Controller
Program.cs
Controller