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

Global @Error doesn't work when Controller return is Flux #238

Open
luisospina-sealed opened this issue Oct 26, 2022 · 1 comment · May be fixed by micronaut-projects/micronaut-core#10058
Assignees

Comments

@luisospina-sealed
Copy link

Expected Behavior

When an endpoint does return a Flux<Something> if an error occurs and there's a global @error handler defined it should catch the exception.

Actual Behaviour

Exception is not captured and a 500 error is returned as the response of the HTTP request

Steps To Reproduce

Having the following Global @error

@Error(global = true)
public HttpResponse<JsonError> entityNotFoundHandler(HttpRequest<?> request, EntityNotFoundException exception) {
    JsonError error = new JsonError("Entity Not Found: " + exception.getMessage());
    return HttpResponse.<JsonError>status(HttpStatus.NOT_FOUND, "Not found")
            .body(error);
}

And the following Endpoint inside a Controller

@Get
Flux<Entity> findAllEntities() {
    return service.findAll(); -> This line throws EntityNotFoundException
}

The code in the global @error above is not being executed and the Exception is propagated to the HTTP response which ends up in a 500 status error instead of the 404 specified in the Error handling.

Note that the following works perfect (when Mono is the return type)

@Get
Mono<Entity> findById() {
    return service.findById(); -> This line throws EntityNotFoundException;
}

Environment Information

No response

Example Application

No response

Version

3.6.0

@jeremyg484
Copy link

I have tested and verified that the error handler gets invoked as expected in both Micronaut 3.10.1 and 4.1.5.

Note that in the case of the controller method returning a Flux, the method must also be annotated with @SingleResult, otherwise Micronaut treats it as a chunked response.

When writing a chunked response, the current implementation does not invoke the @Error handler in the case of a Flux.error() (likely because it is difficult to know if any data has already been written to the response body).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants