Skip to content

Pick up ExceptionHandlers from any parent of a class marked as @ControllerAdvice #1156

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

Merged
merged 1 commit into from
May 17, 2021

Conversation

eshishkin
Copy link
Contributor

Currently the library scans for exception handlers only declared method of @ControllerAdvices. So, if an advice has parent classes with exception handlers, handlers will not be picked up and models will not be displayed in OpenAPI spec.

Sometimes, it might be useful to have one class with common handlers (e.g. a jar library that should be imported in all services across a project/company) and extend with more specific handlers.

Something like that

-----------------------------------------
public class Error {
  // ...
}
-----------------------------------------
public class CommonErrorHandler {

  @ResponseStatus(INTERNAL_SERVER_ERROR)
  @ExceptionHandler(Exception.class)
  public Error onException(Exception ex) {
    return new Error();
  }

  // other common handlers
}
-----------------------------------------
@ControllerAdvice
public class ErrorHandler extends CommonErrorHandler {
  
  //additional error handlers

  @ResponseStatus(BAD_REQUEST)
  @ExceptionHandler(IllegalArgumentException.class)
  public SpecificError onIllegalArgumentException(IllegalArgumentException ex) {
    return new SpecificError ();
  }

}
-----------------------------------------

@bnasslahsen bnasslahsen merged commit ecc45df into springdoc:master May 17, 2021
@bnasslahsen
Copy link
Collaborator

Thank you @eshishkin for your contribution.

@bnasslahsen bnasslahsen added the enhancement New feature or request label Jan 10, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants