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 for rendering exception response statuses in response codes #521

Open
dilipkrish opened this issue Dec 5, 2014 · 18 comments
Open

Comments

@dilipkrish
Copy link
Member

    @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 dilipkrish modified the milestone: 0.9.4 Dec 5, 2014
@adrianbk adrianbk modified the milestones: 0.9.5, 0.9.4 Dec 21, 2014
@dilipkrish dilipkrish modified the milestones: 0.9.5, 2.0 Feb 14, 2015
@dilipkrish dilipkrish modified the milestones: 2.1, 2.0 Apr 6, 2015
@omayevskiy
Copy link

👍 +1

@GitVhaos
Copy link
Contributor

GitVhaos commented Jun 2, 2015

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

@apomelov
Copy link

apomelov commented Jun 8, 2015

Awesome! Any plans for this issue?

@evgeny-konovalov
Copy link

+1

@dilipkrish dilipkrish modified the milestones: 2.2.0, 2.3.0 Aug 11, 2015
@dilipkrish dilipkrish modified the milestones: 2.3.0, Someday Aug 28, 2015
@alex-under
Copy link

+1

1 similar comment
@ret0
Copy link

ret0 commented Oct 26, 2015

+1

@ianaz
Copy link

ianaz commented Dec 2, 2016

+1

1 similar comment
@rmasgutov
Copy link

+1

@zacyang
Copy link

zacyang commented Jul 21, 2017

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.

@dilipkrish
Copy link
Member Author

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.

@benweet
Copy link

benweet commented Jul 26, 2018

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 ?

@dilipkrish
Copy link
Member Author

@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

@benweet
Copy link

benweet commented Aug 4, 2018

@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.

@dilipkrish
Copy link
Member Author

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

@benweet
Copy link

benweet commented Aug 5, 2018

@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);
    ...

@dilipkrish dilipkrish modified the milestones: Someday, 3.0 Aug 6, 2018
@dilipkrish
Copy link
Member Author

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

@dlangerenken
Copy link

any volunteers? :)

@dilipkrish
Copy link
Member Author

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

@dilipkrish dilipkrish removed this from the 3.0 milestone Jul 13, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests