{"record":{"id":"0374e03e78c183f9","repo":"dianping/cat","slug":"invalid-level","errorCode":null,"errorMessage":"Invalid level","messagePattern":"Invalid level","errorType":"exception","errorClass":"RuntimeException","httpStatus":null,"severity":"error","filePath":"cat-core/src/main/java/com/dianping/cat/config/Level.java","lineNumber":47,"sourceCode":"\n\tprivate static List<String> m_levels;\n\n\tprivate int m_code;\n\n\tprivate String m_name;\n\n\tprivate Level(int code, String name) {\n\t\tm_code = code;\n\t\tm_name = name;\n\t}\n\n\tpublic static int getCodeByName(String name) {\n\t\tfor (Level level : Level.values()) {\n\t\t\tif (level.getName().equals(name)) {\n\t\t\t\treturn level.getCode();\n\t\t\t}\n\t\t}\n\t\tthrow new RuntimeException(\"Invalid level\");\n\t}\n\n\tpublic static String getNameByCode(int code) {\n\t\tfor (Level level : Level.values()) {\n\t\t\tif (level.getCode() == code) {\n\t\t\t\treturn level.getName();\n\t\t\t}\n\t\t}\n\t\tthrow new RuntimeException(\"Invalid level\");\n\t}\n\n\tpublic static List<String> getLevels() {\n\t\tif (m_levels == null) {\n\t\t\tm_levels = new ArrayList<String>();\n\n\t\t\tfor (Level level : Level.values()) {\n\t\t\t\tm_levels.add(level.getName());\n\t\t\t}","sourceCodeStart":29,"sourceCodeEnd":65,"githubUrl":"https://github.com/dianping/cat/blob/e815e74d4c2dd74edac831241f1253fcc7d25381/cat-core/src/main/java/com/dianping/cat/config/Level.java#L29-L65","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","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."],"exampleFix":"// before\nint code = Level.getCodeByName(\"WARN\"); // RuntimeException: Invalid level\n\n// after\nint code = Level.getCodeByName(\"WARNING\");\n// or defensively:\nString name = configured == null ? \"INFO\" : configured.toUpperCase();\nint code = Level.getCodeByName(name);","handlingStrategy":"validation","validationCode":"public static boolean isValidLevelName(String name) {\n    for (Level l : Level.values()) if (l.getName().equals(name)) return true;\n    return false;\n}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["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."],"tags":["enum","configuration","validation"],"backgroundTag":null,"analyzedSha":"e815e74d4c2dd74edac831241f1253fcc7d25381","analyzedAt":"2026-08-14T14:22:34.512Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}