Author: davsclaus Date: Fri Nov 20 10:49:06 2009 New Revision: 882492 URL: http://svn.apache.org/viewvc?rev=882492&view=rev Log: CAMEL-2205: Improved error reported when failed to create a route on startup. It now pinpoint which part of the route that is causing the problem.
Added: camel/trunk/camel-core/src/main/java/org/apache/camel/FailedToCreateRouteException.java - copied, changed from r882431, camel/trunk/camel-core/src/main/java/org/apache/camel/FailedToStartRouteException.java camel/trunk/camel-core/src/test/java/org/apache/camel/model/StartingRoutesErrorReportedTest.java (with props) Modified: camel/trunk/camel-core/src/main/java/org/apache/camel/model/BeanDefinition.java camel/trunk/camel-core/src/main/java/org/apache/camel/model/RouteDefinition.java camel/trunk/camel-core/src/test/java/org/apache/camel/component/timer/TimerWithTimeOptionTest.java camel/trunk/camel-core/src/test/java/org/apache/camel/model/ProcessorTypeConfigurationTest.java camel/trunk/camel-core/src/test/java/org/apache/camel/processor/CreateRouteWithNonExistingEndpointTest.java camel/trunk/components/camel-spring/src/test/java/org/apache/camel/component/xslt/InvalidXsltFileTest.java camel/trunk/components/camel-spring/src/test/java/org/apache/camel/spring/issues/SpringMainStartFailedIssueTest.java camel/trunk/components/camel-spring/src/test/java/org/apache/camel/spring/management/SpringCamelContextStartingFailedEventTest.java camel/trunk/components/camel-spring/src/test/java/org/apache/camel/spring/processor/SpringDeadLetterChannelInvalidDeadLetterUriTest.java camel/trunk/components/camel-spring/src/test/java/org/apache/camel/spring/processor/SpringDeadLetterChannelInvalidOptionDeadLetterUriTest.java Copied: camel/trunk/camel-core/src/main/java/org/apache/camel/FailedToCreateRouteException.java (from r882431, camel/trunk/camel-core/src/main/java/org/apache/camel/FailedToStartRouteException.java) URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/main/java/org/apache/camel/FailedToCreateRouteException.java?p2=camel/trunk/camel-core/src/main/java/org/apache/camel/FailedToCreateRouteException.java&p1=camel/trunk/camel-core/src/main/java/org/apache/camel/FailedToStartRouteException.java&r1=882431&r2=882492&rev=882492&view=diff ============================================================================== --- camel/trunk/camel-core/src/main/java/org/apache/camel/FailedToStartRouteException.java (original) +++ camel/trunk/camel-core/src/main/java/org/apache/camel/FailedToCreateRouteException.java Fri Nov 20 10:49:06 2009 @@ -17,20 +17,35 @@ package org.apache.camel; /** - * Exception when failing to start a {...@link Route}. + * Exception when failing to create a {...@link org.apache.camel.Route}. * * @version $Revision$ */ -public class FailedToStartRouteException extends CamelException { - private static final long serialVersionUID = -6118520819865759888L; +public class FailedToCreateRouteException extends CamelException { - public FailedToStartRouteException(String routeId, String message) { - super("Failed to start route " + routeId + " because of " + message); + private final String routeId; + + public FailedToCreateRouteException(String routeId, String route, Throwable cause) { + super("Failed to create route " + routeId + ": " + getRouteMessage(route) + " because of " + cause.getMessage(), cause); + this.routeId = routeId; } - public FailedToStartRouteException(Throwable cause) { - super(cause); + public FailedToCreateRouteException(String routeId, String route, String at, Throwable cause) { + super("Failed to create route " + routeId + " at: >>> " + at + " <<< in route: " + getRouteMessage(route) + " because of " + cause.getMessage(), cause); + this.routeId = routeId; } -} + public String getRouteId() { + return routeId; + } + protected static String getRouteMessage(String route) { + // cut the route after 60 chars so it wont be too big in the message + // users just need to be able to identify the route so they know where to look + if (route.length() > 60) { + return route.substring(0, 60) + "..."; + } else { + return route; + } + } +} \ No newline at end of file Modified: camel/trunk/camel-core/src/main/java/org/apache/camel/model/BeanDefinition.java URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/main/java/org/apache/camel/model/BeanDefinition.java?rev=882492&r1=882491&r2=882492&view=diff ============================================================================== --- camel/trunk/camel-core/src/main/java/org/apache/camel/model/BeanDefinition.java (original) +++ camel/trunk/camel-core/src/main/java/org/apache/camel/model/BeanDefinition.java Fri Nov 20 10:49:06 2009 @@ -146,7 +146,7 @@ @SuppressWarnings("unchecked") public Processor createProcessor(RouteContext routeContext) { BeanProcessor answer; - if (ref != null) { + if (ObjectHelper.isNotEmpty(ref)) { answer = new BeanProcessor(new RegistryBean(routeContext.getCamelContext(), ref)); } else { if (bean == null) { Modified: camel/trunk/camel-core/src/main/java/org/apache/camel/model/RouteDefinition.java URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/main/java/org/apache/camel/model/RouteDefinition.java?rev=882492&r1=882491&r2=882492&view=diff ============================================================================== --- camel/trunk/camel-core/src/main/java/org/apache/camel/model/RouteDefinition.java (original) +++ camel/trunk/camel-core/src/main/java/org/apache/camel/model/RouteDefinition.java Fri Nov 20 10:49:06 2009 @@ -30,6 +30,7 @@ import org.apache.camel.CamelContext; import org.apache.camel.CamelContextAware; import org.apache.camel.Endpoint; +import org.apache.camel.FailedToCreateRouteException; import org.apache.camel.NoSuchEndpointException; import org.apache.camel.Route; import org.apache.camel.ServiceStatus; @@ -130,7 +131,15 @@ } for (FromDefinition fromType : inputs) { - RouteContext routeContext = addRoutes(routes, fromType); + RouteContext routeContext; + try { + routeContext = addRoutes(routes, fromType); + } catch (FailedToCreateRouteException e) { + throw e; + } catch (Exception e) { + // wrap in exception which provide more details about which route was failing + throw new FailedToCreateRouteException(getId(), toString(), e); + } answer.add(routeContext); } return answer; @@ -555,7 +564,7 @@ routeContext.setStreamCaching(isStreamCache()); if (isStreamCache()) { if (log.isDebugEnabled()) { - log.debug("StramCaching is enabled on route: " + this); + log.debug("StreamCaching is enabled on route: " + this); } // only add a new stream cache if not already a global configured on camel context if (StreamCaching.getStreamCaching(getCamelContext()) == null) { @@ -627,7 +636,12 @@ List<ProcessorDefinition> list = new ArrayList<ProcessorDefinition>(outputs); for (ProcessorDefinition output : list) { - output.addRoutes(routeContext, routes); + try { + output.addRoutes(routeContext, routes); + } catch (Exception e) { + RouteDefinition route = routeContext.getRoute(); + throw new FailedToCreateRouteException(route.getId(), route.toString(), output.toString(), e); + } } routeContext.commit(); Modified: camel/trunk/camel-core/src/test/java/org/apache/camel/component/timer/TimerWithTimeOptionTest.java URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/test/java/org/apache/camel/component/timer/TimerWithTimeOptionTest.java?rev=882492&r1=882491&r2=882492&view=diff ============================================================================== --- camel/trunk/camel-core/src/test/java/org/apache/camel/component/timer/TimerWithTimeOptionTest.java (original) +++ camel/trunk/camel-core/src/test/java/org/apache/camel/component/timer/TimerWithTimeOptionTest.java Fri Nov 20 10:49:06 2009 @@ -21,7 +21,7 @@ import java.util.Date; import org.apache.camel.ContextTestSupport; -import org.apache.camel.ResolveEndpointFailedException; +import org.apache.camel.FailedToCreateRouteException; import org.apache.camel.builder.RouteBuilder; import org.apache.camel.component.mock.MockEndpoint; @@ -145,8 +145,8 @@ try { context.start(); fail("Should throw an exception"); - } catch (ResolveEndpointFailedException e) { - assertIsInstanceOf(ParseException.class, e.getCause()); + } catch (FailedToCreateRouteException e) { + assertIsInstanceOf(ParseException.class, e.getCause().getCause()); } } Modified: camel/trunk/camel-core/src/test/java/org/apache/camel/model/ProcessorTypeConfigurationTest.java URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/test/java/org/apache/camel/model/ProcessorTypeConfigurationTest.java?rev=882492&r1=882491&r2=882492&view=diff ============================================================================== --- camel/trunk/camel-core/src/test/java/org/apache/camel/model/ProcessorTypeConfigurationTest.java (original) +++ camel/trunk/camel-core/src/test/java/org/apache/camel/model/ProcessorTypeConfigurationTest.java Fri Nov 20 10:49:06 2009 @@ -17,6 +17,7 @@ package org.apache.camel.model; import org.apache.camel.ContextTestSupport; +import org.apache.camel.FailedToCreateRouteException; import org.apache.camel.builder.RouteBuilder; /** @@ -32,8 +33,8 @@ } }); fail("Should have thrown IllegalArgumentException"); - } catch (IllegalArgumentException e) { - assertEquals("registry entry called hello must be specified on: process[ref:hello]", e.getMessage()); + } catch (FailedToCreateRouteException e) { + assertEquals("registry entry called hello must be specified on: process[ref:hello]", e.getCause().getMessage()); } } Added: camel/trunk/camel-core/src/test/java/org/apache/camel/model/StartingRoutesErrorReportedTest.java URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/test/java/org/apache/camel/model/StartingRoutesErrorReportedTest.java?rev=882492&view=auto ============================================================================== --- camel/trunk/camel-core/src/test/java/org/apache/camel/model/StartingRoutesErrorReportedTest.java (added) +++ camel/trunk/camel-core/src/test/java/org/apache/camel/model/StartingRoutesErrorReportedTest.java Fri Nov 20 10:49:06 2009 @@ -0,0 +1,78 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.camel.model; + +import org.apache.camel.ContextTestSupport; +import org.apache.camel.FailedToCreateRouteException; +import org.apache.camel.builder.RouteBuilder; + +/** + * @version $Revision$ + */ +public class StartingRoutesErrorReportedTest extends ContextTestSupport { + + public void testInvalidFrom() throws Exception { + try { + context.addRoutes(new RouteBuilder() { + @Override + public void configure() throws Exception { + from("direct:start?foo=bar").routeId("route1").to("mock:result"); + } + }); + context.start(); + fail(); + } catch (FailedToCreateRouteException e) { + assertTrue(e.getMessage().startsWith("Failed to create route route1: Route[[From[direct:start?foo=bar]] -> [To[mock:result]]] because of")); + } + } + + public void testInvalidTo() throws Exception { + try { + context.addRoutes(new RouteBuilder() { + @Override + public void configure() throws Exception { + from("direct:start").routeId("route2").to("mock:result?foo=bar"); + } + }); + context.start(); + fail(); + } catch (FailedToCreateRouteException e) { + assertTrue(e.getMessage().startsWith("Failed to create route route2 at: >>> To[mock:result?foo=bar] <<< in route: Route[[From[direct://start]] -> [To[mock:result?foo=bar]]] because of")); + } + } + + public void testInvalidBean() throws Exception { + try { + context.addRoutes(new RouteBuilder() { + @Override + public void configure() throws Exception { + from("direct:start").routeId("route3") + .to("mock:foo") + .beanRef(""); + } + }); + context.start(); + } catch (FailedToCreateRouteException e) { + assertTrue(e.getMessage().startsWith("Failed to create route route3 at: >>> Bean[ref:] <<< in route: Route[[From[direct://start]] -> [To[mock://foo], Bean[ref:]]... because of")); + } + } + + @Override + public boolean isUseRouteBuilder() { + return false; + } +} Propchange: camel/trunk/camel-core/src/test/java/org/apache/camel/model/StartingRoutesErrorReportedTest.java ------------------------------------------------------------------------------ svn:eol-style = native Propchange: camel/trunk/camel-core/src/test/java/org/apache/camel/model/StartingRoutesErrorReportedTest.java ------------------------------------------------------------------------------ svn:keywords = Rev Date Modified: camel/trunk/camel-core/src/test/java/org/apache/camel/processor/CreateRouteWithNonExistingEndpointTest.java URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/test/java/org/apache/camel/processor/CreateRouteWithNonExistingEndpointTest.java?rev=882492&r1=882491&r2=882492&view=diff ============================================================================== --- camel/trunk/camel-core/src/test/java/org/apache/camel/processor/CreateRouteWithNonExistingEndpointTest.java (original) +++ camel/trunk/camel-core/src/test/java/org/apache/camel/processor/CreateRouteWithNonExistingEndpointTest.java Fri Nov 20 10:49:06 2009 @@ -17,6 +17,7 @@ package org.apache.camel.processor; import org.apache.camel.ContextTestSupport; +import org.apache.camel.FailedToCreateRouteException; import org.apache.camel.NoSuchEndpointException; import org.apache.camel.builder.RouteBuilder; @@ -33,9 +34,10 @@ try { super.setUp(); fail("Should have failed to create this route!"); - } catch (NoSuchEndpointException e) { + } catch (FailedToCreateRouteException e) { log.debug("Caught expected exception: " + e, e); - assertEquals("uri", "thisUriDoesNotExist", e.getUri()); + NoSuchEndpointException nse = assertIsInstanceOf(NoSuchEndpointException.class, e.getCause()); + assertEquals("uri", "thisUriDoesNotExist", nse.getUri()); } } Modified: camel/trunk/components/camel-spring/src/test/java/org/apache/camel/component/xslt/InvalidXsltFileTest.java URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-spring/src/test/java/org/apache/camel/component/xslt/InvalidXsltFileTest.java?rev=882492&r1=882491&r2=882492&view=diff ============================================================================== --- camel/trunk/components/camel-spring/src/test/java/org/apache/camel/component/xslt/InvalidXsltFileTest.java (original) +++ camel/trunk/components/camel-spring/src/test/java/org/apache/camel/component/xslt/InvalidXsltFileTest.java Fri Nov 20 10:49:06 2009 @@ -17,6 +17,7 @@ package org.apache.camel.component.xslt; import org.apache.camel.CamelContext; +import org.apache.camel.FailedToCreateRouteException; import org.apache.camel.ResolveEndpointFailedException; import org.apache.camel.TestSupport; import org.apache.camel.builder.RouteBuilder; @@ -34,9 +35,10 @@ context.addRoutes(builder); context.start(); - fail("Should have thrown a ResolveEndpointFailedException due XSL compilation error"); - } catch (ResolveEndpointFailedException e) { + fail("Should have thrown an exception due XSL compilation error"); + } catch (FailedToCreateRouteException e) { // expected + assertIsInstanceOf(ResolveEndpointFailedException.class, e.getCause()); } } Modified: camel/trunk/components/camel-spring/src/test/java/org/apache/camel/spring/issues/SpringMainStartFailedIssueTest.java URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-spring/src/test/java/org/apache/camel/spring/issues/SpringMainStartFailedIssueTest.java?rev=882492&r1=882491&r2=882492&view=diff ============================================================================== --- camel/trunk/components/camel-spring/src/test/java/org/apache/camel/spring/issues/SpringMainStartFailedIssueTest.java (original) +++ camel/trunk/components/camel-spring/src/test/java/org/apache/camel/spring/issues/SpringMainStartFailedIssueTest.java Fri Nov 20 10:49:06 2009 @@ -16,7 +16,7 @@ */ package org.apache.camel.spring.issues; -import org.apache.camel.ResolveEndpointFailedException; +import org.apache.camel.FailedToCreateRouteException; import org.apache.camel.TestSupport; import org.apache.camel.spring.Main; @@ -32,8 +32,8 @@ try { main.run(args); fail("Should have thrown an exception"); - } catch (ResolveEndpointFailedException e) { - // expected + } catch (Exception e) { + assertIsInstanceOf(FailedToCreateRouteException.class, e.getCause()); } assertNull("Spring application context should NOT be created", main.getApplicationContext()); Modified: camel/trunk/components/camel-spring/src/test/java/org/apache/camel/spring/management/SpringCamelContextStartingFailedEventTest.java URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-spring/src/test/java/org/apache/camel/spring/management/SpringCamelContextStartingFailedEventTest.java?rev=882492&r1=882491&r2=882492&view=diff ============================================================================== --- camel/trunk/components/camel-spring/src/test/java/org/apache/camel/spring/management/SpringCamelContextStartingFailedEventTest.java (original) +++ camel/trunk/components/camel-spring/src/test/java/org/apache/camel/spring/management/SpringCamelContextStartingFailedEventTest.java Fri Nov 20 10:49:06 2009 @@ -16,6 +16,7 @@ */ package org.apache.camel.spring.management; +import org.apache.camel.FailedToCreateRouteException; import org.apache.camel.ResolveEndpointFailedException; import org.apache.camel.spring.SpringTestSupport; import org.springframework.context.support.AbstractXmlApplicationContext; @@ -31,7 +32,8 @@ try { answer = new ClassPathXmlApplicationContext("org/apache/camel/spring/management/SpringCamelContextStartingFailedEventTest.xml"); fail("Should thrown an exception"); - } catch (ResolveEndpointFailedException e) { + } catch (Exception e) { + assertIsInstanceOf(ResolveEndpointFailedException.class, e.getCause().getCause()); // expected } Modified: camel/trunk/components/camel-spring/src/test/java/org/apache/camel/spring/processor/SpringDeadLetterChannelInvalidDeadLetterUriTest.java URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-spring/src/test/java/org/apache/camel/spring/processor/SpringDeadLetterChannelInvalidDeadLetterUriTest.java?rev=882492&r1=882491&r2=882492&view=diff ============================================================================== --- camel/trunk/components/camel-spring/src/test/java/org/apache/camel/spring/processor/SpringDeadLetterChannelInvalidDeadLetterUriTest.java (original) +++ camel/trunk/components/camel-spring/src/test/java/org/apache/camel/spring/processor/SpringDeadLetterChannelInvalidDeadLetterUriTest.java Fri Nov 20 10:49:06 2009 @@ -36,8 +36,9 @@ try { super.setUp(); fail("Should have thrown an exception"); - } catch (NoSuchEndpointException e) { - assertEquals("No endpoint could be found for: xxx, please check your classpath contains the needed camel component jar.", e.getMessage()); + } catch (Exception e) { + NoSuchEndpointException cause = assertIsInstanceOf(NoSuchEndpointException.class, e.getCause().getCause()); + assertEquals("No endpoint could be found for: xxx, please check your classpath contains the needed camel component jar.", cause.getMessage()); } } Modified: camel/trunk/components/camel-spring/src/test/java/org/apache/camel/spring/processor/SpringDeadLetterChannelInvalidOptionDeadLetterUriTest.java URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-spring/src/test/java/org/apache/camel/spring/processor/SpringDeadLetterChannelInvalidOptionDeadLetterUriTest.java?rev=882492&r1=882491&r2=882492&view=diff ============================================================================== --- camel/trunk/components/camel-spring/src/test/java/org/apache/camel/spring/processor/SpringDeadLetterChannelInvalidOptionDeadLetterUriTest.java (original) +++ camel/trunk/components/camel-spring/src/test/java/org/apache/camel/spring/processor/SpringDeadLetterChannelInvalidOptionDeadLetterUriTest.java Fri Nov 20 10:49:06 2009 @@ -36,8 +36,9 @@ try { super.setUp(); fail("Should have thrown an exception"); - } catch (ResolveEndpointFailedException e) { - assertTrue(e.getMessage().endsWith("Unknown parameters=[{foo=bar}]")); + } catch (Exception e) { + ResolveEndpointFailedException cause = assertIsInstanceOf(ResolveEndpointFailedException.class, e.getCause().getCause()); + assertTrue(cause.getMessage().endsWith("Unknown parameters=[{foo=bar}]")); } }