You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@camel.apache.org by Robert Huffman <ro...@gmail.com> on 2017/01/03 18:44:19 UTC

Handling errors

I'm having trouble understanding how to write and use an error handler. The
use case is testing a custom Camel component. I would like to have an error
handler that just collects any exceptions thrown by the endpoint's producer.

I somewhat naively assumed this would be simple. I thought the Camel
framework must catch any exception, add it the exchange, and invoke the
handler's process method. That appears not to be the case.

Below is my attempt. The test is set up so my the producer will throw an
exception. To my surprise, the CaptureErrorHandler.process method is being
invoked twice. my component's producer is never invoked.

But If I remove usage of my my error handler from the RouteBuilder, the
producer is invoked, and it throws an exception that is logged by the
DefaultErrorHandler.

Clearly, I don't understand how error handles are supposed to be used. Can
someone enlighten me, or point me to more detailed documentation? The Camel
documentation for ErrorHandler is pretty light on details. Alternatively,
maybe there's a simpler approach I can take for testing my component.


The RouteBuilder's configure method looks like this:

public void configure() throws Exception {
  errorHandler(new CaptureErrorHandlerBuilder());
  from(destinations)
      .process(new Counter())
      .to("foo:endpoint");
}


And the CaptureErrorHandlerBuilder and CaptureErrorHandler looks like this:

private class CaptureErrorHandlerBuilder extends ErrorHandlerBuilderSupport
{

  @Override
  public boolean supportTransacted() {
    return false;
  }

  @Override
  public ErrorHandlerBuilder cloneBuilder() {
    return new CaptureErrorHandlerBuilder();
  }

  @Override
  public Processor createErrorHandler(RouteContext routeContext, Processor
processor) throws Exception {
    CaptureErrorHandler handler = new CaptureErrorHandler();
    configure(routeContext, handler);
    return handler;
  }
}

private class CaptureErrorHandler implements ErrorHandler {
  @Override
  public void process(Exchange exchange) throws Exception {
    Exception e = exchange.getException();

    // For now, just log the exception

    if (e != null) {
      e.printStackTrace();

    }

  }
}

Re: Handling errors

Posted by Robert Huffman <ro...@gmail.com>.
To close this discussion, Claus's solution, along with try, catch and
process, clauses, worked for me.


The route looks like this:

from(destinations)
    .doTry()
    .to("foo:bar")
    .doCatch(Exception.class)
    .process((exchange) -> handleException(exchange));


And the method to add the exception to a collection looks like this:

private void handleException(Exchange exchange) {
  Exception e = exchange.getException();
  if (e == null) {
    // If another handler marked the exception as handled, we have to get
the
    // original exception from a property.
    // See
http://camel.apache.org/why-is-the-exception-null-when-i-use-onexception.html
    e = exchange.getProperty(Exchange.EXCEPTION_CAUGHT, Exception.class);
  }
  if (e != null) {
    exceptions.add(e);
  }
}


Thanks for the help.

On Tue, Jan 3, 2017 at 2:31 PM, Robert Huffman <ro...@gmail.com>
wrote:

> Ah, nice. I did not see that. Thanks for pointing it out. (And I'll check
> out that chapter, too.)
>
>
> On Tue, Jan 3, 2017 at 1:15 PM, Claus Ibsen <cl...@gmail.com> wrote:
>
>> Hi
>>
>> There is this FAQ
>> http://camel.apache.org/why-is-the-exception-null-when-i-use
>> -onexception.html
>>
>> On Tue, Jan 3, 2017 at 9:31 PM, Robert Huffman <ro...@gmail.com>
>> wrote:
>> > Well, of course I read that, but all I want is access to the actual
>> > Exception objects in my test. I can add the try/catch clauses, but as
>> > nearly as I can tell the only thing you can do with the catch is route
>> to
>> > another endpoint. Isn't there a simple way to get at the underlying
>> > exceptions?
>> >
>> >
>> > On Tue, Jan 3, 2017 at 10:57 AM, Claus Ibsen <cl...@gmail.com>
>> wrote:
>> >
>> >> Hi
>> >>
>> >> You should not build your own, but use what is OOTB
>> >> http://camel.apache.org/error-handling-in-camel.html
>> >>
>> >> If you have Camel in Action book then it has a full chapter devoted to
>> >> error handling.
>> >>
>> >>
>> >>
>> >> On Tue, Jan 3, 2017 at 7:44 PM, Robert Huffman <
>> robert.huffman@gmail.com>
>> >> wrote:
>> >> > I'm having trouble understanding how to write and use an error
>> handler.
>> >> The
>> >> > use case is testing a custom Camel component. I would like to have an
>> >> error
>> >> > handler that just collects any exceptions thrown by the endpoint's
>> >> producer.
>> >> >
>> >> > I somewhat naively assumed this would be simple. I thought the Camel
>> >> > framework must catch any exception, add it the exchange, and invoke
>> the
>> >> > handler's process method. That appears not to be the case.
>> >> >
>> >> > Below is my attempt. The test is set up so my the producer will
>> throw an
>> >> > exception. To my surprise, the CaptureErrorHandler.process method is
>> >> being
>> >> > invoked twice. my component's producer is never invoked.
>> >> >
>> >> > But If I remove usage of my my error handler from the RouteBuilder,
>> the
>> >> > producer is invoked, and it throws an exception that is logged by the
>> >> > DefaultErrorHandler.
>> >> >
>> >> > Clearly, I don't understand how error handles are supposed to be
>> used.
>> >> Can
>> >> > someone enlighten me, or point me to more detailed documentation? The
>> >> Camel
>> >> > documentation for ErrorHandler is pretty light on details.
>> Alternatively,
>> >> > maybe there's a simpler approach I can take for testing my component.
>> >> >
>> >> >
>> >> > The RouteBuilder's configure method looks like this:
>> >> >
>> >> > public void configure() throws Exception {
>> >> >   errorHandler(new CaptureErrorHandlerBuilder());
>> >> >   from(destinations)
>> >> >       .process(new Counter())
>> >> >       .to("foo:endpoint");
>> >> > }
>> >> >
>> >> >
>> >> > And the CaptureErrorHandlerBuilder and CaptureErrorHandler looks like
>> >> this:
>> >> >
>> >> > private class CaptureErrorHandlerBuilder extends
>> >> ErrorHandlerBuilderSupport
>> >> > {
>> >> >
>> >> >   @Override
>> >> >   public boolean supportTransacted() {
>> >> >     return false;
>> >> >   }
>> >> >
>> >> >   @Override
>> >> >   public ErrorHandlerBuilder cloneBuilder() {
>> >> >     return new CaptureErrorHandlerBuilder();
>> >> >   }
>> >> >
>> >> >   @Override
>> >> >   public Processor createErrorHandler(RouteContext routeContext,
>> >> Processor
>> >> > processor) throws Exception {
>> >> >     CaptureErrorHandler handler = new CaptureErrorHandler();
>> >> >     configure(routeContext, handler);
>> >> >     return handler;
>> >> >   }
>> >> > }
>> >> >
>> >> > private class CaptureErrorHandler implements ErrorHandler {
>> >> >   @Override
>> >> >   public void process(Exchange exchange) throws Exception {
>> >> >     Exception e = exchange.getException();
>> >> >
>> >> >     // For now, just log the exception
>> >> >
>> >> >     if (e != null) {
>> >> >       e.printStackTrace();
>> >> >
>> >> >     }
>> >> >
>> >> >   }
>> >> > }
>> >>
>> >>
>> >>
>> >> --
>> >> Claus Ibsen
>> >> -----------------
>> >> http://davsclaus.com @davsclaus
>> >> Camel in Action 2: https://www.manning.com/ibsen2
>> >>
>>
>>
>>
>> --
>> Claus Ibsen
>> -----------------
>> http://davsclaus.com @davsclaus
>> Camel in Action 2: https://www.manning.com/ibsen2
>>
>
>

Re: Handling errors

Posted by Robert Huffman <ro...@gmail.com>.
Ah, nice. I did not see that. Thanks for pointing it out. (And I'll check
out that chapter, too.)


On Tue, Jan 3, 2017 at 1:15 PM, Claus Ibsen <cl...@gmail.com> wrote:

> Hi
>
> There is this FAQ
> http://camel.apache.org/why-is-the-exception-null-when-i-
> use-onexception.html
>
> On Tue, Jan 3, 2017 at 9:31 PM, Robert Huffman <ro...@gmail.com>
> wrote:
> > Well, of course I read that, but all I want is access to the actual
> > Exception objects in my test. I can add the try/catch clauses, but as
> > nearly as I can tell the only thing you can do with the catch is route to
> > another endpoint. Isn't there a simple way to get at the underlying
> > exceptions?
> >
> >
> > On Tue, Jan 3, 2017 at 10:57 AM, Claus Ibsen <cl...@gmail.com>
> wrote:
> >
> >> Hi
> >>
> >> You should not build your own, but use what is OOTB
> >> http://camel.apache.org/error-handling-in-camel.html
> >>
> >> If you have Camel in Action book then it has a full chapter devoted to
> >> error handling.
> >>
> >>
> >>
> >> On Tue, Jan 3, 2017 at 7:44 PM, Robert Huffman <
> robert.huffman@gmail.com>
> >> wrote:
> >> > I'm having trouble understanding how to write and use an error
> handler.
> >> The
> >> > use case is testing a custom Camel component. I would like to have an
> >> error
> >> > handler that just collects any exceptions thrown by the endpoint's
> >> producer.
> >> >
> >> > I somewhat naively assumed this would be simple. I thought the Camel
> >> > framework must catch any exception, add it the exchange, and invoke
> the
> >> > handler's process method. That appears not to be the case.
> >> >
> >> > Below is my attempt. The test is set up so my the producer will throw
> an
> >> > exception. To my surprise, the CaptureErrorHandler.process method is
> >> being
> >> > invoked twice. my component's producer is never invoked.
> >> >
> >> > But If I remove usage of my my error handler from the RouteBuilder,
> the
> >> > producer is invoked, and it throws an exception that is logged by the
> >> > DefaultErrorHandler.
> >> >
> >> > Clearly, I don't understand how error handles are supposed to be used.
> >> Can
> >> > someone enlighten me, or point me to more detailed documentation? The
> >> Camel
> >> > documentation for ErrorHandler is pretty light on details.
> Alternatively,
> >> > maybe there's a simpler approach I can take for testing my component.
> >> >
> >> >
> >> > The RouteBuilder's configure method looks like this:
> >> >
> >> > public void configure() throws Exception {
> >> >   errorHandler(new CaptureErrorHandlerBuilder());
> >> >   from(destinations)
> >> >       .process(new Counter())
> >> >       .to("foo:endpoint");
> >> > }
> >> >
> >> >
> >> > And the CaptureErrorHandlerBuilder and CaptureErrorHandler looks like
> >> this:
> >> >
> >> > private class CaptureErrorHandlerBuilder extends
> >> ErrorHandlerBuilderSupport
> >> > {
> >> >
> >> >   @Override
> >> >   public boolean supportTransacted() {
> >> >     return false;
> >> >   }
> >> >
> >> >   @Override
> >> >   public ErrorHandlerBuilder cloneBuilder() {
> >> >     return new CaptureErrorHandlerBuilder();
> >> >   }
> >> >
> >> >   @Override
> >> >   public Processor createErrorHandler(RouteContext routeContext,
> >> Processor
> >> > processor) throws Exception {
> >> >     CaptureErrorHandler handler = new CaptureErrorHandler();
> >> >     configure(routeContext, handler);
> >> >     return handler;
> >> >   }
> >> > }
> >> >
> >> > private class CaptureErrorHandler implements ErrorHandler {
> >> >   @Override
> >> >   public void process(Exchange exchange) throws Exception {
> >> >     Exception e = exchange.getException();
> >> >
> >> >     // For now, just log the exception
> >> >
> >> >     if (e != null) {
> >> >       e.printStackTrace();
> >> >
> >> >     }
> >> >
> >> >   }
> >> > }
> >>
> >>
> >>
> >> --
> >> Claus Ibsen
> >> -----------------
> >> http://davsclaus.com @davsclaus
> >> Camel in Action 2: https://www.manning.com/ibsen2
> >>
>
>
>
> --
> Claus Ibsen
> -----------------
> http://davsclaus.com @davsclaus
> Camel in Action 2: https://www.manning.com/ibsen2
>

Re: Handling errors

Posted by Claus Ibsen <cl...@gmail.com>.
Hi

There is this FAQ
http://camel.apache.org/why-is-the-exception-null-when-i-use-onexception.html

On Tue, Jan 3, 2017 at 9:31 PM, Robert Huffman <ro...@gmail.com> wrote:
> Well, of course I read that, but all I want is access to the actual
> Exception objects in my test. I can add the try/catch clauses, but as
> nearly as I can tell the only thing you can do with the catch is route to
> another endpoint. Isn't there a simple way to get at the underlying
> exceptions?
>
>
> On Tue, Jan 3, 2017 at 10:57 AM, Claus Ibsen <cl...@gmail.com> wrote:
>
>> Hi
>>
>> You should not build your own, but use what is OOTB
>> http://camel.apache.org/error-handling-in-camel.html
>>
>> If you have Camel in Action book then it has a full chapter devoted to
>> error handling.
>>
>>
>>
>> On Tue, Jan 3, 2017 at 7:44 PM, Robert Huffman <ro...@gmail.com>
>> wrote:
>> > I'm having trouble understanding how to write and use an error handler.
>> The
>> > use case is testing a custom Camel component. I would like to have an
>> error
>> > handler that just collects any exceptions thrown by the endpoint's
>> producer.
>> >
>> > I somewhat naively assumed this would be simple. I thought the Camel
>> > framework must catch any exception, add it the exchange, and invoke the
>> > handler's process method. That appears not to be the case.
>> >
>> > Below is my attempt. The test is set up so my the producer will throw an
>> > exception. To my surprise, the CaptureErrorHandler.process method is
>> being
>> > invoked twice. my component's producer is never invoked.
>> >
>> > But If I remove usage of my my error handler from the RouteBuilder, the
>> > producer is invoked, and it throws an exception that is logged by the
>> > DefaultErrorHandler.
>> >
>> > Clearly, I don't understand how error handles are supposed to be used.
>> Can
>> > someone enlighten me, or point me to more detailed documentation? The
>> Camel
>> > documentation for ErrorHandler is pretty light on details. Alternatively,
>> > maybe there's a simpler approach I can take for testing my component.
>> >
>> >
>> > The RouteBuilder's configure method looks like this:
>> >
>> > public void configure() throws Exception {
>> >   errorHandler(new CaptureErrorHandlerBuilder());
>> >   from(destinations)
>> >       .process(new Counter())
>> >       .to("foo:endpoint");
>> > }
>> >
>> >
>> > And the CaptureErrorHandlerBuilder and CaptureErrorHandler looks like
>> this:
>> >
>> > private class CaptureErrorHandlerBuilder extends
>> ErrorHandlerBuilderSupport
>> > {
>> >
>> >   @Override
>> >   public boolean supportTransacted() {
>> >     return false;
>> >   }
>> >
>> >   @Override
>> >   public ErrorHandlerBuilder cloneBuilder() {
>> >     return new CaptureErrorHandlerBuilder();
>> >   }
>> >
>> >   @Override
>> >   public Processor createErrorHandler(RouteContext routeContext,
>> Processor
>> > processor) throws Exception {
>> >     CaptureErrorHandler handler = new CaptureErrorHandler();
>> >     configure(routeContext, handler);
>> >     return handler;
>> >   }
>> > }
>> >
>> > private class CaptureErrorHandler implements ErrorHandler {
>> >   @Override
>> >   public void process(Exchange exchange) throws Exception {
>> >     Exception e = exchange.getException();
>> >
>> >     // For now, just log the exception
>> >
>> >     if (e != null) {
>> >       e.printStackTrace();
>> >
>> >     }
>> >
>> >   }
>> > }
>>
>>
>>
>> --
>> Claus Ibsen
>> -----------------
>> http://davsclaus.com @davsclaus
>> Camel in Action 2: https://www.manning.com/ibsen2
>>



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

Re: Handling errors

Posted by souciance <so...@gmail.com>.
If you don't care about a particular Exception why don't you just have a
global error handler that simply routes the exchange to some error handler
route. There you can log whatever you want or stop the flow. Here are some
ways to access various properties.

.setHeader("CamelMessageId", simple("${id}"))
.setHeader("CamelContextId", simple("${camelId}"))
.setHeader("CamelCreatedTimestamp",
simple("${property.CamelCreatedTimestamp}"))
.setHeader("CamelExceptionCaught", simple("Exception Object:
${property.CamelExceptionCaught}"))
.setHeader("CamelStacktrace", simple("${exception.stacktrace}"))
.setHeader("ExceptionMessage", simple("${exception.message}"))
.setHeader("CamelFailureEndpoint",
simple("${property.CamelFailureEndpoint}"))
.setHeader("CamelFailureRouteId", simple("${property.CamelFailureRouteId}"))


On Tue, Jan 3, 2017 at 9:49 PM, Quinn Stevenson [via Camel] <
ml-node+s465427n5792116h59@n5.nabble.com> wrote:

> Have you tried getting the exception from the exchange?
>
> https://camel.apache.org/maven/current/camel-core/
> apidocs/org/apache/camel/Exchange.html#getException() <
> https://camel.apache.org/maven/current/camel-core/
> apidocs/org/apache/camel/Exchange.html#getException()>
>
>
>
> > On Jan 3, 2017, at 1:31 PM, Robert Huffman <[hidden email]
> <http:///user/SendEmail.jtp?type=node&node=5792116&i=0>> wrote:
> >
> > Well, of course I read that, but all I want is access to the actual
> > Exception objects in my test. I can add the try/catch clauses, but as
> > nearly as I can tell the only thing you can do with the catch is route
> to
> > another endpoint. Isn't there a simple way to get at the underlying
> > exceptions?
> >
> >
> > On Tue, Jan 3, 2017 at 10:57 AM, Claus Ibsen <[hidden email]
> <http:///user/SendEmail.jtp?type=node&node=5792116&i=1>> wrote:
> >
> >> Hi
> >>
> >> You should not build your own, but use what is OOTB
> >> http://camel.apache.org/error-handling-in-camel.html
> >>
> >> If you have Camel in Action book then it has a full chapter devoted to
> >> error handling.
> >>
> >>
> >>
> >> On Tue, Jan 3, 2017 at 7:44 PM, Robert Huffman <[hidden email]
> <http:///user/SendEmail.jtp?type=node&node=5792116&i=2>>
> >> wrote:
> >>> I'm having trouble understanding how to write and use an error
> handler.
> >> The
> >>> use case is testing a custom Camel component. I would like to have an
> >> error
> >>> handler that just collects any exceptions thrown by the endpoint's
> >> producer.
> >>>
> >>> I somewhat naively assumed this would be simple. I thought the Camel
> >>> framework must catch any exception, add it the exchange, and invoke
> the
> >>> handler's process method. That appears not to be the case.
> >>>
> >>> Below is my attempt. The test is set up so my the producer will throw
> an
> >>> exception. To my surprise, the CaptureErrorHandler.process method is
> >> being
> >>> invoked twice. my component's producer is never invoked.
> >>>
> >>> But If I remove usage of my my error handler from the RouteBuilder,
> the
> >>> producer is invoked, and it throws an exception that is logged by the
> >>> DefaultErrorHandler.
> >>>
> >>> Clearly, I don't understand how error handles are supposed to be used.
> >> Can
> >>> someone enlighten me, or point me to more detailed documentation? The
> >> Camel
> >>> documentation for ErrorHandler is pretty light on details.
> Alternatively,
> >>> maybe there's a simpler approach I can take for testing my component.
> >>>
> >>>
> >>> The RouteBuilder's configure method looks like this:
> >>>
> >>> public void configure() throws Exception {
> >>>  errorHandler(new CaptureErrorHandlerBuilder());
> >>>  from(destinations)
> >>>      .process(new Counter())
> >>>      .to("foo:endpoint");
> >>> }
> >>>
> >>>
> >>> And the CaptureErrorHandlerBuilder and CaptureErrorHandler looks like
> >> this:
> >>>
> >>> private class CaptureErrorHandlerBuilder extends
> >> ErrorHandlerBuilderSupport
> >>> {
> >>>
> >>>  @Override
> >>>  public boolean supportTransacted() {
> >>>    return false;
> >>>  }
> >>>
> >>>  @Override
> >>>  public ErrorHandlerBuilder cloneBuilder() {
> >>>    return new CaptureErrorHandlerBuilder();
> >>>  }
> >>>
> >>>  @Override
> >>>  public Processor createErrorHandler(RouteContext routeContext,
> >> Processor
> >>> processor) throws Exception {
> >>>    CaptureErrorHandler handler = new CaptureErrorHandler();
> >>>    configure(routeContext, handler);
> >>>    return handler;
> >>>  }
> >>> }
> >>>
> >>> private class CaptureErrorHandler implements ErrorHandler {
> >>>  @Override
> >>>  public void process(Exchange exchange) throws Exception {
> >>>    Exception e = exchange.getException();
> >>>
> >>>    // For now, just log the exception
> >>>
> >>>    if (e != null) {
> >>>      e.printStackTrace();
> >>>
> >>>    }
> >>>
> >>>  }
> >>> }
> >>
> >>
> >>
> >> --
> >> Claus Ibsen
> >> -----------------
> >> http://davsclaus.com @davsclaus
> >> Camel in Action 2: https://www.manning.com/ibsen2
> >>
>
>
>
> ------------------------------
> If you reply to this email, your message will be added to the discussion
> below:
> http://camel.465427.n5.nabble.com/Handling-errors-tp5792112p5792116.html
> To start a new topic under Camel - Users, email
> ml-node+s465427n465428h31@n5.nabble.com
> To unsubscribe from Camel - Users, click here
> <http://camel.465427.n5.nabble.com/template/NamlServlet.jtp?macro=unsubscribe_by_code&node=465428&code=c291Y2lhbmNlLmVxZGFtLnJhc2h0aUBnbWFpbC5jb218NDY1NDI4fDE1MzI5MTE2NTY=>
> .
> NAML
> <http://camel.465427.n5.nabble.com/template/NamlServlet.jtp?macro=macro_viewer&id=instant_html%21nabble%3Aemail.naml&base=nabble.naml.namespaces.BasicNamespace-nabble.view.web.template.NabbleNamespace-nabble.view.web.template.NodeNamespace&breadcrumbs=notify_subscribers%21nabble%3Aemail.naml-instant_emails%21nabble%3Aemail.naml-send_instant_email%21nabble%3Aemail.naml>
>




--
View this message in context: http://camel.465427.n5.nabble.com/Handling-errors-tp5792112p5792117.html
Sent from the Camel - Users mailing list archive at Nabble.com.

Re: Handling errors

Posted by Quinn Stevenson <qu...@pronoia-solutions.com>.
Have you tried getting the exception from the exchange? 

https://camel.apache.org/maven/current/camel-core/apidocs/org/apache/camel/Exchange.html#getException() <https://camel.apache.org/maven/current/camel-core/apidocs/org/apache/camel/Exchange.html#getException()>



> On Jan 3, 2017, at 1:31 PM, Robert Huffman <ro...@gmail.com> wrote:
> 
> Well, of course I read that, but all I want is access to the actual
> Exception objects in my test. I can add the try/catch clauses, but as
> nearly as I can tell the only thing you can do with the catch is route to
> another endpoint. Isn't there a simple way to get at the underlying
> exceptions?
> 
> 
> On Tue, Jan 3, 2017 at 10:57 AM, Claus Ibsen <cl...@gmail.com> wrote:
> 
>> Hi
>> 
>> You should not build your own, but use what is OOTB
>> http://camel.apache.org/error-handling-in-camel.html
>> 
>> If you have Camel in Action book then it has a full chapter devoted to
>> error handling.
>> 
>> 
>> 
>> On Tue, Jan 3, 2017 at 7:44 PM, Robert Huffman <ro...@gmail.com>
>> wrote:
>>> I'm having trouble understanding how to write and use an error handler.
>> The
>>> use case is testing a custom Camel component. I would like to have an
>> error
>>> handler that just collects any exceptions thrown by the endpoint's
>> producer.
>>> 
>>> I somewhat naively assumed this would be simple. I thought the Camel
>>> framework must catch any exception, add it the exchange, and invoke the
>>> handler's process method. That appears not to be the case.
>>> 
>>> Below is my attempt. The test is set up so my the producer will throw an
>>> exception. To my surprise, the CaptureErrorHandler.process method is
>> being
>>> invoked twice. my component's producer is never invoked.
>>> 
>>> But If I remove usage of my my error handler from the RouteBuilder, the
>>> producer is invoked, and it throws an exception that is logged by the
>>> DefaultErrorHandler.
>>> 
>>> Clearly, I don't understand how error handles are supposed to be used.
>> Can
>>> someone enlighten me, or point me to more detailed documentation? The
>> Camel
>>> documentation for ErrorHandler is pretty light on details. Alternatively,
>>> maybe there's a simpler approach I can take for testing my component.
>>> 
>>> 
>>> The RouteBuilder's configure method looks like this:
>>> 
>>> public void configure() throws Exception {
>>>  errorHandler(new CaptureErrorHandlerBuilder());
>>>  from(destinations)
>>>      .process(new Counter())
>>>      .to("foo:endpoint");
>>> }
>>> 
>>> 
>>> And the CaptureErrorHandlerBuilder and CaptureErrorHandler looks like
>> this:
>>> 
>>> private class CaptureErrorHandlerBuilder extends
>> ErrorHandlerBuilderSupport
>>> {
>>> 
>>>  @Override
>>>  public boolean supportTransacted() {
>>>    return false;
>>>  }
>>> 
>>>  @Override
>>>  public ErrorHandlerBuilder cloneBuilder() {
>>>    return new CaptureErrorHandlerBuilder();
>>>  }
>>> 
>>>  @Override
>>>  public Processor createErrorHandler(RouteContext routeContext,
>> Processor
>>> processor) throws Exception {
>>>    CaptureErrorHandler handler = new CaptureErrorHandler();
>>>    configure(routeContext, handler);
>>>    return handler;
>>>  }
>>> }
>>> 
>>> private class CaptureErrorHandler implements ErrorHandler {
>>>  @Override
>>>  public void process(Exchange exchange) throws Exception {
>>>    Exception e = exchange.getException();
>>> 
>>>    // For now, just log the exception
>>> 
>>>    if (e != null) {
>>>      e.printStackTrace();
>>> 
>>>    }
>>> 
>>>  }
>>> }
>> 
>> 
>> 
>> --
>> Claus Ibsen
>> -----------------
>> http://davsclaus.com @davsclaus
>> Camel in Action 2: https://www.manning.com/ibsen2
>> 


Re: Handling errors

Posted by Robert Huffman <ro...@gmail.com>.
Well, of course I read that, but all I want is access to the actual
Exception objects in my test. I can add the try/catch clauses, but as
nearly as I can tell the only thing you can do with the catch is route to
another endpoint. Isn't there a simple way to get at the underlying
exceptions?


On Tue, Jan 3, 2017 at 10:57 AM, Claus Ibsen <cl...@gmail.com> wrote:

> Hi
>
> You should not build your own, but use what is OOTB
> http://camel.apache.org/error-handling-in-camel.html
>
> If you have Camel in Action book then it has a full chapter devoted to
> error handling.
>
>
>
> On Tue, Jan 3, 2017 at 7:44 PM, Robert Huffman <ro...@gmail.com>
> wrote:
> > I'm having trouble understanding how to write and use an error handler.
> The
> > use case is testing a custom Camel component. I would like to have an
> error
> > handler that just collects any exceptions thrown by the endpoint's
> producer.
> >
> > I somewhat naively assumed this would be simple. I thought the Camel
> > framework must catch any exception, add it the exchange, and invoke the
> > handler's process method. That appears not to be the case.
> >
> > Below is my attempt. The test is set up so my the producer will throw an
> > exception. To my surprise, the CaptureErrorHandler.process method is
> being
> > invoked twice. my component's producer is never invoked.
> >
> > But If I remove usage of my my error handler from the RouteBuilder, the
> > producer is invoked, and it throws an exception that is logged by the
> > DefaultErrorHandler.
> >
> > Clearly, I don't understand how error handles are supposed to be used.
> Can
> > someone enlighten me, or point me to more detailed documentation? The
> Camel
> > documentation for ErrorHandler is pretty light on details. Alternatively,
> > maybe there's a simpler approach I can take for testing my component.
> >
> >
> > The RouteBuilder's configure method looks like this:
> >
> > public void configure() throws Exception {
> >   errorHandler(new CaptureErrorHandlerBuilder());
> >   from(destinations)
> >       .process(new Counter())
> >       .to("foo:endpoint");
> > }
> >
> >
> > And the CaptureErrorHandlerBuilder and CaptureErrorHandler looks like
> this:
> >
> > private class CaptureErrorHandlerBuilder extends
> ErrorHandlerBuilderSupport
> > {
> >
> >   @Override
> >   public boolean supportTransacted() {
> >     return false;
> >   }
> >
> >   @Override
> >   public ErrorHandlerBuilder cloneBuilder() {
> >     return new CaptureErrorHandlerBuilder();
> >   }
> >
> >   @Override
> >   public Processor createErrorHandler(RouteContext routeContext,
> Processor
> > processor) throws Exception {
> >     CaptureErrorHandler handler = new CaptureErrorHandler();
> >     configure(routeContext, handler);
> >     return handler;
> >   }
> > }
> >
> > private class CaptureErrorHandler implements ErrorHandler {
> >   @Override
> >   public void process(Exchange exchange) throws Exception {
> >     Exception e = exchange.getException();
> >
> >     // For now, just log the exception
> >
> >     if (e != null) {
> >       e.printStackTrace();
> >
> >     }
> >
> >   }
> > }
>
>
>
> --
> Claus Ibsen
> -----------------
> http://davsclaus.com @davsclaus
> Camel in Action 2: https://www.manning.com/ibsen2
>

Re: Handling errors

Posted by Claus Ibsen <cl...@gmail.com>.
Hi

You should not build your own, but use what is OOTB
http://camel.apache.org/error-handling-in-camel.html

If you have Camel in Action book then it has a full chapter devoted to
error handling.



On Tue, Jan 3, 2017 at 7:44 PM, Robert Huffman <ro...@gmail.com> wrote:
> I'm having trouble understanding how to write and use an error handler. The
> use case is testing a custom Camel component. I would like to have an error
> handler that just collects any exceptions thrown by the endpoint's producer.
>
> I somewhat naively assumed this would be simple. I thought the Camel
> framework must catch any exception, add it the exchange, and invoke the
> handler's process method. That appears not to be the case.
>
> Below is my attempt. The test is set up so my the producer will throw an
> exception. To my surprise, the CaptureErrorHandler.process method is being
> invoked twice. my component's producer is never invoked.
>
> But If I remove usage of my my error handler from the RouteBuilder, the
> producer is invoked, and it throws an exception that is logged by the
> DefaultErrorHandler.
>
> Clearly, I don't understand how error handles are supposed to be used. Can
> someone enlighten me, or point me to more detailed documentation? The Camel
> documentation for ErrorHandler is pretty light on details. Alternatively,
> maybe there's a simpler approach I can take for testing my component.
>
>
> The RouteBuilder's configure method looks like this:
>
> public void configure() throws Exception {
>   errorHandler(new CaptureErrorHandlerBuilder());
>   from(destinations)
>       .process(new Counter())
>       .to("foo:endpoint");
> }
>
>
> And the CaptureErrorHandlerBuilder and CaptureErrorHandler looks like this:
>
> private class CaptureErrorHandlerBuilder extends ErrorHandlerBuilderSupport
> {
>
>   @Override
>   public boolean supportTransacted() {
>     return false;
>   }
>
>   @Override
>   public ErrorHandlerBuilder cloneBuilder() {
>     return new CaptureErrorHandlerBuilder();
>   }
>
>   @Override
>   public Processor createErrorHandler(RouteContext routeContext, Processor
> processor) throws Exception {
>     CaptureErrorHandler handler = new CaptureErrorHandler();
>     configure(routeContext, handler);
>     return handler;
>   }
> }
>
> private class CaptureErrorHandler implements ErrorHandler {
>   @Override
>   public void process(Exchange exchange) throws Exception {
>     Exception e = exchange.getException();
>
>     // For now, just log the exception
>
>     if (e != null) {
>       e.printStackTrace();
>
>     }
>
>   }
> }



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