problem-spring-web icon indicating copy to clipboard operation
problem-spring-web copied to clipboard

Support Controller Based Exception Handling

Open durimkryeziu opened this issue 4 years ago • 2 comments

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

durimkryeziu avatar Sep 29 '21 12:09 durimkryeziu

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)

}

darioseidl avatar Oct 21 '21 12:10 darioseidl

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)

}

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!

durimkryeziu avatar Oct 25 '21 08:10 durimkryeziu