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

Provide mechanism for just mapping an exception to a status code and use the default error handling afterwards #7349

Closed
mvitz opened this issue Nov 8, 2016 · 9 comments
Labels
status: superseded An issue that has been superseded by another theme: web-error-handling type: enhancement A general enhancement

Comments

@mvitz
Copy link
Contributor

mvitz commented Nov 8, 2016

If an exception is directly annotated with @ResponseStatus(HttpStatus.NOT_FOUND) the default error page handling works and a provided template at error/404.html is rendered.

However when using a custom @ControllerAdvice for exception mapping the template isn't rendered because it's now the developers responsibility.

What I want is a mechanisms which just allows me to map any exception (which I can not or do not want to annotate) to a HTTP status code and let the default error handling take over the template resolution.

The application at https://github.com/mvitz/spring-boot-exception-handling can be used to demonstrate the current behaviour.

@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged label Nov 8, 2016
@mvitz
Copy link
Contributor Author

mvitz commented Nov 8, 2016

I just found #7329 which is in some way related.

@bclozel
Copy link
Member

bclozel commented Nov 10, 2016

Indeed, this is a duplicate of #7329 - closing this now.

@bclozel bclozel closed this as completed Nov 10, 2016
@bclozel bclozel removed the status: waiting-for-triage An issue we've not yet triaged label Nov 10, 2016
@philwebb philwebb added the status: duplicate A duplicate of another issue label Nov 15, 2016
@philwebb philwebb reopened this Nov 15, 2016
@philwebb philwebb added type: enhancement A general enhancement and removed status: duplicate A duplicate of another issue labels Nov 15, 2016
@philwebb
Copy link
Member

I'm not sure that this is exactly a duplicate of #7329. It would be nice to do something to help with this (even if it's just better documentation).

@ZaitsevY
Copy link

ZaitsevY commented Jul 27, 2017

@ExceptionHandler(CustomException.class)
void handleCustomExcepption(HttpServletResponse response) throws IOException {
    response.sendError(HttpStatus.NO_CONTENT.value());
}

won't produce default error message as of Spring Boot 1.5.5

Update: my own fault (or misundestanding): no response was generated for NO_CONTENT (204) status code, after switching to any other code the response is there.

@wilkinsona
Copy link
Member

@ZaitsevY You've already said that once. There's no need to spam multiple issues with the same comment.

@ZaitsevY
Copy link

@wilkinsona excuse, please, I duplicated after having understood that the first comment went to the closed issue.

@bclozel
Copy link
Member

bclozel commented Jan 19, 2023

I think this has been superseded by the ProblemDetail support in #32634. This will be expanded as well in #19525.

@bclozel bclozel closed this as not planned Won't fix, can't repro, duplicate, stale Jan 19, 2023
@bclozel bclozel added the status: superseded An issue that has been superseded by another label Jan 19, 2023
@bclozel bclozel removed this from the 3.x milestone Jan 19, 2023
@mvitz
Copy link
Contributor Author

mvitz commented Jan 31, 2023

@bclozel I'm uncertain if this is really superseded by the ProblemDetail support because the original problem is still present with Spring Boot 3.0.2.

Given the following Spring Boot 3.0.2 application

@SpringBootApplication
public class Application {

    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }

    @ResponseStatus(NOT_FOUND)
    public static class CustomExceptionWithResponseStatusAnnotation extends RuntimeException {}

    public static class CustomExceptionWithoutResponseStatusAnnotation extends RuntimeException {}

    @ControllerAdvice
    public static class ErrorHandler {

        @ExceptionHandler(CustomExceptionWithoutResponseStatusAnnotation.class)
        @ResponseStatus(NOT_FOUND)
        public void handleCustomExceptionWithoutResponseStatusAnnotation() {}
    }

    @RestController
    public static class MyController {

        @GetMapping("/withAnnotation")
        public String withAnnotation() {
            throw new CustomExceptionWithResponseStatusAnnotation();
        }

        @GetMapping("/withoutAnnotation")
        public String withoutAnnotation() {
            throw new CustomExceptionWithoutResponseStatusAnnotation();
        }
    }
}

Hitting /withoutAnnotation which throws the Exception handled by my custom ControllerAdvice does not return a body although the /withAnnotation which throws the Exception that is directly annotated with @ResponseStatus works and returns the problem details JSON.

@bclozel
Copy link
Member

bclozel commented Jan 31, 2023

@mvitz You're right, this will be dealt with #33885

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
status: superseded An issue that has been superseded by another theme: web-error-handling type: enhancement A general enhancement
Projects
None yet
Development

No branches or pull requests

6 participants