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
This commit is contained in:
@@ -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() {
|
||||
StringBuilder output = new StringBuilder();
|
||||
if (this.expressionString!=null) {
|
||||
@@ -99,7 +117,7 @@ public class ExpressionException extends RuntimeException {
|
||||
}
|
||||
output.append(": ");
|
||||
}
|
||||
output.append(getMessage());
|
||||
output.append(getSimpleMessage());
|
||||
return output.toString();
|
||||
}
|
||||
|
||||
|
||||
@@ -31,7 +31,6 @@ import org.junit.Ignore;
|
||||
import org.junit.Rule;
|
||||
import org.junit.Test;
|
||||
import org.junit.rules.ExpectedException;
|
||||
|
||||
import org.springframework.core.MethodParameter;
|
||||
import org.springframework.core.convert.TypeDescriptor;
|
||||
import org.springframework.expression.AccessException;
|
||||
@@ -39,6 +38,7 @@ import org.springframework.expression.BeanResolver;
|
||||
import org.springframework.expression.EvaluationContext;
|
||||
import org.springframework.expression.EvaluationException;
|
||||
import org.springframework.expression.Expression;
|
||||
import org.springframework.expression.ExpressionException;
|
||||
import org.springframework.expression.ExpressionParser;
|
||||
import org.springframework.expression.MethodExecutor;
|
||||
import org.springframework.expression.MethodResolver;
|
||||
@@ -545,20 +545,26 @@ public class SpelReproTests extends ExpressionTestCase {
|
||||
assertEquals(expectedValue,expr.getValue(TestScenarioCreator.getTestEvaluationContext()));
|
||||
}
|
||||
|
||||
private void checkTemplateParsingError(String expression,String expectedMessage) throws Exception {
|
||||
checkTemplateParsingError(expression, TemplateExpressionParsingTests.DEFAULT_TEMPLATE_PARSER_CONTEXT,expectedMessage);
|
||||
private void checkTemplateParsingError(String expression, String 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();
|
||||
try {
|
||||
parser.parseExpression(expression,context);
|
||||
fail("Should have failed");
|
||||
} catch (Exception e) {
|
||||
if (!e.getMessage().equals(expectedMessage)) {
|
||||
e.printStackTrace();
|
||||
} catch (Exception ex) {
|
||||
String message = ex.getMessage();
|
||||
if (ex instanceof ExpressionException) {
|
||||
message = ((ExpressionException) ex).getSimpleMessage();
|
||||
}
|
||||
assertEquals(expectedMessage,e.getMessage());
|
||||
if (!message.equals(expectedMessage)) {
|
||||
ex.printStackTrace();
|
||||
}
|
||||
assertThat(expectedMessage, equalTo(message));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -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");
|
||||
* 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);
|
||||
fail("Should have failed");
|
||||
} 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 {
|
||||
ex = parser.parseExpression("hello ${listOfNumbersUpToTen.$[#root.listOfNumbersUpToTen.$[#this%2==1==3]} world",DEFAULT_TEMPLATE_PARSER_CONTEXT);
|
||||
fail("Should have failed");
|
||||
} 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);
|
||||
fail("Should have failed");
|
||||
} catch (ParseException pe) {
|
||||
assertEquals("No ending suffix '}' for expression starting at character 6: ${'world'",pe.getMessage());
|
||||
assertEquals("hello ${'world'",pe.getExpressionString());
|
||||
assertEquals("No ending suffix '}' for expression starting at character 6: ${'world'", pe.getSimpleMessage());
|
||||
assertEquals("hello ${'world'", pe.getExpressionString());
|
||||
}
|
||||
try {
|
||||
parser.parseExpression("hello ${'wibble'${'world'}", DEFAULT_TEMPLATE_PARSER_CONTEXT);
|
||||
fail("Should have failed");
|
||||
} 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 {
|
||||
parser.parseExpression("hello ${} world", DEFAULT_TEMPLATE_PARSER_CONTEXT);
|
||||
fail("Should have failed");
|
||||
} 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());
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -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");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -342,16 +342,19 @@ public class SpelParserTests {
|
||||
@Test
|
||||
public void exceptions() {
|
||||
ExpressionException exprEx = new ExpressionException("test");
|
||||
assertEquals("test", exprEx.getMessage());
|
||||
assertEquals("test", exprEx.getSimpleMessage());
|
||||
assertEquals("test", exprEx.toDetailedString());
|
||||
assertEquals("test", exprEx.getMessage());
|
||||
|
||||
exprEx = new ExpressionException("wibble", "test");
|
||||
assertEquals("test", exprEx.getMessage());
|
||||
assertEquals("test", exprEx.getSimpleMessage());
|
||||
assertEquals("Expression 'wibble': test", exprEx.toDetailedString());
|
||||
assertEquals("Expression 'wibble': test", exprEx.getMessage());
|
||||
|
||||
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.getMessage());
|
||||
}
|
||||
|
||||
@Test
|
||||
|
||||
Reference in New Issue
Block a user