Skip to content

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