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

BindException raised instead of MethodArgumentNotValidException subclass #30100

Closed
tinah-1973 opened this issue Mar 10, 2023 · 2 comments
Closed
Assignees
Labels
in: web Issues in web modules (web, webmvc, webflux, websocket) type: bug A general bug
Milestone

Comments

@tinah-1973
Copy link

tinah-1973 commented Mar 10, 2023

In our application we override handleBindException from ResponseEntityExceptionHandler to improve the output of error message.

    @Override
    protected ResponseEntity<Object> handleBindException(
            BindException ex, HttpHeaders headers, HttpStatusCode status, WebRequest request) {
        final List<String> errors = new ArrayList<String>();

        for (final FieldError error : ex.getBindingResult().getFieldErrors()) {
            errors.add(error.getField() + " has an invalid value: " + error.getRejectedValue());
        }

        for (final ObjectError error : ex.getBindingResult().getGlobalErrors()) {
            errors.add(error.getObjectName() + ": " + error.getDefaultMessage());
        }

        apiError.setStatus(HttpStatus.BAD_REQUEST);
        apiError.setMessage(errors.toString());
        apiError.generateDate();

        return handleExceptionInternal(ex, apiError, headers, apiError.getStatus(), request);
    }

Since Spring 6.0 the method is marked as deprecated, removal is planned for 6.1.
I tried to override handleMethodArgumentNotValid Method, but that has no impact.
For example if several arguments are not valid, then a BindException is thrown instead
How can I deal with this problem, so that my solution works with the next upgrade?

@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged or decided on label Mar 10, 2023
@rstoyanchev rstoyanchev added in: web Issues in web modules (web, webmvc, webflux, websocket) type: bug A general bug and removed status: waiting-for-triage An issue we've not yet triaged or decided on labels Mar 10, 2023
@rstoyanchev rstoyanchev self-assigned this Mar 10, 2023
@rstoyanchev rstoyanchev added this to the 6.0.7 milestone Mar 10, 2023
@rstoyanchev
Copy link
Contributor

It looks like there is one more place in ModelAttributeMethodProcessor that wasn't switched to use MethodArgumentNotValidException as part of #29251. Could you confirm that the BindException you're seeing is raised from here?

@rstoyanchev rstoyanchev changed the title How to handle a BindException in Spring 6 BindException raised instead of MethodArgumentNotValidException subclass Mar 10, 2023
@tinah-1973
Copy link
Author

Yes I can confirm that the BindException is initiated there!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
in: web Issues in web modules (web, webmvc, webflux, websocket) type: bug A general bug
Projects
None yet
Development

No branches or pull requests

3 participants