jasontaylordev / CleanArchitecture

Clean Architecture Solution Template for ASP.NET Core
MIT License
16.96k stars 3.64k forks source link

ValidationException doesn't return the errors only a stacktrace #1085

Open stijnvddonk opened 10 months ago

stijnvddonk commented 10 months ago

Describe the bug When a validation of a api request body fails the thrown exception doesn't give any details on what exactly failed.

To Reproduce Steps to reproduce the behavior:

  1. Make a call to any endpoint with a validation added to it but make sure it fails
  2. See error
image

Here the failures has 1 item: {Email is required and must be valid.}

image

The response i get is the following:

HDN_Component_App.Application.Common.Exceptions.ValidationException: One or more validation failures have occurred. at HDN_Component_App.Application.Common.Behaviours.ValidationBehaviour2.Handle(TRequest request, RequestHandlerDelegate1 next, CancellationToken cancellationToken) at HDN_Component_App.Application.Common.Behaviours.AuthorizationBehaviour2.Handle(TRequest request, RequestHandlerDelegate1 next, CancellationToken cancellationToken) at HDN_Component_App.Application.Common.Behaviours.UnhandledExceptionBehaviour2.Handle(TRequest request, RequestHandlerDelegate1 next, CancellationToken cancellationToken) at HDN_Component_App.Web.Endpoints.Users.CreateUser(ISender sender, CreateUserCommand command) in D:\Projects\HDN-Component-App\Back-End\src\Web\Endpoints\Users.cs:line 34 at Microsoft.AspNetCore.Http.RequestDelegateFactory.gExecuteAwaited|121_0(Task`1 task, HttpContext httpContext) at Microsoft.AspNetCore.Http.RequestDelegateFactory.<>c__DisplayClass94_2.<b2>d.MoveNext() --- End of stack trace from previous location --- at NSwag.AspNetCore.Middlewares.SwaggerUiIndexMiddleware.Invoke(HttpContext context) at NSwag.AspNetCore.Middlewares.RedirectToIndexMiddleware.Invoke(HttpContext context) at Microsoft.AspNetCore.Authorization.AuthorizationMiddleware.Invoke(HttpContext context) at Microsoft.AspNetCore.Authentication.AuthenticationMiddleware.Invoke(HttpContext context) at Microsoft.AspNetCore.Diagnostics.DeveloperExceptionPageMiddlewareImpl.Invoke(HttpContext context)

HEADERS

Accept: / Connection: keep-alive Host: localhost:5001 User-Agent: PostmanRuntime/7.36.0 Accept-Encoding: gzip, deflate, br Cache-Control: no-cache Content-Type: application/json Content-Length: 183 Postman-Token: 921198c3-f13f-40fb-8c9a-b8ceb7cf104d

How do i get it so that the response is the errors not the stacktrace.

jasontaylordev commented 8 months ago

Hi! I can't reproduce your issue using the sample code in the template. When I try, I get the expected response: image

Based on your screenshots and error message, I can see that the request is correctly validated and that the appropriate exception is thrown. The last piece of the puzzle is within ./src/Web/Infrastructure/CustomExceptionHandler.cs. This class is responsible for handling exceptions thrown by the application. For known exception types, it will return a problem details response with the appropriate status code and message.

You can troubleshoot your issue and verify that the CustomExceptionHandler is invoking HandleAsync correctly and ensure that it is returning the appropriate problem details response. Let me know how you go.