{"record":{"id":"6f57a572d8053ff8","repo":"apple/pkl","slug":"formatexception-string-obj-getclass","errorCode":null,"errorMessage":"FormatException(\"string\", obj.getClass())","messagePattern":"FormatException\\(\"string\", obj\\.getClass\\(\\)\\)","errorType":"validation","errorClass":"FormatException","httpStatus":null,"severity":"error","filePath":"pkl-core/src/main/java/org/pkl/core/packages/PackageUtils.java","lineNumber":29,"sourceCode":" * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\npackage org.pkl.core.packages;\n\nimport java.net.URISyntaxException;\nimport org.pkl.core.util.ErrorMessages;\nimport org.pkl.core.util.json.Json.FormatException;\nimport org.pkl.core.util.json.Json.JsonParseException;\n\npublic final class PackageUtils {\n  private PackageUtils() {}\n\n  public static PackageUri parsePackageUriWithoutChecksums(Object obj)\n      throws JsonParseException, URISyntaxException {\n    if (!(obj instanceof String string)) {\n      throw new FormatException(\"string\", obj.getClass());\n    }\n    var packageUri = new PackageUri(string);\n    checkHasNoChecksumComponent(packageUri);\n    return packageUri;\n  }\n\n  public static void checkHasNoChecksumComponent(PackageUri packageUri) throws URISyntaxException {\n    if (packageUri.getChecksums() != null) {\n      throw new URISyntaxException(\n          packageUri.toString(), ErrorMessages.create(\"unexpectedChecksumInPackageUri\"));\n    }\n  }\n}\n","sourceCodeStart":11,"sourceCodeEnd":43,"githubUrl":"https://github.com/apple/pkl/blob/f3efcbfc9b60d30053b0536d664948d7aa1b8673/pkl-core/src/main/java/org/pkl/core/packages/PackageUtils.java#L11-L43","documentation":"Thrown by PackageUtils.parsePackageUriWithoutChecksums when the input object is not a String. The API expects the raw package URI text; any other type (e.g. a URI object, char sequence of another type, or deserialized non-string) is a format error.","triggerScenarios":"Calling parsePackageUriWithoutChecksums(obj) where obj is not an instanceof String — e.g. passing java.net.URI, a GString-like wrapper, or JSON-decoded non-string values.","commonSituations":"Deserialized configuration where the package URI field came back as a non-string; passing a URI instance instead of uri.toString(); generic pipelines that forward unchecked values.","solutions":["Call toString() on URI objects before passing: parsePackageUriWithoutChecksums(uri.toString())","Ensure upstream deserialization produces a plain String for the package URI field","Check the input with instanceof String before calling"],"exampleFix":"// before\nvar pkg = PackageUtils.parsePackageUriWithoutChecksums(uriObj);\n// after\nvar pkg = PackageUtils.parsePackageUriWithoutChecksums(uriObj.toString());","handlingStrategy":"type-guard","validationCode":"if (!(input instanceof String s) || s.isBlank()) {\n  throw new IllegalArgumentException(\"Package URI must be a non-empty string\");\n}","typeGuard":"static boolean isStringPackageUri(Object obj) {\n  return obj instanceof String s && s.startsWith(\"package://\");\n}","tryCatchPattern":"if (!(obj instanceof String string)) {\n  throw new IllegalArgumentException(\"Expected a String package URI, got: \" + obj.getClass());\n}\nvar pkg = PackageUtils.parsePackageUriWithoutChecksums(string);","preventionTips":["Call toString() on URI objects before passing","Verify deserialization yields plain Strings for URI fields","Type-check inputs at pipeline boundaries"],"tags":["pkl","type-mismatch","package-uri","argument"],"backgroundTag":"type-mismatch","analyzedSha":"f3efcbfc9b60d30053b0536d664948d7aa1b8673","analyzedAt":"2026-09-08T13:10:45.570Z","contentChangedAt":"2026-09-08T13:10:45.570Z","schemaVersion":2},"datasetVersion":"2026-09-14T16:17:12.679Z"}