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

Simplify mapping of custom exception to an RFC 7807 response in ResponseEntityExceptionHandler #29384

Closed
rstoyanchev opened this issue Oct 26, 2022 · 2 comments
Assignees
Labels
in: web Issues in web modules (web, webmvc, webflux, websocket) type: enhancement A general enhancement
Milestone

Comments

@rstoyanchev
Copy link
Contributor

rstoyanchev commented Oct 26, 2022

ResponseEntityExceptionHandler supports RFC 7807 transparently for any ErrorResponse exception, but as explained in #28814 (comment), what a subclass needs to do to map a custom exception is not as easy as could be. We could add a protected method to create the ProblemDetail and that method could also perform a lookup via MessageSource to resolve the detail:

protected ProblemDetail createProblemDetail(
		Exception ex, HttpStatusCode status, String detailMessage, @Nullable Object[] detailMessageArguments) {
    // ...
}

This would allow handling a custom exception like this:

@ExceptionHandler
ResponseEntity<Object> handleMyException(MyException myException, WebRequest request) {

    ProblemDetail body = createProblemDetail(
            HttpStatus.BAD_REQUEST, "My default message", new Object[] {ex.getParam1(), ex.getParam2()});

    // ...

    return handleExceptionInternal(myException, body, null, null, request);
}
@rstoyanchev rstoyanchev added in: web Issues in web modules (web, webmvc, webflux, websocket) type: enhancement A general enhancement labels Oct 26, 2022
@rstoyanchev rstoyanchev added this to the 6.0.0-RC3 milestone Oct 26, 2022
@rstoyanchev rstoyanchev self-assigned this Oct 26, 2022
@biergit
Copy link

biergit commented Oct 27, 2022

Hey @rstoyanchev

on another thought there is also the "title" member for which having an equally convenient i18n measure may be nice. What do you think?
Then I read about additional members in the RfC:

If such additional members are defined, their names SHOULD start with
a letter (ALPHA, as per [RFC5234], Appendix B.1) and SHOULD consist
of characters from ALPHA, DIGIT ([RFC5234], Appendix B.1), and "_"
(so that it can be serialized in formats other than JSON), and they
SHOULD be three characters or longer.

and wondered if this could be checked by Spring as well or if I'm definitely going to far now? 😃

@rstoyanchev
Copy link
Contributor Author

Yes, it should be straight forward to do the same for the "title", so I've created #29407.

Additional members are less straight forward as those could be in the "properties" map in ProblemDetail or actual fields on a sub-class. I'll leave those out for now.

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: enhancement A general enhancement
Projects
None yet
Development

No branches or pull requests

2 participants