jwtk/jjwt · error · IllegalArgumentException
constant [%s] does not exist in enum type %s
Error message
constant [%s] does not exist in enum type %s
What it means
This IllegalArgumentException is thrown by Objects.caseInsensitiveValueOf when none of the supplied enum constants matches the given string case-insensitively via toString(). The library uses it to convert user-provided strings (e.g. algorithm names, header values) into enum values and fails fast when the string is not a valid constant.
Source
Thrown at api/src/main/java/io/jsonwebtoken/lang/Objects.java:213
/**
* Case insensitive alternative to {@link Enum#valueOf(Class, String)}.
*
* @param <E> the concrete Enum type
* @param enumValues the array of all Enum constants in question, usually per Enum.values()
* @param constant the constant to get the enum value of
* @return the enum constant of the specified enum type with the specified case-insensitive name
* @throws IllegalArgumentException if the given constant is not found in the given array
* of enum values. Use {@link #containsConstant(Enum[], String)} as a guard to
* avoid this exception.
*/
public static <E extends Enum<?>> E caseInsensitiveValueOf(E[] enumValues, String constant) {
for (E candidate : enumValues) {
if (candidate.toString().equalsIgnoreCase(constant)) {
return candidate;
}
}
throw new IllegalArgumentException(
String.format("constant [%s] does not exist in enum type %s",
constant, enumValues.getClass().getComponentType().getName()));
}
/**
* Append the given object to the given array, returning a new array
* consisting of the input array contents plus the given object.
*
* @param array the array to append to (can be <code>null</code>)
* @param <A> the type of each element in the specified {@code array}
* @param obj the object to append
* @param <O> the type of the specified object, which must be equal to or extend the <code><A></code> type.
* @return the new array (of the same component type; never <code>null</code>)
*/
public static <A, O extends A> A[] addObjectToArray(A[] array, O obj) {
Class<?> compType = Object.class;
if (array != null) {
compType = array.getClass().getComponentType();View on GitHub (pinned to fb71496164)
Solutions
- Fix the input string to match a valid enum constant (case-insensitive), e.g. "HS256"
- Trim and normalize the string before calling caseInsensitiveValueOf
- Pre-validate with a loop over the enum array checking toString().equalsIgnoreCase(input) to produce a friendlier message
- Catch IllegalArgumentException and surface the list of valid constants to the user
Example fix
// before SigAlg alg = Objects.caseInsensitiveValueOf(SigAlg.values(), rawAlg); // throws on typo // after String normalized = rawAlg == null ? "" : rawAlg.trim(); SigAlg alg = Objects.caseInsensitiveValueOf(SigAlg.values(), normalized);
Defensive patterns
Strategy: validation
Validate before calling
boolean valid = java.util.Arrays.stream(SigAlg.values()).anyMatch(e -> e.toString().equalsIgnoreCase(input));
if (!valid) throw new IllegalArgumentException("Invalid value: " + input); Type guard
static <E extends Enum<?>> boolean isValidConstant(E[] values, String s) {
return s != null && java.util.Arrays.stream(values).anyMatch(e -> e.toString().equalsIgnoreCase(s));
} Try / catch
try {
alg = Objects.caseInsensitiveValueOf(SigAlg.values(), input);
} catch (IllegalArgumentException e) {
throw new IllegalArgumentException("Unknown algorithm '" + input + "', expected one of: " + java.util.Arrays.toString(SigAlg.values()), e);
} Prevention
- Trim and normalize user-supplied enum strings before conversion
- Pre-validate against Enum.valueOf with a toUpperCase/known-list check
- Return the list of valid constants in error messages
- Add unit tests covering all external input strings mapped to enums
When it happens
Trigger: Calling caseInsensitiveValueOf(enumValues, constant) with a string that does not equal (ignoring case) any constant's toString(), e.g. a typo'd algorithm name like "HS356" or extra whitespace.
Common situations: Parsing user/config-supplied enum names into JWT signature algorithms, compression or claim enum types; typos, wrong case assumptions with trailing spaces, or strings from external systems.
Understand the failure class
Background: Invalid enum value errors: "Unknown type", "Invalid scope", "must be one of" — when a string is not on the library's allowed list — this error's family across 23 libraries.
Related errors
- bitLength must be an even multiple of 8
- Unexpected unsecured Claims JWT.
- Unexpected content JWS.
- Unexpected Claims JWS.
- Unexpected content JWE.
AI-assisted analysis of jwtk/jjwt@fb71496164 (2026-09-09).
Data as JSON: /api/errors/d72e24c004bb2fc9.
Report an issue: GitHub.