2016-08-31 2 views
1

キャメルでは、私のルートに例外がスローされた場合、それを例えばドメイン例外に変換して、テストのためにmock:errorsエンドポイントに渡したいと思います。しかし、私はそれをすることができないようです。Camel:例外を別のクラスに翻訳する

errorHandler(deadLetterChannel("mock:errors").maximumRedeliveries(0)); 

    // even tried this alternative while debugging the processor below 
    // to be sure it is not rethrown 
     onException(Exception.class).process((Exchange e) -> { 
      System.out.println(e.getException()); // breakpoint here 
     }); 

    from("direct:createRequest") 
      .onException(Exception.class) 
       .handled(true) 
       .process(toInvalidRequestException) 
      .end() 
      .unmarshal().json(Jackson, Request.class) 
      .bean(validateRequest); 

------------- 

private static Processor toInvalidRequestException = exchange -> { 
    Exception e = exchange.getProperty(Exchange.EXCEPTION_CAUGHT, Exception.class); 
    throw new InvalidRequestException(format("Barcode request is not valid or has wrong format: %s", 
      getRootCauseMessage(e)), e); 
}; 

上記のルートでは、任意の例外(たとえば、検証例外、またはJacksonの解析例外)がスローされる可能性があります。私はonException節を使ってそれを傍受し、それをメッセージの詳細でInvalidRequestExceptionに変換したいと思う。これは動作しますが、この例外が実際にmock:errorsエンドポイントを使用してテストでスローされたと主張したいと思います。ただし、例外が変換プロセッサから伝播されないため、そのエンドポイントは常に空です。

handled(true)continued(true)と組み合わせて試しましたが、わかりません。

答えて

0

エラーハンドラがデフォルトの動作を定義します。各エラーハンドラには、最初に照合しようとする例外ポリシー定義のリスト(onException DSLで作成)があります。一致するものが見つからない場合にのみ、errorHandlerのデフォルト値が適用されます。また、onExceptionパイプラインから例外をスローしているので、基本的にエラー処理フロー全体に失敗しています。ラップ、例外を再スローするより

より良いオプションは、/適切なタイプのメッセージに例外を変換するでしょう、あなたのルートビルダーでグローバルonExceptionを持っているし、メッセージのタイプに基づいてエラー処理を行うことであろう例外タイプの代わりにコンテンツ。例えば、ここで私は私の現在のプロジェクトで使用している便利なエラーメッセージの例

です:exchange.getException()が空である理由については

public class DeadLetterChannelMessage { 
    private String timestamp = Times.nowInUtc().toString(); 
    private String exchangeId; 
    private String originalMessageBody; 
    private Map<String, Object> headers; 
    private String fromRouteId; 
    private String errorMessage; 
    private String stackTrace; 

    @RequiredByThirdPartyFramework("jackson") 
    private DeadLetterChannelMessage() { 
    } 

    @SuppressWarnings("ThrowableResultOfMethodCallIgnored") 
    public DeadLetterChannelMessage(Exchange e) { 
    exchangeId = e.getExchangeId(); 
    originalMessageBody = e.getIn().getBody(String.class); 
    headers = Collections.unmodifiableMap(e.getIn().getHeaders()); 
    fromRouteId = e.getFromRouteId(); 

    Optional.ofNullable(e.getProperty(Exchange.EXCEPTION_CAUGHT, Exception.class)).ifPresent(throwable -> { 
     errorMessage = throwable.getMessage(); 
     stackTrace = ExceptionUtils.getStackTrace(throwable); 
    }); 
    } 

    // getters 
} 

- あなたはスローを取得するためにExchange.EXCEPTION_CAUGHTプロパティを使用する必要がありますあなたが扱って例外マーキングしている場合は例外:

private static Processor toInvalidRequestException = exchange -> { 
    Exception e = exchange.getProperty(Exchange.EXCEPTION_CAUGHT, Exception.class); 
    throw new InvalidRequestException(
      format("Barcode request is not valid or has wrong format: %s", 
        getRootCauseMessage(e)), e); 
}; 
+0

をしかし、私のコードは、同じ作品は、例外がありますが、新しい例外はErrorHandlerのこのsubrouteからと 'モックに再スローされていません。errors'終点。 – redhead

+0

私がこのように扱ったとしても(真)、「疑似:エラー」は空です。私は質問を更新します。 – redhead

+0

これは、 'onException'が' errorHandler'の定義をオーバーライドしているからです。 –

関連する問題