Simon LEDUNOIS
Simon LEDUNOIS

Reputation: 367

Quarkus reactive PostgreSQL request exception does not trigger onFailure

I'm new to quarkus and reactive programming. I'm currently facing an issue with quarkus-reactive-postgresql extension.

I have a list containing events that perform a database update. Each event has to be updated independently (so I don't use a transaction).

Here is my web service:

    @POST
    @Consumes(MediaType.APPLICATION_JSON)
    @Produces(MediaType.APPLICATION_JSON)
    public Uni<JsonObject> synchro(List<Event> events) {
        List<Uni<RowSet<Row>>> queries = new ArrayList<>();
        List<Event> success = new ArrayList<>();
        List<Event> fails = new ArrayList<>();
        for (Event evt : events) {
            // Perform update
            var query = client.preparedQuery("My failing query")
                    .execute(Tuple.of(evt.field));

            // Subscribe. It's ok. Add event to success list
            query.subscribe().with(unused -> success.add(evt));

            // It's failure. Add event to failures
            query.onFailure(throwable -> {
                log.error(String.format("Unable to update event %s", evt.toString()), throwable);
                fails.add(evt);
                return true;
            });

            queries.add(query);
        }

        return Uni.combine().all().unis(queries)
                .combinedWith(ArrayList::new)
                .onItem().transform(list -> new JsonObject()
                        .put("success", success.stream().map(Event::toJson).collect(Collectors.toList()))
                        .put("errors", fails.stream().map(Event::toJson).collect(Collectors.toList()))
                );
    }

Quarkus reactive pg extension throws an exception :

2021-08-06 10:35:37,665 ERROR [io.qua.mut.run.MutinyInfrastructure] (vert.x-eventloop-thread-23) Mutiny had to drop the following exception: io.vertx.pgclient.PgException: { "message": "column \"fake_column\" of relation \"table\" does not exist", "severity": "ERROR", "code": "42703", "position": "18", "file": "analyze.c", "line": "2263", "routine": "transformUpdateTargetList" }

However, .onFailure is not triggered and ever fill my failures list.

Is it a bug or something goes wrong with my code ?

Thanks for your help,

Upvotes: 0

Views: 514

Answers (1)

Nurlan Zhaniyar
Nurlan Zhaniyar

Reputation: 1

In case someone is struggling with this as I did, you can try adding the following code:

.onFailure().transform(ex -> {
    if (ex.getClass().equals(PgException.class)) {
        //here you can do log and stuff
        //in case, you only need to return this boolean you can create 
        //custom handler for your custom exception
        return new CustomException(ex);
    }else{
        return new Exception(ex);
    });    

transform() function somehow lets you return an exception no matter what your method was supposed to return, here is some resource for it: https://cescoffier.github.io/mutiny-doc-sandbox/getting-started/handling-failures

Concerning handling the exception, here is a source that really helped me: https://howtodoinjava.com/resteasy/resteasy-exceptionmapper-example/

Upvotes: 0

Related Questions