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

Error HandlerMethodValidationException not handle #27476

Open
1 task done
duongxinh2003 opened this issue Oct 2, 2024 · 2 comments
Open
1 task done

Error HandlerMethodValidationException not handle #27476

duongxinh2003 opened this issue Oct 2, 2024 · 2 comments

Comments

@duongxinh2003
Copy link

duongxinh2003 commented Oct 2, 2024

Overview of the issue

Error HandlerMethodValidationException didn't handle when validation at request param

Motivation for or Use Case

Create a REST API with valid data at @RequestParam

Reproduce the error

When I create an API and valid data of jakarta.validation package

@PutMapping("/sell-abcxyz")
    @PreAuthorize("hasAuthority(\"" + AuthoritiesConstants.USER + "\")")
    public ResponseEntity<AbcResponse> sellPlant(@RequestParam Long userId,
                 @RequestParam @Min(value = 1, message = "Must be greater than zero") Integer quantity) {
        AbcResponse userItem = userItemService.sellItem(userId, quantity);
        return ResponseEntity.ok(userItem);
    }

The actual response not showing message of error:

{
    "type": "about:blank/problem-with-message",
    "title": "Bad Request",
    "status": 400,
    "detail": "400 BAD_REQUEST \"Validation failure\"",
    "instance": "/api/user-items/sell-abcxyz",
    "message": "error.validation",
    "path": "/api/user-items/sell-abcxyz"
}

I need a specific error message. At least, the error message must show the default value of jakarta validation error message

Related issues
Suggest a Fix

I think, in the ExceptionTranslator.getProblemDetailWithCause method you need to handle HandlerMethodValidationException exception like:

if (ex instanceof HandlerMethodValidationException methodValidationException) {
            return ProblemDetailWithCauseBuilder.instance()
                .withStatus(HttpStatus.BAD_REQUEST.value())
                .withType(ErrorConstants.DEFAULT_TYPE)
                .withTitle(methodValidationException.getReason())
                .withDetail(methodValidationException.getValueResults().get(0)
                    .getResolvableErrors().get(0).getDefaultMessage())
                .build();
        }

then the response looks like this:

{
    "type": "about:blank/problem-with-message",
    "title": "Bad Request",
    "status": 400,
    "detail": "Quantity must be greater than zero",
    "instance": "/api/user-items/sell-abcxyz",
    "message": "error.http.400",
    "path": "/api/user-items/sell-abcxyz"
}
JHipster Version(s)

8.7.1

Browsers and Operating System

MacOS 13.5, JDK 17

  • Checking this box is mandatory (this is just to show you read everything)
@shrralis
Copy link
Contributor

shrralis commented Oct 15, 2024

Yes, I was fighting with that as well some time ago.

The thing is that the MethodArgumentNotValidException is not handled by ExceptionTranslator#handleAnyException because there's ResponseEntityExceptionHandler#handleException defined.

I think I overrode handleExceptionInternal() and did some stuff there to build proper error responses for that case.

I can take a look at this next week if you want me to.

@duongxinh2003
Copy link
Author

@shrralis Thanks

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

No branches or pull requests

3 participants
@shrralis @duongxinh2003 and others