You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@camel.apache.org by Trevv <45...@safe-mail.net> on 2008/11/20 11:04:38 UTC

Exception handling, for InOut exchanges?

I would appreciate some advice about how to access the
HttpOperationFailedException (or any other exception that occurs, anywhere
in the route) and format it appropriately for my application's protocol,
and send it back through MinaConsumer's TCP connection.

I haven't found any example code that would help me, because all of
the error handling example code seems to be meant for InOnly exchanges.
My exchanges are InOut, because I'm using MinaConsumer in sync=true mode.

I don't want any retries.  I just want to take the exception message,
and format it, and send it back.

I have found a trick that works, but I suspect that this trick is anti-
idiomatic, and that there must be a more Camel-appropriate way to do this.

What is the "right" way to do this?

errorHandler(loggingErrorHandler());
from("mina:tcp://[::]:5927?textline=true")
  .intercept(new DelegateProcessor() {
      public void process(Exchange exchange) throws Exception {
          processNext(exchange);
          Throwable exception = exchange.getException();
          if (exception != null) {
              exchange.setException(null);
              exchange.getOut().setBody(formatException(exception));
          }
      }
  })
  . // route continues ...
-- 
View this message in context: http://www.nabble.com/HttpProducer%3A-how-to-access-the-body-of-an-error-page--tp20475651s22882p20598121.html
Sent from the Camel - Users mailing list archive at Nabble.com.


Re: Exception handling, for InOut exchanges?

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

I have added a sample how to send a response back to the client

http://cwiki.apache.org/confluence/display/CAMEL/Exception+Clause
Section: Handling and sending a fixed response back to the client



/Claus Ibsen
Apache Camel Committer
Blog: http://davsclaus.blogspot.com/



On Tue, Nov 25, 2008 at 9:34 AM, Trevv <45...@safe-mail.net> wrote:
>
>
> Claus Ibsen-2 wrote:
>> BTW: As I am interesting in improving the documentation could you
>> maybe help me out here. On which page/section on the wiki would you
>> expect a sample for in out style?
> Possibly http://activemq.apache.org/camel/exception-clause.html .
>
> The problem was that all the exception handling examples, on that page
> and on other pages, end with '.to("some endpoint")' to specify where
> the failure responses will go; I didn't know what endpoint I could put
> inside the '.to("")' in order to get the failure responses to go back
> to the same consumer that originally created the exchange.
>
> It took a few days to realize that I could omit the ".to()" and replace
> it with a ".handled()" and a ".transform()" to get the right behavior.
> --
> View this message in context:
http://www.nabble.com/HttpProducer%3A-how-to-access-the-body-of-an-error-page--tp20475651s22882p20677003.html
> Sent from the Camel - Users mailing list archive at Nabble.com.
>
>

Re: Exception handling, for InOut exchanges?

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

Thanks for the pointer. Yes to transform and no .to is the pattern to
return a custom response to the caller instead of routing to a new
endpoint. I will try to remember to add some wiki doc about this
scenario.

Thanks for reporting and digging real deep into Camel to provide good feedback.

/Claus Ibsen
Apache Camel Committer
Blog: http://davsclaus.blogspot.com/



On Tue, Nov 25, 2008 at 9:34 AM, Trevv <45...@safe-mail.net> wrote:
>
>
> Claus Ibsen-2 wrote:
>> BTW: As I am interesting in improving the documentation could you
>> maybe help me out here. On which page/section on the wiki would you
>> expect a sample for in out style?
> Possibly http://activemq.apache.org/camel/exception-clause.html .
>
> The problem was that all the exception handling examples, on that page
> and on other pages, end with '.to("some endpoint")' to specify where
> the failure responses will go; I didn't know what endpoint I could put
> inside the '.to("")' in order to get the failure responses to go back
> to the same consumer that originally created the exchange.
>
> It took a few days to realize that I could omit the ".to()" and replace
> it with a ".handled()" and a ".transform()" to get the right behavior.
> --
> View this message in context: http://www.nabble.com/HttpProducer%3A-how-to-access-the-body-of-an-error-page--tp20475651s22882p20677003.html
> Sent from the Camel - Users mailing list archive at Nabble.com.
>
>

Re: Exception handling, for InOut exchanges?

Posted by Trevv <45...@safe-mail.net>.

Claus Ibsen-2 wrote:
> BTW: As I am interesting in improving the documentation could you
> maybe help me out here. On which page/section on the wiki would you
> expect a sample for in out style?
Possibly http://activemq.apache.org/camel/exception-clause.html .

The problem was that all the exception handling examples, on that page
and on other pages, end with '.to("some endpoint")' to specify where
the failure responses will go; I didn't know what endpoint I could put
inside the '.to("")' in order to get the failure responses to go back
to the same consumer that originally created the exchange.

It took a few days to realize that I could omit the ".to()" and replace
it with a ".handled()" and a ".transform()" to get the right behavior.
-- 
View this message in context: http://www.nabble.com/HttpProducer%3A-how-to-access-the-body-of-an-error-page--tp20475651s22882p20677003.html
Sent from the Camel - Users mailing list archive at Nabble.com.


Re: Exception handling, for InOut exchanges?

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

BTW: As I am interesting in improving the documentation could you
maybe help me out here. On which page/section on the wiki would you
expect a sample for in out style?



/Claus Ibsen
Apache Camel Committer
Blog: http://davsclaus.blogspot.com/



On Thu, Nov 20, 2008 at 11:26 AM, Claus Ibsen <cl...@gmail.com> wrote:
> Hi
>
> The patch at http://issues.apache.org/activemq/browse/CAMEL-1083 might
> give some points for sending a HTTP request to HTTP server that
> returns a error code 500 so it triggers the
> HttpOperationFailedException.
>
>
>
> /Claus Ibsen
> Apache Camel Committer
> Blog: http://davsclaus.blogspot.com/
>
>
>
> On Thu, Nov 20, 2008 at 11:04 AM, Trevv <45...@safe-mail.net> wrote:
>>
>> I would appreciate some advice about how to access the
>> HttpOperationFailedException (or any other exception that occurs, anywhere
>> in the route) and format it appropriately for my application's protocol,
>> and send it back through MinaConsumer's TCP connection.
>>
>> I haven't found any example code that would help me, because all of
>> the error handling example code seems to be meant for InOnly exchanges.
>> My exchanges are InOut, because I'm using MinaConsumer in sync=true mode.
>>
>> I don't want any retries.  I just want to take the exception message,
>> and format it, and send it back.
>>
>> I have found a trick that works, but I suspect that this trick is anti-
>> idiomatic, and that there must be a more Camel-appropriate way to do this.
>>
>> What is the "right" way to do this?
>>
>> errorHandler(loggingErrorHandler());
>> from("mina:tcp://[::]:5927?textline=true")
>>  .intercept(new DelegateProcessor() {
>>      public void process(Exchange exchange) throws Exception {
>>          processNext(exchange);
>>          Throwable exception = exchange.getException();
>>          if (exception != null) {
>>              exchange.setException(null);
>>              exchange.getOut().setBody(formatException(exception));
>>          }
>>      }
>>  })
>>  . // route continues ...
>> --
>> View this message in context: http://www.nabble.com/HttpProducer%3A-how-to-access-the-body-of-an-error-page--tp20475651s22882p20598121.html
>> Sent from the Camel - Users mailing list archive at Nabble.com.
>>
>>
>

Re: HttpProducer: how to access the body of an error page?

Posted by Trevv <45...@safe-mail.net>.

Claus Ibsen-2 wrote:
> Yeah but that must be on the content type header right?
Yes, that's right.  I mis-parsed your earlier message; I thought you
were suggesting that I should convert the byte stream to String without
using the Content-Type header.

text/xml;charset=utf-8 or how this is normally specified?
Yes, that's right.

A new issue linking to 1083 would be good.
See  https://issues.apache.org/activemq/browse/CAMEL-1119 CAMEL-1119 .
-- 
View this message in context: http://www.nabble.com/HttpProducer%3A-how-to-access-the-body-of-an-error-page--tp20475651s22882p20699130.html
Sent from the Camel - Users mailing list archive at Nabble.com.


Re: HttpProducer: how to access the body of an error page?

Posted by Trevv <45...@safe-mail.net>.

Claus Ibsen-2 wrote:
> 
> If anyone knows if HttpClient will consider contenttype charset when
> you use it's method to get the response body as a String, then we
> could use this one and expose it on the HttpOperationFailedException
> as well so you can use this instead of dealing with the charset
> convertions yourself
Yes, org.apache.commons.httpclient.HttpMethod.getResponseBodyAsString()
does work that way, and it probably would be helpful for some future
camel riders, though I don't personally need that feature for my
current application.
-- 
View this message in context: http://www.nabble.com/HttpProducer%3A-how-to-access-the-body-of-an-error-page--tp20475651s22882p20699379.html
Sent from the Camel - Users mailing list archive at Nabble.com.


Re: HttpProducer: how to access the body of an error page?

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

If anyone knows if HttpClient will consider contenttype charset when
you use it's method to get the response body as a String, then we
could use this one and expose it on the HttpOperationFailedException
as well so you can use this instead of dealing with the charset
convertions yourself

/Claus Ibsen
Apache Camel Committer
Blog: http://davsclaus.blogspot.com/



On Tue, Nov 25, 2008 at 11:40 AM, Claus Ibsen <cl...@gmail.com> wrote:
> On Tue, Nov 25, 2008 at 11:34 AM, Trevv <45...@safe-mail.net> wrote:
>>
>>
>> Claus Ibsen-2 wrote:
>>> Ah that would be nice to have the content type on the
>>> HttpOperationException as well?
>>>
>>> But I guess in 99% converting to String would be fine as most servers
>>> return a text/plain or text/html error page.
>> Yes, but to convert bytes to String, you need to know the charset.
>>
>> The charset that was used by the Web server to encode the error page
>> content might not be the same as your camel's JVM's default charset.
> Yeah but that must be on the content type header right?
> text/xml;charset=utf-8 or how this is normally specified?
>
>>
>> Do you mind adding a JIRA ticket to add a getter on the
>>> HttpOperationException for the content type.
>> As an addition to CAMEL-1083, or as a completely new issue?
> A new issue linking to 1083 would be good.
>
>> --
>> View this message in context: http://www.nabble.com/HttpProducer%3A-how-to-access-the-body-of-an-error-page--tp20475651s22882p20678642.html
>> Sent from the Camel - Users mailing list archive at Nabble.com.
>>
>>
>

Re: HttpProducer: how to access the body of an error page?

Posted by Claus Ibsen <cl...@gmail.com>.
On Tue, Nov 25, 2008 at 11:34 AM, Trevv <45...@safe-mail.net> wrote:
>
>
> Claus Ibsen-2 wrote:
>> Ah that would be nice to have the content type on the
>> HttpOperationException as well?
>>
>> But I guess in 99% converting to String would be fine as most servers
>> return a text/plain or text/html error page.
> Yes, but to convert bytes to String, you need to know the charset.
>
> The charset that was used by the Web server to encode the error page
> content might not be the same as your camel's JVM's default charset.
Yeah but that must be on the content type header right?
text/xml;charset=utf-8 or how this is normally specified?

>
> Do you mind adding a JIRA ticket to add a getter on the
>> HttpOperationException for the content type.
> As an addition to CAMEL-1083, or as a completely new issue?
A new issue linking to 1083 would be good.

> --
> View this message in context: http://www.nabble.com/HttpProducer%3A-how-to-access-the-body-of-an-error-page--tp20475651s22882p20678642.html
> Sent from the Camel - Users mailing list archive at Nabble.com.
>
>

Re: HttpProducer: how to access the body of an error page?

Posted by Trevv <45...@safe-mail.net>.

Claus Ibsen-2 wrote:
> Ah that would be nice to have the content type on the
> HttpOperationException as well?
> 
> But I guess in 99% converting to String would be fine as most servers
> return a text/plain or text/html error page.
Yes, but to convert bytes to String, you need to know the charset.

The charset that was used by the Web server to encode the error page
content might not be the same as your camel's JVM's default charset.

Do you mind adding a JIRA ticket to add a getter on the
> HttpOperationException for the content type.
As an addition to CAMEL-1083, or as a completely new issue?
-- 
View this message in context: http://www.nabble.com/HttpProducer%3A-how-to-access-the-body-of-an-error-page--tp20475651s22882p20678642.html
Sent from the Camel - Users mailing list archive at Nabble.com.


Re: HttpProducer: how to access the body of an error page?

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

Ah that would be nice to have the content type on the
HttpOperationException as well?

But I guess in 99% converting to String would be fine as most servers
return a text/plain or text/html error page.
Do you mind adding a JIRA ticket to add a getter on the
HttpOperationException for the content type.



/Claus Ibsen
Apache Camel Committer
Blog: http://davsclaus.blogspot.com/



On Tue, Nov 25, 2008 at 9:30 AM, Trevv <45...@safe-mail.net> wrote:
>
>
> Claus Ibsen-2 wrote:
>> The patch at http://issues.apache.org/activemq/browse/CAMEL-1083 might
>> give some points for sending a HTTP request to HTTP server that
>> returns a error code 500 so it triggers the
>> HttpOperationFailedException.
> Thank you Claus, your patch almost restores the 1.4.0 functionality
> that I need.
>
> Now I can access the error page content, as a stream of raw bytes.
> But I can't convert those raw bytes into something more meaningful
> (e.g. characters) because the Content-Type header has been discarded.
> --
> View this message in context: http://www.nabble.com/HttpProducer%3A-how-to-access-the-body-of-an-error-page--tp20475651s22882p20676965.html
> Sent from the Camel - Users mailing list archive at Nabble.com.
>
>

Re: HttpProducer: how to access the body of an error page?

Posted by Trevv <45...@safe-mail.net>.

Claus Ibsen-2 wrote:
> The patch at http://issues.apache.org/activemq/browse/CAMEL-1083 might
> give some points for sending a HTTP request to HTTP server that
> returns a error code 500 so it triggers the
> HttpOperationFailedException.
Thank you Claus, your patch almost restores the 1.4.0 functionality
that I need.

Now I can access the error page content, as a stream of raw bytes.
But I can't convert those raw bytes into something more meaningful
(e.g. characters) because the Content-Type header has been discarded.
-- 
View this message in context: http://www.nabble.com/HttpProducer%3A-how-to-access-the-body-of-an-error-page--tp20475651s22882p20676965.html
Sent from the Camel - Users mailing list archive at Nabble.com.


Re: Exception handling, for InOut exchanges?

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

The patch at http://issues.apache.org/activemq/browse/CAMEL-1083 might
give some points for sending a HTTP request to HTTP server that
returns a error code 500 so it triggers the
HttpOperationFailedException.



/Claus Ibsen
Apache Camel Committer
Blog: http://davsclaus.blogspot.com/



On Thu, Nov 20, 2008 at 11:04 AM, Trevv <45...@safe-mail.net> wrote:
>
> I would appreciate some advice about how to access the
> HttpOperationFailedException (or any other exception that occurs, anywhere
> in the route) and format it appropriately for my application's protocol,
> and send it back through MinaConsumer's TCP connection.
>
> I haven't found any example code that would help me, because all of
> the error handling example code seems to be meant for InOnly exchanges.
> My exchanges are InOut, because I'm using MinaConsumer in sync=true mode.
>
> I don't want any retries.  I just want to take the exception message,
> and format it, and send it back.
>
> I have found a trick that works, but I suspect that this trick is anti-
> idiomatic, and that there must be a more Camel-appropriate way to do this.
>
> What is the "right" way to do this?
>
> errorHandler(loggingErrorHandler());
> from("mina:tcp://[::]:5927?textline=true")
>  .intercept(new DelegateProcessor() {
>      public void process(Exchange exchange) throws Exception {
>          processNext(exchange);
>          Throwable exception = exchange.getException();
>          if (exception != null) {
>              exchange.setException(null);
>              exchange.getOut().setBody(formatException(exception));
>          }
>      }
>  })
>  . // route continues ...
> --
> View this message in context: http://www.nabble.com/HttpProducer%3A-how-to-access-the-body-of-an-error-page--tp20475651s22882p20598121.html
> Sent from the Camel - Users mailing list archive at Nabble.com.
>
>

Re: Exception handling, for InOut exchanges?

Posted by Trevv <45...@safe-mail.net>.

RomKal wrote:
> You can always use try/catch construct to catch the exception and
> create appropriate response before it goes back to mina.
> 
> An example can be found here:
Thank you Romek, that looks good.
But I think I found an even better way, idiomatic and extremely succinct.

What I need to do is:
(a) prevent redelivery attempts;
(b) short-circuit the pipeline;
(c) access the exception and use it to build the new exchange.out.body;
(d) prevent exchange.exception from being set (because MinaConsumer
    would want to send exchange.exception instead of exchange.out.body).

It seems that I can achieve all these goals, globally for all routes,
with just these few lines of code:

onException(Exception.class)
  .handled(true)
  .process(new Processor() {
    public void process(Exchange exchange) {
      Throwable exception = (Throwable)
        exchange.getProperty(DeadLetterChannel.EXCEPTION_CAUSE_PROPERTY);
      exchange.getOut().setBody(myExceptionFormatter(exception));
    }
  });
-- 
View this message in context: http://www.nabble.com/HttpProducer%3A-how-to-access-the-body-of-an-error-page--tp20475651s22882p20676917.html
Sent from the Camel - Users mailing list archive at Nabble.com.


Re: Exception handling, for InOut exchanges?

Posted by Roman Kalukiewicz <ro...@gmail.com>.
Hey Trevv!

You can always use try/catch construct to catch the exception and
create appropriate response before it goes back to mina.

An example can be found here:

http://svn.apache.org/repos/asf/activemq/camel/trunk/camel-core/src/test/java/org/apache/camel/processor/TryProcessorHandleTest.java

Romek

2008/11/20 Trevv <45...@safe-mail.net>:
>
> I would appreciate some advice about how to access the
> HttpOperationFailedException (or any other exception that occurs, anywhere
> in the route) and format it appropriately for my application's protocol,
> and send it back through MinaConsumer's TCP connection.
>
> I haven't found any example code that would help me, because all of
> the error handling example code seems to be meant for InOnly exchanges.
> My exchanges are InOut, because I'm using MinaConsumer in sync=true mode.
>
> I don't want any retries.  I just want to take the exception message,
> and format it, and send it back.
>
> I have found a trick that works, but I suspect that this trick is anti-
> idiomatic, and that there must be a more Camel-appropriate way to do this.
>
> What is the "right" way to do this?
>
> errorHandler(loggingErrorHandler());
> from("mina:tcp://[::]:5927?textline=true")
>  .intercept(new DelegateProcessor() {
>      public void process(Exchange exchange) throws Exception {
>          processNext(exchange);
>          Throwable exception = exchange.getException();
>          if (exception != null) {
>              exchange.setException(null);
>              exchange.getOut().setBody(formatException(exception));
>          }
>      }
>  })
>  . // route continues ...
> --
> View this message in context: http://www.nabble.com/HttpProducer%3A-how-to-access-the-body-of-an-error-page--tp20475651s22882p20598121.html
> Sent from the Camel - Users mailing list archive at Nabble.com.
>
>