problem-spring-web
problem-spring-web copied to clipboard
Support Controller Based Exception Handling
Detailed Description
I'd like to have the ability to handle specific exceptions to specific Controllers, as we can do without the library by having a method annotated with @ExceptionHandler on a given Controller
Context
Instead of handing all exception types into single class/controller advice, having it handled on appropriate controllers for exceptions which are specific for a controller, so not general one!
Your Environment
- Version used: 0.26.2
- Link to your project: closed source
You can create a ControllerAdvice that is limited to a specific controller, with assignableTypes. Give it a higher order than your your other, general advice traits (like ProblemHandling).
For example (in Kotlin):
@ControllerAdvice(assignableTypes = [ExampleController::class])
@Order(Ordered.HIGHEST_PRECEDENCE)
class ExampleControllerAdvice : AdviceTrait {
@ExceptionHandler
fun handle(exception: SpecificNotFoundException, request: NativeWebRequest): ResponseEntity<Problem> =
create(Status.NOT_FOUND, exception, request)
}
You can create a
ControllerAdvicethat is limited to a specific controller, withassignableTypes. Give it a higher order than your your other, general advice traits (likeProblemHandling).For example (in Kotlin):
@ControllerAdvice(assignableTypes = [ExampleController::class]) @Order(Ordered.HIGHEST_PRECEDENCE) class ExampleControllerAdvice : AdviceTrait { @ExceptionHandler fun handle(exception: SpecificNotFoundException, request: NativeWebRequest): ResponseEntity<Problem> = create(Status.NOT_FOUND, exception, request) }
Hi @darioseidl
Thank you!
Yes, that improves the design a bit, but still, need to create multiple Controller Advice classes and be careful with orders!