onException onWhen behavior

classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

onException onWhen behavior

Pascal Schumacher
Hi everybody,

I expected exceptionMessage() of onWhen() for a specific onException()
to refer to the message of the exception instance in the exception
hierarchy. E.g. I expected this to work:

public void configure() {
     onException(IOException.class)
         .onWhen(exceptionMessage().contains("root exception message"))
         .to("mock:ioexception");

     from("seda:start")
         .throwException(new RuntimeException("leaf exception message",
new IOException("root exception message")));
}

Then I tried a custom predicate instead:

public void configure() {
     onException(IOException.class)
         .onWhen(method(IOExceptionMatcher.class))
         .to("mock:ioexception");

     from("seda:start")
         .throwException(new RuntimeException("leaf exception message",
new IOException("root exception message")));
}

public static class IOExceptionMatcher {
     public static boolean matches(IOException e) {
         return e.getMessage().contains("root exception message");
     }
}

This also did not work. Therefore I tried:

public void configure() {
     onException(IOException.class)
         .onWhen(method(ExceptionMatcher.class))
         .to("mock:ioexception");

     from("seda:start")
         .throwException(new RuntimeException("leaf exception message",
new IOException("root exception message")));
}

public static class ExceptionMatcher {
     public static boolean matches(Exception e) {
         return e.getMessage().contains("root exception message");
     }
}

This also did not work. This "workaround" does work:

public void configure() {
     onException(IOException.class)
         .onWhen(method(ExceptionMatcher.class))
         .to("mock:ioexception");

     from("seda:start")
         .throwException(new RuntimeException("leaf exception message",
new IOException("root exception message")));
}

public static class ExceptionMatcher {
     public static boolean matches(Exception e) {
         return Throwables.getRootCause(e).getMessage().contains("root
exception message");
     }
}

Is any of this supposed to work, or are my expectations wrong?

Thanks,
Pascal



Reply | Threaded
Open this post in threaded view
|

Re: onException onWhen behavior

Claus Ibsen-2
Hi

This is not supported, that code uses the simple ${exception.message}
which invokes the getMessage method on the top exception.

On Sun, Dec 2, 2018 at 12:50 PM Pascal Schumacher
<[hidden email]> wrote:

>
> Hi everybody,
>
> I expected exceptionMessage() of onWhen() for a specific onException()
> to refer to the message of the exception instance in the exception
> hierarchy. E.g. I expected this to work:
>
> public void configure() {
>      onException(IOException.class)
>          .onWhen(exceptionMessage().contains("root exception message"))
>          .to("mock:ioexception");
>
>      from("seda:start")
>          .throwException(new RuntimeException("leaf exception message",
> new IOException("root exception message")));
> }
>
> Then I tried a custom predicate instead:
>
> public void configure() {
>      onException(IOException.class)
>          .onWhen(method(IOExceptionMatcher.class))
>          .to("mock:ioexception");
>
>      from("seda:start")
>          .throwException(new RuntimeException("leaf exception message",
> new IOException("root exception message")));
> }
>
> public static class IOExceptionMatcher {
>      public static boolean matches(IOException e) {
>          return e.getMessage().contains("root exception message");
>      }
> }
>
> This also did not work. Therefore I tried:
>
> public void configure() {
>      onException(IOException.class)
>          .onWhen(method(ExceptionMatcher.class))
>          .to("mock:ioexception");
>
>      from("seda:start")
>          .throwException(new RuntimeException("leaf exception message",
> new IOException("root exception message")));
> }
>
> public static class ExceptionMatcher {
>      public static boolean matches(Exception e) {
>          return e.getMessage().contains("root exception message");
>      }
> }
>
> This also did not work. This "workaround" does work:
>
> public void configure() {
>      onException(IOException.class)
>          .onWhen(method(ExceptionMatcher.class))
>          .to("mock:ioexception");
>
>      from("seda:start")
>          .throwException(new RuntimeException("leaf exception message",
> new IOException("root exception message")));
> }
>
> public static class ExceptionMatcher {
>      public static boolean matches(Exception e) {
>          return Throwables.getRootCause(e).getMessage().contains("root
> exception message");
>      }
> }
>
> Is any of this supposed to work, or are my expectations wrong?
>
> Thanks,
> Pascal
>
>
>


--
Claus Ibsen
-----------------
http://davsclaus.com @davsclaus
Camel in Action 2: https://www.manning.com/ibsen2