springfox icon indicating copy to clipboard operation
springfox copied to clipboard

Support for rendering exception response statuses in response codes

Open dilipkrish opened this issue 11 years ago • 18 comments

    @RequestMapping
    public void someMethod() throws ResourceNotFoundException {...}

   @ExceptionHandler(ResourceNotFoundException)
   @ResponseStatus(HttpStatus.NOT_FOUND)
   void throw404() {...}

should generate 404 as a response status for declared exceptions

dilipkrish avatar Dec 05 '14 18:12 dilipkrish

:+1: +1

omayevskiy avatar May 14 '15 10:05 omayevskiy

This feature would be awesome. Should be also supported when declaring @ExceptionHandler in "@ControllerAdvice" annotated classes.

GitVhaos avatar Jun 02 '15 13:06 GitVhaos

Awesome! Any plans for this issue?

apomelov avatar Jun 08 '15 09:06 apomelov

+1

evgeny-konovalov avatar Jun 08 '15 10:06 evgeny-konovalov

+1

alex-under avatar Sep 14 '15 08:09 alex-under

+1

ret0 avatar Oct 26 '15 12:10 ret0

+1

ianaz avatar Dec 02 '16 11:12 ianaz

+1

rmasgutov avatar Dec 21 '16 11:12 rmasgutov

Hi all, I am currently working on a project which requires this feature to generate swagger spec based on exception handler. Therefore, I would like to try to fix this if no one picked up this issue yet. Also, if there's any previous discussions regarding this issue regarding the potential approach, it would be helpful. Thanks.

zacyang avatar Jul 21 '17 06:07 zacyang

Not sure that it is easy. So any approach you take, if you solve the problem we'd be glad to accept your contribution. Perhaps easier to do via some (custom or otherwise) annotations. At a high level the problem with controller exception handler is that there is no way to know if your controller does not use checked exception without additional annotations.

dilipkrish avatar Jul 28 '17 02:07 dilipkrish

I couldn't find a way to the org.springframework.web.method.HandlerMethod (the java.lang.reflect.Method either) from the RequestMappingContext/OperationContext. Is it possible to expose one or the other so that we can work with it in our plugins ?

benweet avatar Jul 26 '18 18:07 benweet

@benweet that is intentionally protected from being exposed. Would you mind explaining how you're using the method and we can see if we can abstract that on one of those contexts

dilipkrish avatar Aug 04 '18 00:08 dilipkrish

@dilipkrish I would have a class that implements the OperationBuilderPlugin interface and, inside the apply(OperationContext operationContext) method, I would list the operation exception via java.lang.reflect.Method.getExceptionTypes() (which I can't access at the moment). For each exception type, I would add a ResponseMessage to the OperationBuilder.

benweet avatar Aug 04 '18 17:08 benweet

@benweet isnt that implementation details that is better supported by adding support for @ExceptionHandler?

dilipkrish avatar Aug 05 '18 00:08 dilipkrish

@dilipkrish I'm not really interested in @ExceptionHandler. My use case is to produce proper error message documentation for exceptions like this:

  @RequestMapping(
      method = RequestMethod.POST,
      path = "/api/requests",
      produces = "application/json")
  public RequestDto createRequest() throws InvalidApiKey {
@ResponseStatus(
    value = HttpStatus.FORBIDDEN,
    reason = "Unknown or missing API key.")
public class InvalidApiKey extends Exception {
}

And then implement the OperationBuilderPlugin like this:

  @Override
  public void apply(OperationContext operationContext) {
    ...
    // Get the java.lang.reflect.Method somehow
    for (Class exceptionType: method.getExceptionTypes())
      ResponseStatus responseStatus = errorType.getAnnotation(ResponseStatus.class);
      if (responseStatus != null) {
        HttpStatus httpStatus = responseStatus.value();
        // Build the ResponseMessage
        ...
    operationContext.operationBuilder().responseMessages(responseMessages);
    ...

benweet avatar Aug 05 '18 14:08 benweet

Ah got it, which is what this initial story was for. Thanks for clarifying. Yes, that requires that RequestMappingContext and consequently RequestHandler expose the exceptions in a way that doesnt leak implementation details. I added this feature request to the current version

dilipkrish avatar Aug 06 '18 00:08 dilipkrish

any volunteers? :)

dlangerenken avatar Jul 02 '20 21:07 dlangerenken

This is a little bit involved. Dont want to rush this into 3.0 which is due in a week

dilipkrish avatar Jul 03 '20 01:07 dilipkrish