08-02-2023, 12:12 PM
## The Question
When creating a controller in Spring Boot to handle **all** errors/exceptions in a custom way, **including custom exceptions**, which technique should be preferred?
1. Should the controller implement the Spring Boot's `ErrorController`?
2. Should the controller extend Spring's `ResponseEntityExceptionHandler`?
3. Both: a single controller implementing and extending both classes including both of their functionality?
4. Both: two separate controllers, one implementing `ErrorController`, the other extending `ResponseEntityExceptionHandler`?
## The Goal
The reason for this post is to find a way of exception-handling in Spring Boot with *all* of the following attributes:
* All `Throwable`s occurring in controllers/filters/interceptors during the handling of a request should be caught.
* In the case of catching a `Throwable`, we **do not** want to expose any of the stack trace or other implementation details to the client **ever** (unless explicitly coded that way).
* It should be possible to handle all occurred `Throwable`s separately by their class. For any other non-specified type, a default response can be specified. (I know for sure, that this *is* possible with `@ExceptionHandler`. But `ErrorController`?)
* The code should be as clean and explicit as possible with no ugly work-arounds or UB to achieve this goal.
## More Details
I noticed that both the controllers (see 1 and 2 above) may contain methods returning a `ResponseEntity` object, thus handling the occurred exception and returning a response to the client. So they could in theory produce the same result?
There are several tutorials out there on the use of techniques 1 and 2. But I have found no articles considering both options, comparing them or using them together, which raises several additional questions:
1. Should they even be considered together?
2. What are the main differences between these two proposed techniques? What are the similarities?
3. Is one a more powerful version of the other? Is there something one can do that the other can't and vice versa?
4. Can they be used together? Are there situations where this would be necessary?
5. If they are used together, how would an exception be handled? Does it go through both handlers or just one? In the case of the latter, which one?
6. If they are used together, and an exception is thrown *inside* the controller (one or the other) **during exception handling**, how would that exception be handled? Is it sent to the other controller? Could exceptions theoretically start bouncing between controllers or create some other kind of non-recovering loop?
7. Is there any trusted/official documentation on how Spring Boot uses Spring's `ResponseEntityExceptionHandler` internally or how it expects it to be used in Spring Boot applications?
8. If `ResponseEntityExceptionHandler` alone is already enough, then why does `ErrorController` exist?
When looking at the Spring's `ResponseEntityExceptionHandler` together with the `@ExceptionHandler` annotation, it seems to be more powerful in handling different types of exceptions separately and using cleaner code. But because Spring Boot is built on top of Spring, does this mean:
* When using Spring Boot, we should implement `ErrorController` instead of extending `ResponseEntityExceptionHandler`?
* Can `ErrorController` do everything `ResponseEntityExceptionHandler` can, including handling different types of exceptions separately?
**EDIT:** Related: [Spring @ControllerAdvice vs ErrorController][1]
[1]:
When creating a controller in Spring Boot to handle **all** errors/exceptions in a custom way, **including custom exceptions**, which technique should be preferred?
1. Should the controller implement the Spring Boot's `ErrorController`?
2. Should the controller extend Spring's `ResponseEntityExceptionHandler`?
3. Both: a single controller implementing and extending both classes including both of their functionality?
4. Both: two separate controllers, one implementing `ErrorController`, the other extending `ResponseEntityExceptionHandler`?
## The Goal
The reason for this post is to find a way of exception-handling in Spring Boot with *all* of the following attributes:
* All `Throwable`s occurring in controllers/filters/interceptors during the handling of a request should be caught.
* In the case of catching a `Throwable`, we **do not** want to expose any of the stack trace or other implementation details to the client **ever** (unless explicitly coded that way).
* It should be possible to handle all occurred `Throwable`s separately by their class. For any other non-specified type, a default response can be specified. (I know for sure, that this *is* possible with `@ExceptionHandler`. But `ErrorController`?)
* The code should be as clean and explicit as possible with no ugly work-arounds or UB to achieve this goal.
## More Details
I noticed that both the controllers (see 1 and 2 above) may contain methods returning a `ResponseEntity` object, thus handling the occurred exception and returning a response to the client. So they could in theory produce the same result?
There are several tutorials out there on the use of techniques 1 and 2. But I have found no articles considering both options, comparing them or using them together, which raises several additional questions:
1. Should they even be considered together?
2. What are the main differences between these two proposed techniques? What are the similarities?
3. Is one a more powerful version of the other? Is there something one can do that the other can't and vice versa?
4. Can they be used together? Are there situations where this would be necessary?
5. If they are used together, how would an exception be handled? Does it go through both handlers or just one? In the case of the latter, which one?
6. If they are used together, and an exception is thrown *inside* the controller (one or the other) **during exception handling**, how would that exception be handled? Is it sent to the other controller? Could exceptions theoretically start bouncing between controllers or create some other kind of non-recovering loop?
7. Is there any trusted/official documentation on how Spring Boot uses Spring's `ResponseEntityExceptionHandler` internally or how it expects it to be used in Spring Boot applications?
8. If `ResponseEntityExceptionHandler` alone is already enough, then why does `ErrorController` exist?
When looking at the Spring's `ResponseEntityExceptionHandler` together with the `@ExceptionHandler` annotation, it seems to be more powerful in handling different types of exceptions separately and using cleaner code. But because Spring Boot is built on top of Spring, does this mean:
* When using Spring Boot, we should implement `ErrorController` instead of extending `ResponseEntityExceptionHandler`?
* Can `ErrorController` do everything `ResponseEntityExceptionHandler` can, including handling different types of exceptions separately?
**EDIT:** Related: [Spring @ControllerAdvice vs ErrorController][1]
[1]:
[To see links please register here]