簡體   English   中英

Quarkus 反應式 PostgreSQL 請求異常不會觸發 onFailure

[英]Quarkus reactive PostgreSQL request exception does not trigger onFailure

我是 quarkus 和響應式編程的新手。 我目前面臨 quarkus-reactive-postgresql 擴展的問題。

我有一個包含執行數據庫更新的事件的列表。 每個事件都必須獨立更新(所以我不使用事務)。

這是我的 web 服務:

    @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 反應式 pg 擴展拋出異常:

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" }

但是, .onFailure不會被觸發並且會填滿我的失敗列表。

是錯誤還是我的代碼出了問題?

謝謝你的幫助,

如果有人像我一樣為此苦苦掙扎,您可以嘗試添加以下代碼:

.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

關於處理異常,這里有一個真正幫助我的來源: https://howtodoinjava.com/resteasy/resteasy-exceptionmapper-example/

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM