Skip to content

Commit

Permalink
Improve error message for unsupported character in SpEL expression
Browse files Browse the repository at this point in the history
Prior to this commit, when an unsupported character such as "ü" was
encountered in a SpEL expression, the error message was:

Cannot handle (252) 'ü'

With this commit, the error message is now similar to:

Unsupported character 'ü' (252) encountered at position 5 in expression.

See gh-30580
Closes gh-30602
  • Loading branch information
sbrannen committed Jun 6, 2023
1 parent cdc4497 commit 7b20aef
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 3 deletions.
@@ -1,5 +1,5 @@
/*
* Copyright 2002-2021 the original author or authors.
* Copyright 2002-2023 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.
Expand Down Expand Up @@ -265,7 +265,9 @@ else if (isTwoCharToken(TokenKind.SAFE_NAVI)) {
raiseParseException(this.pos, SpelMessage.UNEXPECTED_ESCAPE_CHAR);
break;
default:
throw new IllegalStateException("Cannot handle (" + (int) ch + ") '" + ch + "'");
throw new IllegalStateException(
"Unsupported character '%s' (%d) encountered at position %d in expression."
.formatted(ch, (int) ch, (this.pos + 1)));
}
}
}
Expand Down
@@ -1,5 +1,5 @@
/*
* Copyright 2002-2022 the original author or authors.
* Copyright 2002-2023 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.
Expand All @@ -24,6 +24,7 @@
import org.springframework.expression.spel.standard.SpelExpressionParser;

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatIllegalStateException;

/**
* Parse some expressions and check we get the AST we expect.
Expand All @@ -42,6 +43,58 @@ class ParsingTests {
@Nested
class Miscellaneous {

@Test
void supportedCharactersInIdentifiers() {
parseCheck("#var='value'");
parseCheck("#Varz='value'");
parseCheck("#VarZ='value'");
parseCheck("#_var='value'");
parseCheck("#$var='value'");
parseCheck("#_$_='value'");

parseCheck("age");
parseCheck("getAge()");
parseCheck("get$age()");
parseCheck("age");
parseCheck("Age");
parseCheck("__age");
parseCheck("get__age()");

parseCheck("person.age");
parseCheck("person.getAge()");
parseCheck("person.get$age()");
parseCheck("person$1.age");
parseCheck("person_1.Age");
parseCheck("person_1.__age");
parseCheck("Person_1.get__age()");
}

@Test
void unsupportedCharactersInIdentifiers() {
// Invalid syntax
assertThatIllegalStateException()
.isThrownBy(() -> parser.parseRaw("apple~banana"))
.withMessage("Unsupported character '~' (126) encountered at position 6 in expression.");

// German characters
assertThatIllegalStateException()
.isThrownBy(() -> parser.parseRaw("begrüssung"))
.withMessage("Unsupported character 'ü' (252) encountered at position 5 in expression.");
assertThatIllegalStateException()
.isThrownBy(() -> parser.parseRaw("Spaß"))
.withMessage("Unsupported character 'ß' (223) encountered at position 4 in expression.");

// Spanish characters
assertThatIllegalStateException()
.isThrownBy(() -> parser.parseRaw("buenos_sueños"))
.withMessage("Unsupported character 'ñ' (241) encountered at position 11 in expression.");

// Chinese characters
assertThatIllegalStateException()
.isThrownBy(() -> parser.parseRaw("have乐趣()"))
.withMessage("Unsupported character '乐' (20048) encountered at position 5 in expression.");
}

@Test
void literalNull() {
parseCheck("null");
Expand Down

0 comments on commit 7b20aef

Please sign in to comment.