camel quarkus 中使用 throw 的具体异常捕获

问题描述 投票:0回答:1

我目前正在 Quarkus 中使用 Apache Camel,我需要使用 throw 捕获以下类型的异常,以便调用它的类可以处理它:

java.lang.Exception: Input byte array has wrong 4-byte ending unit
    at org.tmve.customer.ms.processor.UserProfileProcessorUserIdParamReq.process(UserProfileProcessorUserIdParamReq.java:57)
    at org.apache.camel.support.processor.DelegateSyncProcessor.process(DelegateSyncProcessor.java:65)
    at org.apache.camel.impl.engine.CamelInternalProcessor.process(CamelInternalProcessor.java:392)
    at org.apache.camel.processor.Pipeline$PipelineTask.run(Pipeline.java:104)
    at org.apache.camel.impl.engine.DefaultReactiveExecutor$Worker.schedule(DefaultReactiveExecutor.java:181)
    at org.apache.camel.impl.engine.DefaultReactiveExecutor.scheduleMain(DefaultReactiveExecutor.java:59)
    at org.apache.camel.processor.Pipeline.process(Pipeline.java:165)
    at org.apache.camel.impl.engine.CamelInternalProcessor.process(CamelInternalProcessor.java:392)
    at org.apache.camel.component.platform.http.vertx.VertxPlatformHttpConsumer.lambda$handleRequest$2(VertxPlatformHttpConsumer.java:201)
    at io.vertx.core.impl.ContextBase.lambda$null$0(ContextBase.java:137)
    at io.vertx.core.impl.ContextInternal.dispatch(ContextInternal.java:264)
    at io.vertx.core.impl.ContextBase.lambda$executeBlocking$1(ContextBase.java:135)
    at org.jboss.threads.ContextHandler$1.runWith(ContextHandler.java:18)
    at org.jboss.threads.EnhancedQueueExecutor$Task.run(EnhancedQueueExecutor.java:2449)
    at org.jboss.threads.EnhancedQueueExecutor$ThreadBody.run(EnhancedQueueExecutor.java:1478)
    at org.jboss.threads.DelegatingRunnable.run(DelegatingRunnable.java:29)
    at org.jboss.threads.ThreadLocalResettingRunnable.run(ThreadLocalResettingRunnable.java:29)
    at io.netty.util.concurrent.FastThreadLocalRunnable.run(FastThreadLocalRunnable.java:30)
    at java.base/java.lang.Thread.run(Thread.java:840)

基本上我需要捕获异常个性java.lang.Exception:输入字节数组有错误的4字节尾随单元,到目前为止我知道我可以捕获全局异常异常,但在这种情况下我想抓住上面提到的那个。 ,因为对于这种情况,它应该返回一个自定义错误:

这就是我目前在课堂上捕获异常的方式:

public class UserProfileProcessorUserIdParamReq implements Processor {
    private final IValidationFieldsUserID validationFields;

    public UserProfileProcessorUserIdParamReq() {
        validationFields = new ValidationFieldsUserID();
    }
    @Override
    public void process(Exchange exchange) throws Exception {

        String documentType;
        String documentNumber;
        String userId;
        String correlatorId;
        String tokenInfo;
        String userIdDecoded;

        try {
            userId = exchange.getIn().getHeader("user_id").toString();
            /*correlatorId = exchange.getIn().getHeader("x-correlator").toString(); */
            userIdDecoded = EncryptBase64.decrypt(userId);
            validateUserIdDecrypt(userIdDecoded);
            validationFields.validateUserId(exchange);
            documentType = userIdDecoded.split("-")[0];
            documentNumber = userIdDecoded.split("-")[1];
            validateTokenInfo(exchange,userId);

        } catch (RequiredValueException | NullPointerException re) {
            throw new RequiredValueException("Error validando campos de entrada para UserID");
        }
        catch (NotFoundDataException e) {
            throw new NotFoundDataException(e.getMessage());
        }
        catch (PermissionDeniedException e) {
            throw new PermissionDeniedException(e.getMessage());
        }
        catch (Exception e){
            throw new Exception(e.getMessage());
        }
        correlatorId= Optional.ofNullable(exchange.getIn().getHeader("x-correlator").toString()).map(Object::toString).orElse("");
        exchange.setProperty("isSearchByQueryParam","false");
        exchange.setProperty("userId", userIdDecoded);
        exchange.setProperty("userIdEncrypt", userId);
        exchange.setProperty("correlatorId",correlatorId);
        exchange.setProperty("documentType", documentType);
        exchange.setProperty("documentNumber", documentNumber);
    }

我在这里该怎么做?

java exception apache-camel quarkus throw
1个回答
0
投票

我也曾在亚马逊图书馆做过一次。这不是很困难,但有点难看:

...
catch (PermissionDeniedException e) {
    throw new PermissionDeniedException(e.getMessage());
}
catch (Exception e){
    if( e.getMessage().contains("Input byte array has wrong 4-byte ending unit") )
        throw new SpecialException(e.getMessage());

    throw new Exception(e.getMessage());
}

在此代码中,我们检查异常中的字符串是否与您的特殊情况匹配。如果是这样,请抛出一个

SpecialException
,您可以在其中抛出一个异常,指示发生了“4 字节”错误。

请注意,此代码有点脆弱,因为您需要测试是否升级底层库以确保它们没有更新字符串。而且,它可能不容易国际化,因为字符串可能会根据区域设置而有所不同。但是,不幸的是,如果 Camel 库没有抛出更具体的东西,那么处理这个问题的方法就有限。

© www.soinside.com 2019 - 2024. All rights reserved.