dianping/cat · error · RuntimeException
Invalid level
Error message
Invalid level
What it means
Level.getCodeByName(String) iterates the Level enum and throws RuntimeException("Invalid level") when no enum constant's name matches the argument. Level represents CAT's severity names (e.g. INFO/WARNING/ERROR) with numeric codes; any unrecognized spelling is rejected. The message does not echo the bad input, so you must capture the argument yourself.
Source
Thrown at cat-core/src/main/java/com/dianping/cat/config/Level.java:47
private static List<String> m_levels;
private int m_code;
private String m_name;
private Level(int code, String name) {
m_code = code;
m_name = name;
}
public static int getCodeByName(String name) {
for (Level level : Level.values()) {
if (level.getName().equals(name)) {
return level.getCode();
}
}
throw new RuntimeException("Invalid level");
}
public static String getNameByCode(int code) {
for (Level level : Level.values()) {
if (level.getCode() == code) {
return level.getName();
}
}
throw new RuntimeException("Invalid level");
}
public static List<String> getLevels() {
if (m_levels == null) {
m_levels = new ArrayList<String>();
for (Level level : Level.values()) {
m_levels.add(level.getName());
}View on GitHub (pinned to e815e74d4c)
Solutions
- Log the exact name being converted right before the call (the exception message omits it).
- Compare against Level.getLevels() (the list of valid names) and correct the config value to match exactly.
- Normalize case before calling if the enum uses one casing.
- If the level may legitimately be absent, look it up with a loop over Level.values() returning a default instead of calling the throwing helper.
Example fix
// before
int code = Level.getCodeByName("WARN"); // RuntimeException: Invalid level
// after
int code = Level.getCodeByName("WARNING");
// or defensively:
String name = configured == null ? "INFO" : configured.toUpperCase();
int code = Level.getCodeByName(name); Defensive patterns
Strategy: validation
Validate before calling
public static boolean isValidLevelName(String name) {
for (Level l : Level.values()) if (l.getName().equals(name)) return true;
return false;
} Prevention
- Expose valid names from Level.getLevels() in config UIs as an enum dropdown.
- Normalize case and trim before conversion.
- Never pass possibly-null config values directly.
When it happens
Trigger: Level.getCodeByName(name) where name is null, differently cased ('error' vs 'ERROR'), or not a defined level name at all. Typically called when parsing user-supplied or config-supplied level strings.
Common situations: Alert-rule or log-level configuration with a typo or non-standard level ('WARN' vs the enum's 'WARNING'), case-mismatched values from an external system, or a null passed through after a missing config key.
Related errors
- Invalid level.
- Unsupported MetricType Name!
- Expected {name} at line {line}, col {col}.
- Expected end of value but found '{part}'.
- Expected (<'azimuth'>) but found '{}'.
AI-assisted analysis of dianping/cat@e815e74d4c (2026-08-14).
Data as JSON: /api/errors/0374e03e78c183f9.
Report an issue: GitHub.