Search code examples
javaexceptionapache-camelquarkusthrow

Specific exception capture using throw in camel quarkus


I am currently working with Apache Camel in Quarkus and I need to catch an exception of the following type using throw, so that the class that calls it can handle it:

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)

Basically I need to catch the exception personality java.lang.Exception: input byte array has wrong 4 byte trailing unit, so far I know that I can catch the global exception exception, but in this case I would like to catch the one mentioned above . , since for this case it should return a custom error for this case:

This is how I am currently catching my exceptions in the class:

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);
    }

How can I do it here?


Solution

  • I had to do this with an Amazon library once too. It's not very difficult but it's a bit ugly:

    ...
    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());
    }
    

    In this code, we're checking to see if the string in the Exception matches your special case. If so, throw a SpecialException which is where you could throw an exception that indicates that the "4-byte" error has occurred.

    Note that this code is a bit fragile in that you'll need to test if you upgrade the underlying library to make sure that they haven't updated the string. And, it may not be easily internationalized as the string could be different depending on the locale. But, unfortunately, if the Camel libraries are not throwing something more specific there are limited ways to handle this.