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

Support Controller Based Exception Handling #689

Open
durimkryeziu opened this issue Sep 29, 2021 · 2 comments
Open

Support Controller Based Exception Handling #689

durimkryeziu opened this issue Sep 29, 2021 · 2 comments
Labels

Comments

@durimkryeziu
Copy link

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 durimkryeziu changed the title Support handling exceptions in different controllers Support Controller Based Exception Handling Sep 29, 2021
@darioseidl
Copy link

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)

}

@durimkryeziu
Copy link
Author

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!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants