You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by ro...@apache.org on 2009/08/06 12:48:14 UTC

svn commit: r801596 - in /camel/trunk/camel-core/src: main/java/org/apache/camel/processor/CatchProcessor.java main/java/org/apache/camel/processor/TryProcessor.java test/java/org/apache/camel/processor/TryProcessorTest.java

Author: romkal
Date: Thu Aug  6 10:48:00 2009
New Revision: 801596

URL: http://svn.apache.org/viewvc?rev=801596&view=rev
Log:
CAMEL-1883: Exceptions caught in doCatch are of the same type as what is kept in EXCEPTION_CAUGHT

Modified:
    camel/trunk/camel-core/src/main/java/org/apache/camel/processor/CatchProcessor.java
    camel/trunk/camel-core/src/main/java/org/apache/camel/processor/TryProcessor.java
    camel/trunk/camel-core/src/test/java/org/apache/camel/processor/TryProcessorTest.java

Modified: camel/trunk/camel-core/src/main/java/org/apache/camel/processor/CatchProcessor.java
URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/main/java/org/apache/camel/processor/CatchProcessor.java?rev=801596&r1=801595&r2=801596&view=diff
==============================================================================
--- camel/trunk/camel-core/src/main/java/org/apache/camel/processor/CatchProcessor.java (original)
+++ camel/trunk/camel-core/src/main/java/org/apache/camel/processor/CatchProcessor.java Thu Aug  6 10:48:00 2009
@@ -51,27 +51,31 @@
     }
 
     /**
-     * Whether this catch processor catch the given thrown exception
+     * Returns with the exception that is caught by this processor.
+     * 
+     * This method traverses exception causes, so sometimes the exception
+     * returned from this method might be one of causes of the parameter
+     * passed.
      *
      * @param exchange  the current exchange
      * @param exception the thrown exception
-     * @return <tt>true</tt> if this processor catches it, <tt>false</tt> otherwise.
+     * @return Throwable that this processor catches. <tt>null</tt> if nothing matches.
      */
-    public boolean catches(Exchange exchange, Throwable exception) {
+    public Throwable catches(Exchange exchange, Throwable exception) {
         // use the exception iterator to walk the caused by hierachy
         Iterator<Throwable> it = ObjectHelper.createExceptionIterator(exception);
         while (it.hasNext()) {
             Throwable e = it.next();
             // see if we catch this type
-            for (Class type : exceptions) {
+            for (Class<?> type : exceptions) {
                 if (type.isInstance(e) && matchesWhen(exchange)) {
-                    return true;
+                    return e;
                 }
             }
         }
 
         // not found
-        return false;
+        return null;
     }
 
 

Modified: camel/trunk/camel-core/src/main/java/org/apache/camel/processor/TryProcessor.java
URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/main/java/org/apache/camel/processor/TryProcessor.java?rev=801596&r1=801595&r2=801596&view=diff
==============================================================================
--- camel/trunk/camel-core/src/main/java/org/apache/camel/processor/TryProcessor.java (original)
+++ camel/trunk/camel-core/src/main/java/org/apache/camel/processor/TryProcessor.java Thu Aug  6 10:48:00 2009
@@ -98,15 +98,16 @@
         }
 
         for (CatchProcessor catchClause : catchClauses) {
-            if (catchClause.catches(exchange, e)) {
+            Throwable caught = catchClause.catches(exchange, e);
+            if (caught != null) {
                 if (LOG.isTraceEnabled()) {
-                    LOG.trace("This TryProcessor catches the exception: " + e.getClass().getName() + " caused by: " + e.getMessage());
+                    LOG.trace("This TryProcessor catches the exception: " + caught.getClass().getName() + " caused by: " + e.getMessage());
                 }
 
                 // lets attach the exception to the exchange
                 Exchange localExchange = exchange.copy();
                 
-                localExchange.setProperty(Exchange.EXCEPTION_CAUGHT, e);
+                localExchange.setProperty(Exchange.EXCEPTION_CAUGHT, caught);
                 // give the rest of the pipeline another chance
                 localExchange.setException(null);
 
@@ -117,7 +118,7 @@
 
                 if (LOG.isDebugEnabled()) {
                     LOG.debug("The exception is handled: " + handled + " for the exception: " + e.getClass().getName()
-                        + " caused by: " + e.getMessage());
+                        + " caused by: " + caught.getMessage());
                 }
 
                 if (handled) {

Modified: camel/trunk/camel-core/src/test/java/org/apache/camel/processor/TryProcessorTest.java
URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/test/java/org/apache/camel/processor/TryProcessorTest.java?rev=801596&r1=801595&r2=801596&view=diff
==============================================================================
--- camel/trunk/camel-core/src/test/java/org/apache/camel/processor/TryProcessorTest.java (original)
+++ camel/trunk/camel-core/src/test/java/org/apache/camel/processor/TryProcessorTest.java Thu Aug  6 10:48:00 2009
@@ -16,9 +16,13 @@
  */
 package org.apache.camel.processor;
 
+import org.apache.camel.CamelException;
 import org.apache.camel.ContextTestSupport;
 import org.apache.camel.Exchange;
+import org.apache.camel.Expression;
+import org.apache.camel.Predicate;
 import org.apache.camel.Processor;
+import org.apache.camel.RuntimeCamelException;
 import org.apache.camel.builder.RouteBuilder;
 import org.apache.camel.component.mock.MockEndpoint;
 
@@ -29,14 +33,26 @@
 
     private boolean handled;
 
-    public void testTryCatchFinally() throws Exception {
+    public void testTryCatchFinallyProcessor() throws Exception {
+        testTryCatchFinally("direct:processor");
+    }
+
+    public void testTryCatchFinallyExpression() throws Exception {
+        testTryCatchFinally("direct:expression");
+    }
+
+    public void testTryCatchFinallyPredicate() throws Exception {
+        testTryCatchFinally("direct:predicate");
+    }
+
+    private void testTryCatchFinally(String endpointName) throws Exception {
         MockEndpoint mock = getMockEndpoint("mock:result");
         mock.expectedMessageCount(0);
 
         getMockEndpoint("mock:last").expectedMessageCount(1);
         getMockEndpoint("mock:finally").expectedMessageCount(1);
 
-        sendBody("direct:start", "<test>Hello World!</test>");
+        sendBody(endpointName, "<test>Hello World!</test>");
         assertTrue("Should have been handled", handled);
 
         assertMockEndpointsSatisfied();
@@ -45,23 +61,57 @@
     protected RouteBuilder createRouteBuilder() {
         return new RouteBuilder() {
             public void configure() {
-                from("direct:start")
+                from("direct:processor")
                     .doTry()
                         .process(new ProcessorFail())
                         .to("mock:result")
-                    .doCatch(Exception.class)
+                    .doCatch(CamelException.class)
                         .process(new ProcessorHandle())
                     .doFinally()
                         .to("mock:finally")
                     .end()
                     .to("mock:last");
+                
+                from("direct:expression")
+                    .doTry()
+                        .setBody(new ProcessorFail())
+                        .to("mock:result")
+                    .doCatch(CamelException.class)
+                        .process(new ProcessorHandle())
+                    .doFinally()
+                        .to("mock:finally")
+                    .end()
+                    .to("mock:last");
+                
+                from("direct:predicate")
+                    .doTry()
+                        .to("direct:sub-predicate")
+                    .doCatch(CamelException.class)
+                        .process(new ProcessorHandle())
+                    .doFinally()
+                        .to("mock:finally")
+                    .end()
+                    .to("mock:last");
+                
+                from("direct:sub-predicate")
+                    .errorHandler(noErrorHandler())
+                    .filter(new ProcessorFail())
+                    .to("mock:result");
             }
         };
     }
 
-    private class ProcessorFail implements Processor {
+    private class ProcessorFail implements Processor, Predicate, Expression {
         public void process(Exchange exchange) throws Exception {
-            throw new IllegalStateException("Force to fail");
+            throw new RuntimeCamelException(new CamelException("Force to fail"));
+        }
+
+        public <T> T evaluate(Exchange exchange, Class<T> type) {
+            throw new RuntimeCamelException(new CamelException("Force to fail"));
+        }
+
+        public boolean matches(Exchange exchange) {
+            throw new RuntimeCamelException(new CamelException("Force to fail"));
         }
     }
 
@@ -73,7 +123,9 @@
 
             Exception e = (Exception)exchange.getProperty(Exchange.EXCEPTION_CAUGHT);
             assertNotNull("There should be an exception", e);
-            assertTrue(e instanceof IllegalStateException);
+            
+            // If we handle CamelException it is what we should have as an exception caught
+            assertTrue(e instanceof CamelException);
             assertEquals("Force to fail", e.getMessage());
         }
     }