micronaut-projects / micronaut-core

Micronaut Application Framework
http://micronaut.io
Apache License 2.0
6.05k stars 1.06k forks source link

@Error( status = HttpStatus.NOT_ACCEPTABLE, global = true) does not work #6929

Open siiinc opened 2 years ago

siiinc commented 2 years ago

Expected Behavior

@Error( status = HttpStatus.NOT_ACCEPTABLE, global = true) annotated method should be run if a request is made with in correct Accept header

@Error( status = HttpStatus.NOT_ACCEPTABLE, global = true)
    public HttpResponse<?> notAcceptable(HttpRequest<?> request) {
        return HttpResponse.serverError("Not acceptable").status(HttpStatus.NOT_ACCEPTABLE);
    }

Actual Behaviour

The method is not used and the default error response is returned

{
    "message": "Not Acceptable",
    "_links": {
        "self": {
            "href": "/test/id",
            "templated": false
        }
    },
    "_embedded": {
        "errors": [
            {
                "message": "Specified Accept Types [image/pjpeg] not supported. Supported types: [application/json]"
            }
        ]
    }
}

Steps To Reproduce

  1. Using Launch I made a simple example with version 3.3.3, Java, Maven
  2. added a simple controller
  3. run in IDE
  4. curl --location --request GET 'localhost:8080/test/someid' works as expected
  5. curl --location --request GET 'localhost:8080/testbad/someid' works as expected ... @Error( status = HttpStatus.NOT_FOUND, global = true) annotated method is hit
  6. curl --location --request GET 'localhost:8080/test/someid' --header 'Accept: image/jpeg' does not work as expected. Default error page/view is returned {"message":"Not Acceptable","_embedded":{"errors":[{"message":"Specified Accept Types [image/jpeg] not supported. Supported types: [application/json]"}]},"_links":{"self":{"href":"/test/someid","templated":false}}}

Environment Information

MacOS Java 11 micronaut 3.3.3 and 2.7.x were tested with same results

Example Application

https://github.com/siiinc/micronaut-error-example.git

Version

3.3.3

jameskleeh commented 2 years ago

So in this case the route is found, but it is not used because it does not produce what the client is requesting.

I agree that it doesn't make sense in this case because the default response is also not what the client is requesting

siiinc commented 2 years ago

So in this case the route is found, but it is not used because it does not produce what the client is requesting.

I agree that it doesn't make sense in this case because the default response is also not what the client is requesting Is there a better option to override the default micronaut error page/view in all error cases? Either want the service to respond according to the controller or with my own standard error response in all other cases.

jameskleeh commented 2 years ago

@siiinc The ErrorResponseProcessor API allows you control over the response when an error response is being sent. You can override the default implementation with your own