使用camel

时间:2015-08-12 02:47:51

标签: junit exception-handling apache-camel

我创建了一个自定义异常来处理我的业务场景需要在验证失败时将此异常推回给调用者

路由器呼叫定义

from(jettyEndpoint_incSubsCnt)
 .doTry().bean(MyProcessor.class).doCatch(MyException.class).bean(MyThrowableException.class);

我的Junit实施

try{template.sendBody(new Gson().toJson(myrequest, MyRequest.class)); }catch(Exception e){ e.printStackTrace(); }

我在MyProcessor中抛出了一个customException

if(errors.hasErrors()){
        throw new MyException(ChangeSub_routerClli,errors.getAllErrors().get(0).getDefaultMessage());
    }

我的自定义异常类

public class MyException extends Exception{

private int exceptionID;

public MyException(int exceptionID) {
    super();
    this.exceptionID = exceptionID;
}

public MyException() {

}


public MyException(int exceptionID,String message) {
    super(message);
    this.exceptionID = exceptionID;
}


public MyException(int exceptionID,String message,Throwable exception){
    super(message,exception);
    this.exceptionID = exceptionID;
}


@Override 
public String toString() { 
    return super.toString(); 
} 

@Override 
public String getMessage() { 
    return super.getMessage() + " for Exception ID :" + exceptionID; 
}


public int getExceptionID() {
    return exceptionID;
}}

我的throwable类实现

public class MyThrowableException implements Processor{
Logger log = Logger.getLogger(MyThrowableException.class);
@Override
public void process(Exchange exchange) throws Exception {
    // TODO Auto-generated method stub
    Exception exception = (Exception) exchange.getProperty(Exchange.EXCEPTION_CAUGHT);

    log.error(exception);

    throw exception;
}}

我的日志

   Logs :::[qtp2078517710-16 - myURL?exchangePattern=InOut] ERROR MyException  - MyException: routerClli is required. for Exception ID :200

我无法将异常退回给调用者

1 个答案:

答案 0 :(得分:0)

两件事:

  1. 您在catch区块的末尾错过了.end()
  2. 如果你想"重新投掷"将其传播给调用者的异常,您需要将异常标记为未处理:.handled(false)
  3. 查看try-catch上的文档。