You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@camel.apache.org by "Claus Ibsen (JIRA)" <ji...@apache.org> on 2018/12/07 06:43:00 UTC
[jira] [Commented] (CAMEL-12949) onWhen predicate with onException
can not bind to method with exception subclass parameter
[ https://issues.apache.org/jira/browse/CAMEL-12949?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=16712396#comment-16712396 ]
Claus Ibsen commented on CAMEL-12949:
-------------------------------------
I replied on the mailing list, its not really a bug as its designed as-is.
> onWhen predicate with onException can not bind to method with exception subclass parameter
> ------------------------------------------------------------------------------------------
>
> Key: CAMEL-12949
> URL: https://issues.apache.org/jira/browse/CAMEL-12949
> Project: Camel
> Issue Type: Bug
> Components: camel-core
> Affects Versions: 2.22.2
> Reporter: Pascal Schumacher
> Priority: Minor
>
> Inspired by this example for "Camel in Action, Second Edition" (page 510):
> {code:java}
> public final class MyHttpUtil {
> public static boolean isIllegalDataError(
> HttpOperationFailedException cause) {
> int code = cause.getStatusCode();
> if (code != 500) {
> return false;
> }
> return "ILLEGAL DATA".equals(cause.getResponseBody().toString());
> }
> }
> onException(HttpOperationFailedException.class)
> .onWhen(bean(MyHttpUtil.class, "isIllegalData"))
> .handled(true)
> .to("file:/rider/files/illegal");
> {code}
> I expected this (simplified test) to pass:
> {code:java}
> public class OnWhenBindMethodWithExceptionSubclassParameterTest extends CamelTestSupport {
> public static class IOExceptionMatcher {
> public static boolean matches(IOException e) {
> // real logic omited
> return true;
> }
> }
> @Override
> protected RouteBuilder createRouteBuilder() throws Exception {
> return new RouteBuilder() {
> public void configure() {
> onException(IOException.class)
> .onWhen(method(IOExceptionMatcher.class))
> .to("mock:ioexception");
> from("seda:start")
> .throwException(new IOException());
> }
> };
> }
> @Test
> public void test() throws Exception {
> getMockEndpoint("mock:ioexception").expectedMessageCount(1);
> template.sendBody("seda:start", "Hello World");
> assertMockEndpointsSatisfied();
> }
> }
> {code}
> but it fails with with:
> {noformat}
> java.lang.AssertionError: mock://ioexception Received message count. Expected: <1> but was: <0>
> {noformat}
> The workaround is to replace the {{IOException}} parameter of {{IOExceptionMatcher#matches}} with an generic {{Exception}}:
> {code:java}
> public static class IOExceptionMatcher {
> public static boolean matches(Exception e) {
> if (e instanceof IOException) {
> return true;
> }
> return false;
> }
> }
> {code}
> but this is not as elegant and more error prone.
--
This message was sent by Atlassian JIRA
(v7.6.3#76005)