You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@camel.apache.org by "Pascal Schumacher (JIRA)" <ji...@apache.org> on 2018/12/07 09:14:00 UTC

[jira] [Comment Edited] (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=16712518#comment-16712518 ] 

Pascal Schumacher edited comment on CAMEL-12949 at 12/7/18 9:13 AM:
--------------------------------------------------------------------

As "Camel in Action, Second Edition" claims that it works, I expected it to have been working in an older camel version.


was (Author: pascalschumacher):
As "Camel in Action, Second Edition" claims that it works I expected it to have been working in an older camel version.

> 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: Improvement
>          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)