CAMEL-6455: Improved exception message when bean parameter binding fails.
Project: http://git-wip-us.apache.org/repos/asf/camel/repo Commit: http://git-wip-us.apache.org/repos/asf/camel/commit/b5c5c262 Tree: http://git-wip-us.apache.org/repos/asf/camel/tree/b5c5c262 Diff: http://git-wip-us.apache.org/repos/asf/camel/diff/b5c5c262 Branch: refs/heads/master Commit: b5c5c262f5304e6c4ca98fd165a62681f309b052 Parents: 8243498 Author: Claus Ibsen <[email protected]> Authored: Thu Jun 20 13:24:23 2013 +0200 Committer: Claus Ibsen <[email protected]> Committed: Thu Jun 20 13:24:23 2013 +0200 ---------------------------------------------------------------------- .../apache/camel/component/bean/MethodInfo.java | 27 +++++---- .../bean/ParameterBindingException.java | 59 ++++++++++++++++++++ ...eterAndNoMethodWithNoParameterIssueTest.java | 4 +- ...nNoTypeConvertionPossibleWhenHeaderTest.java | 7 ++- .../bean/BeanOverloadedMethodFQNTest.java | 4 +- .../bean/BeanParameterInvalidValueTest.java | 4 +- 6 files changed, 88 insertions(+), 17 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/camel/blob/b5c5c262/camel-core/src/main/java/org/apache/camel/component/bean/MethodInfo.java ---------------------------------------------------------------------- diff --git a/camel-core/src/main/java/org/apache/camel/component/bean/MethodInfo.java b/camel-core/src/main/java/org/apache/camel/component/bean/MethodInfo.java index 205e285..721a2c5 100644 --- a/camel-core/src/main/java/org/apache/camel/component/bean/MethodInfo.java +++ b/camel-core/src/main/java/org/apache/camel/component/bean/MethodInfo.java @@ -535,17 +535,19 @@ public class MethodInfo { if (parameterValue instanceof String) { parameterValue = StringHelper.removeLeadingAndEndingQuotes((String) parameterValue); } - try { - // its a valid parameter value, so convert it to the expected type of the parameter - answer = exchange.getContext().getTypeConverter().mandatoryConvertTo(parameterType, exchange, parameterValue); - if (LOG.isTraceEnabled()) { - LOG.trace("Parameter #{} evaluated as: {} type: ", new Object[]{index, answer, ObjectHelper.type(answer)}); + if (parameterValue != null) { + try { + // its a valid parameter value, so convert it to the expected type of the parameter + answer = exchange.getContext().getTypeConverter().mandatoryConvertTo(parameterType, exchange, parameterValue); + if (LOG.isTraceEnabled()) { + LOG.trace("Parameter #{} evaluated as: {} type: ", new Object[]{index, answer, ObjectHelper.type(answer)}); + } + } catch (Exception e) { + if (LOG.isDebugEnabled()) { + LOG.debug("Cannot convert from type: {} to type: {} for parameter #{}", new Object[]{ObjectHelper.type(parameterValue), parameterType, index}); + } + throw new ParameterBindingException(e, method, index, parameterType, parameterValue); } - } catch (NoTypeConversionAvailableException e) { - if (LOG.isDebugEnabled()) { - LOG.debug("Cannot convert from type: {} to type: {} for parameter #{}", new Object[]{ObjectHelper.type(parameterValue), parameterType, index}); - } - throw ObjectHelper.wrapCamelExecutionException(exchange, e); } } } @@ -570,7 +572,10 @@ public class MethodInfo { LOG.trace("Parameter #{} evaluated as: {} type: ", new Object[]{index, answer, ObjectHelper.type(answer)}); } } catch (NoTypeConversionAvailableException e) { - throw ObjectHelper.wrapCamelExecutionException(exchange, e); + if (LOG.isDebugEnabled()) { + LOG.debug("Cannot convert from type: {} to type: {} for parameter #{}", new Object[]{ObjectHelper.type(result), parameterType, index}); + } + throw new ParameterBindingException(e, method, index, parameterType, result); } } else { LOG.trace("Parameter #{} evaluated as null", index); http://git-wip-us.apache.org/repos/asf/camel/blob/b5c5c262/camel-core/src/main/java/org/apache/camel/component/bean/ParameterBindingException.java ---------------------------------------------------------------------- diff --git a/camel-core/src/main/java/org/apache/camel/component/bean/ParameterBindingException.java b/camel-core/src/main/java/org/apache/camel/component/bean/ParameterBindingException.java new file mode 100644 index 0000000..f59bdeb --- /dev/null +++ b/camel-core/src/main/java/org/apache/camel/component/bean/ParameterBindingException.java @@ -0,0 +1,59 @@ +/** + * 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.component.bean; + +import java.lang.reflect.Method; + +import org.apache.camel.RuntimeCamelException; +import org.apache.camel.util.ObjectHelper; + +public class ParameterBindingException extends RuntimeCamelException { + + private Method method; + private int index; + private Class<?> parameterType; + private Object parameterValue; + + public ParameterBindingException(Throwable cause, Method method, int index, Class<?> parameterType, Object parameterValue) { + super(createMessage(method, index, parameterType, parameterValue), cause); + this.method = method; + this.index = index; + this.parameterType = parameterType; + this.parameterValue = parameterValue; + } + + public Method getMethod() { + return method; + } + + public int getIndex() { + return index; + } + + public Class<?> getParameterType() { + return parameterType; + } + + public Object getParameterValue() { + return parameterValue; + } + + private static String createMessage(Method method, int index, Class<?> parameterType, Object parameterValue) { + return "Error during parameter binding on method: " + method + " at parameter #" + index + " with type: " + parameterType + + " with value type: " + ObjectHelper.type(parameterValue) + " and value: " + parameterValue; + } +} http://git-wip-us.apache.org/repos/asf/camel/blob/b5c5c262/camel-core/src/test/java/org/apache/camel/component/bean/BeanMethodWithEmptyParameterAndNoMethodWithNoParameterIssueTest.java ---------------------------------------------------------------------- diff --git a/camel-core/src/test/java/org/apache/camel/component/bean/BeanMethodWithEmptyParameterAndNoMethodWithNoParameterIssueTest.java b/camel-core/src/test/java/org/apache/camel/component/bean/BeanMethodWithEmptyParameterAndNoMethodWithNoParameterIssueTest.java index 45c0292..3058eb8 100644 --- a/camel-core/src/test/java/org/apache/camel/component/bean/BeanMethodWithEmptyParameterAndNoMethodWithNoParameterIssueTest.java +++ b/camel-core/src/test/java/org/apache/camel/component/bean/BeanMethodWithEmptyParameterAndNoMethodWithNoParameterIssueTest.java @@ -26,6 +26,8 @@ import org.apache.camel.impl.JndiRegistry; */ public class BeanMethodWithEmptyParameterAndNoMethodWithNoParameterIssueTest extends ContextTestSupport { + // TODO: CAMEL-6455 + @Override protected JndiRegistry createRegistry() throws Exception { JndiRegistry jndi = super.createRegistry(); @@ -48,7 +50,7 @@ public class BeanMethodWithEmptyParameterAndNoMethodWithNoParameterIssueTest ext @Override public void configure() throws Exception { from("direct:start") - .to("bean:myBean?method=doSomething()") + .to("bean:myBean?method=doSomething(*)") .to("mock:result"); } }; http://git-wip-us.apache.org/repos/asf/camel/blob/b5c5c262/camel-core/src/test/java/org/apache/camel/component/bean/BeanNoTypeConvertionPossibleWhenHeaderTest.java ---------------------------------------------------------------------- diff --git a/camel-core/src/test/java/org/apache/camel/component/bean/BeanNoTypeConvertionPossibleWhenHeaderTest.java b/camel-core/src/test/java/org/apache/camel/component/bean/BeanNoTypeConvertionPossibleWhenHeaderTest.java index 8faca46..1f3f5b4 100644 --- a/camel-core/src/test/java/org/apache/camel/component/bean/BeanNoTypeConvertionPossibleWhenHeaderTest.java +++ b/camel-core/src/test/java/org/apache/camel/component/bean/BeanNoTypeConvertionPossibleWhenHeaderTest.java @@ -38,7 +38,12 @@ public class BeanNoTypeConvertionPossibleWhenHeaderTest extends ContextTestSuppo template.requestBodyAndHeader("direct:start", "Hello World", "foo", 555); fail("Should have thrown an exception"); } catch (CamelExecutionException e) { - NoTypeConversionAvailableException ntae = assertIsInstanceOf(NoTypeConversionAvailableException.class, e.getCause()); + ParameterBindingException pbe = assertIsInstanceOf(ParameterBindingException.class, e.getCause()); + assertEquals(1, pbe.getIndex()); + assertTrue(pbe.getMethod().getName().contains("hello")); + assertEquals(555, pbe.getParameterValue()); + + NoTypeConversionAvailableException ntae = assertIsInstanceOf(NoTypeConversionAvailableException.class, e.getCause().getCause()); assertEquals(Integer.class, ntae.getFromType()); assertEquals(Document.class, ntae.getToType()); assertEquals(555, ntae.getValue()); http://git-wip-us.apache.org/repos/asf/camel/blob/b5c5c262/camel-core/src/test/java/org/apache/camel/component/bean/BeanOverloadedMethodFQNTest.java ---------------------------------------------------------------------- diff --git a/camel-core/src/test/java/org/apache/camel/component/bean/BeanOverloadedMethodFQNTest.java b/camel-core/src/test/java/org/apache/camel/component/bean/BeanOverloadedMethodFQNTest.java index 3b53add..bf202f6 100644 --- a/camel-core/src/test/java/org/apache/camel/component/bean/BeanOverloadedMethodFQNTest.java +++ b/camel-core/src/test/java/org/apache/camel/component/bean/BeanOverloadedMethodFQNTest.java @@ -66,7 +66,7 @@ public class BeanOverloadedMethodFQNTest extends ContextTestSupport { template.sendBody("direct:start", new MyOrder()); fail("Should have thrown an exception"); } catch (CamelExecutionException e) { - NoTypeConversionAvailableException cause = assertIsInstanceOf(NoTypeConversionAvailableException.class, e.getCause()); + NoTypeConversionAvailableException cause = assertIsInstanceOf(NoTypeConversionAvailableException.class, e.getCause().getCause()); assertEquals("Unknown", cause.getValue()); } } @@ -125,7 +125,7 @@ public class BeanOverloadedMethodFQNTest extends ContextTestSupport { template.sendBody("direct:start", new MyOrder()); fail("Should have thrown an exception"); } catch (CamelExecutionException e) { - NoTypeConversionAvailableException cause = assertIsInstanceOf(NoTypeConversionAvailableException.class, e.getCause()); + NoTypeConversionAvailableException cause = assertIsInstanceOf(NoTypeConversionAvailableException.class, e.getCause().getCause()); assertEquals("org.apache.camel.component.bean.BeanOverloadedMethodFQNTest$Unknown", cause.getValue()); } } http://git-wip-us.apache.org/repos/asf/camel/blob/b5c5c262/camel-core/src/test/java/org/apache/camel/component/bean/BeanParameterInvalidValueTest.java ---------------------------------------------------------------------- diff --git a/camel-core/src/test/java/org/apache/camel/component/bean/BeanParameterInvalidValueTest.java b/camel-core/src/test/java/org/apache/camel/component/bean/BeanParameterInvalidValueTest.java index 416742b..c9a72e1 100644 --- a/camel-core/src/test/java/org/apache/camel/component/bean/BeanParameterInvalidValueTest.java +++ b/camel-core/src/test/java/org/apache/camel/component/bean/BeanParameterInvalidValueTest.java @@ -37,7 +37,7 @@ public class BeanParameterInvalidValueTest extends ContextTestSupport { template.sendBody("direct:a", "World"); fail("Should have thrown exception"); } catch (CamelExecutionException e) { - TypeConversionException cause = assertIsInstanceOf(TypeConversionException.class, e.getCause()); + TypeConversionException cause = assertIsInstanceOf(TypeConversionException.class, e.getCause().getCause()); assertEquals(String.class, cause.getFromType()); assertEquals(int.class, cause.getToType()); assertEquals("A", cause.getValue()); @@ -53,7 +53,7 @@ public class BeanParameterInvalidValueTest extends ContextTestSupport { template.sendBody("direct:b", "World"); fail("Should have thrown exception"); } catch (CamelExecutionException e) { - TypeConversionException cause = assertIsInstanceOf(TypeConversionException.class, e.getCause()); + TypeConversionException cause = assertIsInstanceOf(TypeConversionException.class, e.getCause().getCause()); assertEquals(String.class, cause.getFromType()); assertEquals(int.class, cause.getToType()); assertEquals("true", cause.getValue());
