{"record":{"id":"53e5a391e82486a4","repo":"prestodb/presto","slug":"could-not-parse-selected-role","errorCode":null,"errorMessage":"Could not parse selected role: ","messagePattern":"Could not parse selected role: ","errorType":"validation","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"presto-spi/src/main/java/com/facebook/presto/spi/security/SelectedRole.java","lineNumber":120,"sourceCode":"\n    @Override\n    public String toString()\n    {\n        StringBuilder result = new StringBuilder();\n        result.append(type);\n        role.ifPresent(s -> result.append(\"{\").append(s).append(\"}\"));\n        return result.toString();\n    }\n\n    public static SelectedRole valueOf(String value)\n    {\n        Matcher m = PATTERN.matcher(value);\n        if (m.matches()) {\n            Type type = Type.valueOf(m.group(1));\n            Optional<String> role = Optional.ofNullable(m.group(3));\n            return new SelectedRole(type, role);\n        }\n        throw new IllegalArgumentException(\"Could not parse selected role: \" + value);\n    }\n}\n","sourceCodeStart":102,"sourceCodeEnd":123,"githubUrl":"https://github.com/prestodb/presto/blob/55bb57d202de3b926896fa966c2c4a44c779634e/presto-spi/src/main/java/com/facebook/presto/spi/security/SelectedRole.java#L102-L123","documentation":"SelectedRole.valueOf parses the string form '<TYPE> <role>' (e.g. 'ROLE admin', 'NONE') using a regex PATTERN and throws IllegalArgumentException 'Could not parse selected role: <value>' when the string does not match. It is used to round-trip the toString representation back into a SelectedRole.","triggerScenarios":"Calling SelectedRole.valueOf on a string not matching the PATTERN regex — e.g. missing type, unknown type name, wrong separator, empty string, or a value produced by a different/older serialization format.","commonSituations":"Internal tools storing SelectedRole strings in config/DB across Presto version upgrades where the format changed; manually typed values in properties files; interop with clients that serialize roles differently (e.g. 'ROLE:admin' instead of 'ROLE admin').","solutions":["Check the exact expected format in SelectedRole.toString and correct the input (e.g. 'ROLE admin')","Use SelectedRole(Type, Optional) construction directly instead of parsing ad-hoc strings","Handle legacy formats by normalizing separators/type names before calling valueOf","Wrap in try/catch and fall back to SelectedRole(Type.NONE, Optional.empty()) for unparseable legacy values"],"exampleFix":"// before\nSelectedRole.valueOf(\"role=admin\"); // throws\n// after\nSelectedRole.valueOf(\"ROLE admin\");\n// or safer:\ntry { sr = SelectedRole.valueOf(raw); } catch (IllegalArgumentException e) { sr = new SelectedRole(SelectedRole.Type.NONE, Optional.empty()); }","handlingStrategy":"type-guard","validationCode":"// validate format before parsing\nif (!value.matches(\"(NONE|ALL|ROLE)( \\\\S+)?$\")) {\n    throw new IllegalArgumentException(\"Bad SelectedRole string: \" + value);\n}","typeGuard":"Optional<SelectedRole> tryParseSelectedRole(String value) {\n    try {\n        return Optional.of(SelectedRole.valueOf(value));\n    } catch (IllegalArgumentException e) {\n        return Optional.empty();\n    }\n}","tryCatchPattern":"try {\n    return SelectedRole.valueOf(value);\n} catch (IllegalArgumentException e) {\n    LOG.warn(\"Could not parse selected role '%s'; defaulting to NONE\", value);\n    return new SelectedRole(SelectedRole.Type.NONE, Optional.empty());\n}","preventionTips":["Persist SelectedRole via its canonical toString output only","Add a parser round-trip test: valueOf(toString()) for all types","Normalize legacy formats before parsing","Never hand-edit stored role strings without validating the format"],"tags":["presto","parsing","illegal-argument","string-format"],"backgroundTag":"string-parse-failure","analyzedSha":"55bb57d202de3b926896fa966c2c4a44c779634e","analyzedAt":"2026-09-04T12:50:26.162Z","contentChangedAt":"2026-09-04T12:50:26.162Z","schemaVersion":2},"datasetVersion":"2026-09-11T21:17:09.523Z"}