2

Consider this handler:

@ConsumeEvent("foo.create")
public Uni<Long> createFoo(FooCreateRequest request) {
    return Uni.createFrom().item(1L)
            .onItem().failWith(() -> new RuntimeException("foo error message"));
}

According to the documentation:

if a reply handler is set the failure is propagated back to the sender via an ReplyException with code ConsumeEvent.FAILURE_CODE and message Exception.toString().

Based on this, I assume that the only way to determine the status of the HTTP response in exception handler is to parse the exception message like this:

@ServerExceptionMapper(ReplyException.class)
public RestResponse<String> mapException(ReplyException e) {
    Response.Status status = getStatus(e.getMessage());
    return RestResponse.status(status);
}

Is there another way to do this, perhaps a way to specify a custom ReplyException.failureCode?

EDIT:

This is how my FooResource looks like:

@Inject
EventBus eventBus;

@POST
@Consumes(MediaType.APPLICATION_JSON)
public Uni<RestResponse<Void>> createFoo(FooCreateRequest request) {
    return eventBus.<Long>request("foo.create", request)
            .onItem().transform(id -> RestResponse.created(...));
}

I used so-called request/reply method described in https://quarkus.io/guides/reactive-event-bus#putting-things-together-bridging-http-and-messages and from what I understood the only information about the exception that I have in FooResource and @ServerExceptionMapper method, is an exception message.

3zmo
  • 73
  • 9
  • You are mixing two different things here. `@ConsumeEvent` is for internal application events (see https://quarkus.io/guides/reactive-event-bus) while @ServerExceptionMapper is for handling failures that occur during processing of a REST request (see https://quarkus.io/guides/resteasy-reactive#exception-mapping) – geoand Mar 17 '22 at 07:30
  • @geoand Thanks for your answer. I think you might minsunderstod me, whats I'm trying to do is to send event to the EventBus from my FooController/FooResource and then receive the result, in case of an exception in the event receiver, I want to return the appropriate HTTP status. I edited my question for clarification. – 3zmo Mar 17 '22 at 08:43
  • Thanks for clarifying. So if I understand correctly, you are asking if there already exists some class is Quarkus that will allow you to determine an HTTP response code from the `ReplyException`? – geoand Mar 17 '22 at 10:05
  • @geoand Yes, my question is if there is a quarkus-way to handle exceptions thrown in the event receiver, if I can access the exception class in lets say FooResource (ReplyException.cause is null), or maybe I shouldn't throw a business exception in the event receiver using Uni.failWith, but return some sort of container such as Result that in case of exception will contain detailed error information – 3zmo Mar 17 '22 at 11:31
  • 1
    I would assume the latter – geoand Mar 17 '22 at 13:12
  • 2
    Yes, because the event bus is something that can be used in a distributed environment. As a consequence, exceptions are not passed as reference, but only the message (string) is conveyed. I'm wondering if we could improve that somehow in the Quarkus layer. – Clement Mar 22 '22 at 07:22
  • Did anything happen with this idea? Would be pretty useful. – languitar Aug 25 '22 at 11:14

0 Answers0