You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@camel.apache.org by Echo <ah...@gmail.com> on 2011/07/20 10:51:37 UTC

Fetch Custom Exception from the exchange

Hello , 
I have declared my CustomException class. 
When the onException() catches it , it goes to the processor I defined : 

onException(classOf[CustomException]).process(doSmth) 

So far so good . 
The issue that I need into the processor to check if the exception is of
type "CustomException" or not 

when I write : 

 def process(exchange: Exchange) = { 
    val exception: CustomException=
exchange.getProperty(Exchange.EXCEPTION_CAUGHT, classOf[CustomException]) 

I got null 

But when I write : 

 def process(exchange: Exchange) = { 
    val exception: Exception =
exchange.getProperty(Exchange.EXCEPTION_CAUGHT, classOf[Exception]) 

I got my exception object 

How could I check which type of exception is thrown into the processor ! 

--
View this message in context: http://camel.465427.n5.nabble.com/Fetch-Custom-Exception-from-the-exchange-tp4615183p4615183.html
Sent from the Camel - Users mailing list archive at Nabble.com.

Re: Fetch Custom Exception from the exchange

Posted by Echo <ah...@gmail.com>.
I have just figured it out .
It fetched it from the cause of the exception not from the stack trace .

Thanks alot for ur wonderfull support .

@ Claus Ibsen-2: I do appreciate the effort u did and ur amazing test case
Thx 


--
View this message in context: http://camel.465427.n5.nabble.com/Fetch-Custom-Exception-from-the-exchange-tp4615183p4616873.html
Sent from the Camel - Users mailing list archive at Nabble.com.

Re: Fetch Custom Exception from the exchange

Posted by Echo <ah...@gmail.com>.
That's gr8 :)
I tried to apply ur approach but it doesn't work into my case .
As my case has multiple processors and after one of those processors into
the middle I need to log the exchange's exception using DSL .
Applying this approach couldn't allow me to go and continue the rest of
chained processes 

In my case I have a route like : 


this.onException(classOf[IOException]).process(doSmth).log(LoggingLevel.INFO,
"new",     "${exception.stacktrace}")


My processor :

    Exception exception = exchange.getProperty(Exchange.EXCEPTION_CAUGHT,
classOf[Exception])
     CustomException customException=CustomException(exception)

When the onException goes to the DSL logging part , it will print the stack
trace as a IOException 
Instead I need to be CustomException

I have managed to do some work around by doing the following @ processor :

exchange.getOut.setHeader("ex",customException)

then @Route :

this.onException(classOf[IOException]).process(doSmth).log(LoggingLevel.INFO,
"new", ${in.header.ex} ")


It prints into my log the following and that what I exactly need from the
beginning :
CustomException: java.lang.IOException

What's wired is when I use ${exception.stacktrace} it prints the whole stack
trace as needed.
When I googled it , I found out  that expression fetches 

exchange.getProperty(Exchange.EXCEPTION_CAUGHT) and prints its stack trace .

However when I debug what returns from 
exchange.getProperty(Exchange.EXCEPTION_CAUGHT)
doesn't have stack trace 

So do u know from where does it fetch the stack trace when I type
${exception.stacktrace}!








    








--
View this message in context: http://camel.465427.n5.nabble.com/Fetch-Custom-Exception-from-the-exchange-tp4615183p4616849.html
Sent from the Camel - Users mailing list archive at Nabble.com.

Re: Fetch Custom Exception from the exchange

Posted by Claus Ibsen <cl...@gmail.com>.
See this unit test
http://svn.apache.org/viewvc?rev=1148854&view=rev

On Wed, Jul 20, 2011 at 4:27 PM, Echo <ah...@gmail.com> wrote:
> Actually that's the happy scenario :)
> Um now sucedded to log into a new file rather than karaf.log .
>
> I need when an exception occurs , throw a CustomException into my new log .
>
> Um now trying to use these :http://camel.apache.org/simple.html
>
> Trying to use :
>
> exchange.getOut.setHeader("tot","hi there")
>
> process(doSmth).
>       log(LoggingLevel.INFO, "new", "${exchangeId} -CUSTOM--
> ${out.header.tot}
>
> I got nothing when I try to print : ${out.header.tot} ... any ideaS !!
>
>
>
>
> --
> View this message in context: http://camel.465427.n5.nabble.com/Fetch-Custom-Exception-from-the-exchange-tp4615183p4616042.html
> Sent from the Camel - Users mailing list archive at Nabble.com.
>



-- 
Claus Ibsen
-----------------
FuseSource
Email: cibsen@fusesource.com
Web: http://fusesource.com
Twitter: davsclaus, fusenews
Blog: http://davsclaus.blogspot.com/
Author of Camel in Action: http://www.manning.com/ibsen/

Re: Fetch Custom Exception from the exchange

Posted by Echo <ah...@gmail.com>.
Actually that's the happy scenario :)
Um now sucedded to log into a new file rather than karaf.log .

I need when an exception occurs , throw a CustomException into my new log .

Um now trying to use these :http://camel.apache.org/simple.html

Trying to use :

exchange.getOut.setHeader("tot","hi there")

process(doSmth).
       log(LoggingLevel.INFO, "new", "${exchangeId} -CUSTOM--
${out.header.tot}

I got nothing when I try to print : ${out.header.tot} ... any ideaS !! 




--
View this message in context: http://camel.465427.n5.nabble.com/Fetch-Custom-Exception-from-the-exchange-tp4615183p4616042.html
Sent from the Camel - Users mailing list archive at Nabble.com.

Re: Fetch Custom Exception from the exchange

Posted by Ashwin Karpe <ak...@fusesource.com>.
Hi,

Unless you are looking to catch a specific exception, you could catch an
exception of type java.lang.Exception and then in the processor
"doSomething" you could check the exception and perform an action.

Thrown exceptions are stored in the Message Exchange and you can get the
exchange in your processor as follows
        
        public void process(Exchange exchange) throws Exception {
            // the caused by exception is stored in a property on the
exchange
            Throwable caused =
exchange.getProperty(Exchange.EXCEPTION_CAUGHT, Throwable.class);
            ...
        }

Please find an attached example below

https://svn.apache.org/viewvc/camel/trunk/camel-core/src/test/java/org/apache/camel/processor/onexception/OnExceptionProcessorInspectCausedExceptionTest.java?view=co
https://svn.apache.org/viewvc/camel/trunk/camel-core/src/test/java/org/apache/camel/processor/onexception/OnExceptionProcessorInspectCausedExceptionTest.java?view=co 

Cheers,

Ashwin...

-----
---------------------------------------------------------
Ashwin Karpe
Apache Camel Committer & Sr Principal Consultant
FUSESource (a Progress Software Corporation subsidiary)
http://fusesource.com 

Blog: http://opensourceknowledge.blogspot.com 
---------------------------------------------------------
--
View this message in context: http://camel.465427.n5.nabble.com/Fetch-Custom-Exception-from-the-exchange-tp4615183p4615912.html
Sent from the Camel - Users mailing list archive at Nabble.com.