Base solution for your next web application
Open Closed

Input validation errors generate HTTP status code 500 #5543


User avatar
0
MarkusBernhardt created

I have created an input DTO with custom validation as described in [https://aspnetboilerplate.com/Pages/Documents/Validating-Data-Transfer-Objects?searchKey=validation])

When the validation fails the HTTP status code 500 is set. This is wrong. It should be 400.

From the logs:

WARN  2018-08-24 05:37:46,252 [3    ] Mvc.ExceptionHandling.AbpExceptionFilter - Method arguments are not valid! See ValidationErrors for details.
Abp.Runtime.Validation.AbpValidationException: Method arguments are not valid! See ValidationErrors for details.
   at Abp.Runtime.Validation.Interception.MethodInvocationValidator.ThrowValidationError()
   at Abp.Runtime.Validation.Interception.MethodInvocationValidator.Validate()
   at Abp.AspNetCore.Mvc.Validation.AbpValidationActionFilter.<OnActionExecutionAsync>d__3.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker.<InvokeNextActionFilterAsync>d__10.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker.Rethrow(ActionExecutedContext context)
   at Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker.Next(State& next, Scope& scope, Object& state, Boolean& isCompleted)
   at Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker.<InvokeInnerFilterAsync>d__13.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.<InvokeNextExceptionFilterAsync>d__24.MoveNext()
WARN  2018-08-24 05:37:46,255 [3    ] Mvc.ExceptionHandling.AbpExceptionFilter - There are 2 validation errors:
WARN  2018-08-24 05:37:46,255 [3    ] Mvc.ExceptionHandling.AbpExceptionFilter - AssignedPersonId must be set if SendEmailToAssignedPerson is true!
WARN  2018-08-24 05:37:46,255 [3    ] Mvc.ExceptionHandling.AbpExceptionFilter - The Description field is required. (Description)
INFO  2018-08-24 05:37:46,258 [3    ] ore.Mvc.Internal.ControllerActionInvoker - Executed action B4Payment.SEPAexpress.Api.V2.Customers.CustomersAppService.CreateCustomer (B4Payment.SEPAexpress.Api.V2) in 434.6818ms
ERROR 2018-08-24 05:37:46,261 [3    ] Microsoft.AspNetCore.Server.Kestrel      - Connection id "0HLG9EBGV8MG4", Request id "0HLG9EBGV8MG4:00000004": An unhandled exception was thrown by the application.
Abp.Runtime.Validation.AbpValidationException: Method arguments are not valid! See ValidationErrors for details.
   bei Abp.Runtime.Validation.Interception.MethodInvocationValidator.ThrowValidationError()
   bei Abp.Runtime.Validation.Interception.MethodInvocationValidator.Validate()
   bei Abp.AspNetCore.Mvc.Validation.AbpValidationActionFilter.<OnActionExecutionAsync>d__3.MoveNext()
…

Any ideas what I do wrong?


9 Answer(s)
  • User Avatar
    0
    maliming created
    Support Team

    Is there a DontWrapResult Attribute on the dto corresponding method?

  • User Avatar
    0
    MarkusBernhardt created

    Not at the moment, but I am planning to add it, because this is a web API for external use.

  • User Avatar
    0
    maliming created
    Support Team

    <a class="postlink" href="https://github.com/aspnetboilerplate/aspnetboilerplate/blob/354d66d76f63391ee3fd524d7cd35a1701fd3b71/src/Abp.AspNetCore/AspNetCore/Mvc/ExceptionHandling/AbpExceptionFilter.cs#L38">https://github.com/aspnetboilerplate/as ... ter.cs#L38</a>

    <a class="postlink" href="https://github.com/aspnetboilerplate/aspnetboilerplate/blob/354d66d76f63391ee3fd524d7cd35a1701fd3b71/src/Abp.AspNetCore/AspNetCore/Mvc/ExceptionHandling/AbpExceptionFilter.cs#L92">https://github.com/aspnetboilerplate/as ... ter.cs#L92</a>

    You can look at the source code of AbpExceptionFilter. I don't see why it is 500. Please share your project if possible. :)

  • User Avatar
    0
    MarkusBernhardt created

    Is there a way to debug into that code? I found some tickets on GitHub, but no working solution for me.

  • User Avatar
    0
    maliming created
    Support Team

    Follow this documentation to try to debug the code. <a class="postlink" href="https://aspnetboilerplate.com/Pages/Documents/Debugging">https://aspnetboilerplate.com/Pages/Documents/Debugging</a>

  • User Avatar
    0
    MarkusBernhardt created

    This does not work for me. I'm on VS 2017 latest.

    When I set a break point in my code, open the stack and try to open the calling method I get: Abp.pdb not loaded

  • User Avatar
    0
    maliming created
    Support Team

    There are some settings you can try

    [attachment=0:1kjvi4nl]123.png[/attachment:1kjvi4nl]

  • User Avatar
    0
    MarkusBernhardt created

    Ok. I finally got debugging to work. Yeah!

    Now the problem is clear:

    • my service returns Task<IActionResult>
    • AbpExceptionFilter.HandleAndWrapException line 64 tests, whether the return type is a ObjectResult
    • AbpExceptionFilter.GetStatusCode that sets the correct error code is never reached

    How can I change this behaviour?

  • User Avatar
    0
    ryancyq created
    Support Team

    Is AbpValidationException being catch? It should continue to be thrown and handle by Abp framework.

    If you want to handle the exception yourself, change the method return type to Task<JsonResult> .