Browse Source

Make EvaluationException more informative

Update EvaluationException to expose the toDetailedString() method as
the exception message. The simple message can now be accessed via the
new getSimpleMessage() method.

Issue: SPR-10938
pull/393/merge
Phillip Webb 12 years ago
parent
commit
014d156f5b
  1. 20
      spring-expression/src/main/java/org/springframework/expression/ExpressionException.java
  2. 22
      spring-expression/src/test/java/org/springframework/expression/spel/SpelReproTests.java
  3. 14
      spring-expression/src/test/java/org/springframework/expression/spel/TemplateExpressionParsingTests.java
  4. 11
      spring-expression/src/test/java/org/springframework/expression/spel/standard/SpelParserTests.java

20
spring-expression/src/main/java/org/springframework/expression/ExpressionException.java

@ -87,6 +87,24 @@ public class ExpressionException extends RuntimeException {
} }
/**
* Return the exception message. Since Spring 4.0 this method returns the same
* result as {@link #toDetailedString()}.
* @see java.lang.Throwable#getMessage()
*/
@Override
public String getMessage() {
return toDetailedString();
}
/**
* Return the exception simple message without including the expression that caused
* the failure.
*/
public String getSimpleMessage() {
return super.getMessage();
}
public String toDetailedString() { public String toDetailedString() {
StringBuilder output = new StringBuilder(); StringBuilder output = new StringBuilder();
if (this.expressionString!=null) { if (this.expressionString!=null) {
@ -99,7 +117,7 @@ public class ExpressionException extends RuntimeException {
} }
output.append(": "); output.append(": ");
} }
output.append(getMessage()); output.append(getSimpleMessage());
return output.toString(); return output.toString();
} }

22
spring-expression/src/test/java/org/springframework/expression/spel/SpelReproTests.java

@ -31,7 +31,6 @@ import org.junit.Ignore;
import org.junit.Rule; import org.junit.Rule;
import org.junit.Test; import org.junit.Test;
import org.junit.rules.ExpectedException; import org.junit.rules.ExpectedException;
import org.springframework.core.MethodParameter; import org.springframework.core.MethodParameter;
import org.springframework.core.convert.TypeDescriptor; import org.springframework.core.convert.TypeDescriptor;
import org.springframework.expression.AccessException; import org.springframework.expression.AccessException;
@ -39,6 +38,7 @@ import org.springframework.expression.BeanResolver;
import org.springframework.expression.EvaluationContext; import org.springframework.expression.EvaluationContext;
import org.springframework.expression.EvaluationException; import org.springframework.expression.EvaluationException;
import org.springframework.expression.Expression; import org.springframework.expression.Expression;
import org.springframework.expression.ExpressionException;
import org.springframework.expression.ExpressionParser; import org.springframework.expression.ExpressionParser;
import org.springframework.expression.MethodExecutor; import org.springframework.expression.MethodExecutor;
import org.springframework.expression.MethodResolver; import org.springframework.expression.MethodResolver;
@ -545,20 +545,26 @@ public class SpelReproTests extends ExpressionTestCase {
assertEquals(expectedValue,expr.getValue(TestScenarioCreator.getTestEvaluationContext())); assertEquals(expectedValue,expr.getValue(TestScenarioCreator.getTestEvaluationContext()));
} }
private void checkTemplateParsingError(String expression,String expectedMessage) throws Exception { private void checkTemplateParsingError(String expression, String expectedMessage)
checkTemplateParsingError(expression, TemplateExpressionParsingTests.DEFAULT_TEMPLATE_PARSER_CONTEXT,expectedMessage); throws Exception {
checkTemplateParsingError(expression, TemplateExpressionParsingTests.DEFAULT_TEMPLATE_PARSER_CONTEXT, expectedMessage);
} }
private void checkTemplateParsingError(String expression,ParserContext context, String expectedMessage) throws Exception { private void checkTemplateParsingError(String expression, ParserContext context,
String expectedMessage) throws Exception {
SpelExpressionParser parser = new SpelExpressionParser(); SpelExpressionParser parser = new SpelExpressionParser();
try { try {
parser.parseExpression(expression,context); parser.parseExpression(expression,context);
fail("Should have failed"); fail("Should have failed");
} catch (Exception e) { } catch (Exception ex) {
if (!e.getMessage().equals(expectedMessage)) { String message = ex.getMessage();
e.printStackTrace(); if (ex instanceof ExpressionException) {
message = ((ExpressionException) ex).getSimpleMessage();
}
if (!message.equals(expectedMessage)) {
ex.printStackTrace();
} }
assertEquals(expectedMessage,e.getMessage()); assertThat(expectedMessage, equalTo(message));
} }
} }

14
spring-expression/src/test/java/org/springframework/expression/spel/TemplateExpressionParsingTests.java

@ -1,5 +1,5 @@
/* /*
* Copyright 2002-2012 the original author or authors. * Copyright 2002-2013 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -193,14 +193,14 @@ public class TemplateExpressionParsingTests extends ExpressionTestCase {
ex = parser.parseExpression("hello ${listOfNumbersUpToTen.$[#this<5]} ${listOfNumbersUpToTen.$[#this>5] world",DEFAULT_TEMPLATE_PARSER_CONTEXT); ex = parser.parseExpression("hello ${listOfNumbersUpToTen.$[#this<5]} ${listOfNumbersUpToTen.$[#this>5] world",DEFAULT_TEMPLATE_PARSER_CONTEXT);
fail("Should have failed"); fail("Should have failed");
} catch (ParseException pe) { } catch (ParseException pe) {
assertEquals("No ending suffix '}' for expression starting at character 41: ${listOfNumbersUpToTen.$[#this>5] world",pe.getMessage()); assertEquals("No ending suffix '}' for expression starting at character 41: ${listOfNumbersUpToTen.$[#this>5] world", pe.getSimpleMessage());
} }
try { try {
ex = parser.parseExpression("hello ${listOfNumbersUpToTen.$[#root.listOfNumbersUpToTen.$[#this%2==1==3]} world",DEFAULT_TEMPLATE_PARSER_CONTEXT); ex = parser.parseExpression("hello ${listOfNumbersUpToTen.$[#root.listOfNumbersUpToTen.$[#this%2==1==3]} world",DEFAULT_TEMPLATE_PARSER_CONTEXT);
fail("Should have failed"); fail("Should have failed");
} catch (ParseException pe) { } catch (ParseException pe) {
assertEquals("Found closing '}' at position 74 but most recent opening is '[' at position 30",pe.getMessage()); assertEquals("Found closing '}' at position 74 but most recent opening is '[' at position 30", pe.getSimpleMessage());
} }
} }
@ -235,20 +235,20 @@ public class TemplateExpressionParsingTests extends ExpressionTestCase {
parser.parseExpression("hello ${'world'", DEFAULT_TEMPLATE_PARSER_CONTEXT); parser.parseExpression("hello ${'world'", DEFAULT_TEMPLATE_PARSER_CONTEXT);
fail("Should have failed"); fail("Should have failed");
} catch (ParseException pe) { } catch (ParseException pe) {
assertEquals("No ending suffix '}' for expression starting at character 6: ${'world'",pe.getMessage()); assertEquals("No ending suffix '}' for expression starting at character 6: ${'world'", pe.getSimpleMessage());
assertEquals("hello ${'world'",pe.getExpressionString()); assertEquals("hello ${'world'", pe.getExpressionString());
} }
try { try {
parser.parseExpression("hello ${'wibble'${'world'}", DEFAULT_TEMPLATE_PARSER_CONTEXT); parser.parseExpression("hello ${'wibble'${'world'}", DEFAULT_TEMPLATE_PARSER_CONTEXT);
fail("Should have failed"); fail("Should have failed");
} catch (ParseException pe) { } catch (ParseException pe) {
assertEquals("No ending suffix '}' for expression starting at character 6: ${'wibble'${'world'}",pe.getMessage()); assertEquals("No ending suffix '}' for expression starting at character 6: ${'wibble'${'world'}", pe.getSimpleMessage());
} }
try { try {
parser.parseExpression("hello ${} world", DEFAULT_TEMPLATE_PARSER_CONTEXT); parser.parseExpression("hello ${} world", DEFAULT_TEMPLATE_PARSER_CONTEXT);
fail("Should have failed"); fail("Should have failed");
} catch (ParseException pe) { } catch (ParseException pe) {
assertEquals("No expression defined within delimiter '${}' at character 6",pe.getMessage()); assertEquals("No expression defined within delimiter '${}' at character 6", pe.getSimpleMessage());
} }
} }

11
spring-expression/src/test/java/org/springframework/expression/spel/standard/SpelParserTests.java

@ -1,5 +1,5 @@
/* /*
* Copyright 2002-2012 the original author or authors. * Copyright 2002-2013 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -342,16 +342,19 @@ public class SpelParserTests {
@Test @Test
public void exceptions() { public void exceptions() {
ExpressionException exprEx = new ExpressionException("test"); ExpressionException exprEx = new ExpressionException("test");
assertEquals("test", exprEx.getMessage()); assertEquals("test", exprEx.getSimpleMessage());
assertEquals("test", exprEx.toDetailedString()); assertEquals("test", exprEx.toDetailedString());
assertEquals("test", exprEx.getMessage());
exprEx = new ExpressionException("wibble", "test"); exprEx = new ExpressionException("wibble", "test");
assertEquals("test", exprEx.getMessage()); assertEquals("test", exprEx.getSimpleMessage());
assertEquals("Expression 'wibble': test", exprEx.toDetailedString()); assertEquals("Expression 'wibble': test", exprEx.toDetailedString());
assertEquals("Expression 'wibble': test", exprEx.getMessage());
exprEx = new ExpressionException("wibble", 3, "test"); exprEx = new ExpressionException("wibble", 3, "test");
assertEquals("test", exprEx.getMessage()); assertEquals("test", exprEx.getSimpleMessage());
assertEquals("Expression 'wibble' @ 3: test", exprEx.toDetailedString()); assertEquals("Expression 'wibble' @ 3: test", exprEx.toDetailedString());
assertEquals("Expression 'wibble' @ 3: test", exprEx.getMessage());
} }
@Test @Test

Loading…
Cancel
Save