Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
stijnvddonk opened this issue Jan 4, 2024 · 1 comment
Open

Comments

@stijnvddonk
Copy link

stijnvddonk commented Jan 4, 2024

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.g__ExecuteAwaited|121_0(Task`1 task, HttpContext httpContext)
at Microsoft.AspNetCore.Http.RequestDelegateFactory.<>c__DisplayClass94_2.<b__2>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
Copy link
Owner

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants