{"record":{"id":"22a58b9122d22de8","repo":"apple/pkl","slug":"e-getmessage-22a58b","errorCode":null,"errorMessage":"\n+e.getMessage()","messagePattern":"\n\\+e\\.getMessage\\(\\)","errorType":"console","errorClass":"BadValue","httpStatus":null,"severity":"error","filePath":"pkl-core/src/main/java/org/pkl/core/runtime/CommandSpecParser.java","lineNumber":1081,"sourceCode":"  // endregion\n  // region dynamic import handling\n\n  private static boolean isImport(VmTyped value) {\n    return value.getVmClass() == CommandModule.getImportClass();\n  }\n\n  private static boolean isImport(Object value) {\n    return value instanceof VmTyped vmTyped\n        && vmTyped.getVmClass() == CommandModule.getImportClass();\n  }\n\n  // handle errors from convert/transformAll and correctly format them for the CLI\n  private Object handleBadValue(Supplier<Object> f) {\n    try {\n      return handleErrors(f);\n    } catch (Throwable e) {\n      // add a newline so this prints nicely under \"Error: invalid value for <name>:\"\n      throw new BadValue(\"\\n\" + e.getMessage());\n    }\n  }\n\n  private <T> T handleErrors(Supplier<T> f) {\n    try {\n      return f.get();\n    } catch (VmStackOverflowException e) {\n      if (VmUtils.isPklBug(e)) {\n        throw new VmExceptionBuilder()\n            .bug(\"Stack overflow\")\n            .withCause(e.getCause())\n            .build()\n            .toPklException(frameTransformer, color);\n      }\n      throw e.toPklException(frameTransformer, color);\n    } catch (VmException e) {\n      throw e.toPklException(frameTransformer, color);\n    } catch (Exception e) {","sourceCodeStart":1063,"sourceCodeEnd":1099,"githubUrl":"https://github.com/apple/pkl/blob/f3efcbfc9b60d30053b0536d664948d7aa1b8673/pkl-core/src/main/java/org/pkl/core/runtime/CommandSpecParser.java#L1063-L1099","documentation":"CommandSpecParser.handleBadValue wraps any Throwable from value conversion/transform functions into a BadValue whose message is a newline plus e.getMessage(). The reported message is therefore just the underlying exception's message, reformatted so it prints neatly under a CLI 'invalid value for <name>:' line. The real cause is whatever the user-provided convert/transform function threw.","triggerScenarios":"A CLI flag/option value fails conversion: the spec's convert or transformAll function throws (custom converters, parse failures) while parsing command-spec-based CLI arguments.","commonSituations":"Custom `convert { }` blocks in command specs throwing on bad user input; split() or toInt() style failures in transform functions; exceptions thrown by user-provided validation code inside a spec.","solutions":["Read the wrapped message under 'invalid value for <name>:' to identify the failing conversion.","Fix or guard the convert/transformAll function in the command spec so invalid input yields a clear message.","Add explicit input validation in the converter that throws a descriptive message instead of an incidental exception."],"exampleFix":"// before\nconvert { it.toInt() } // NumberFormatException surfaces raw\n// after\nconvert { it.toIntOrNull() ?: throw IllegalArgumentException(\"expected an integer, got: $it\") }","handlingStrategy":"try-catch","validationCode":"// inside a command spec converter\nconvert { v -> require(v.isNotBlank()) { \"value must not be blank\" }; v }","typeGuard":null,"tryCatchPattern":"try {\n  runConversion(value)\n} catch (e: BadValue) {\n  logger.error(\"invalid value for ${flagName}: ${e.message}\")\n}","preventionTips":["Validate flag values inside convert blocks with descriptive messages","Test converters with malformed inputs","Avoid throwing raw runtime exceptions from converters"],"tags":["pkl","cli","bad-value","command-spec"],"backgroundTag":"invalid-cli-argument","analyzedSha":"f3efcbfc9b60d30053b0536d664948d7aa1b8673","analyzedAt":"2026-09-08T13:10:45.570Z","contentChangedAt":"2026-09-08T13:10:45.570Z","schemaVersion":2},"datasetVersion":"2026-09-14T11:17:12.474Z"}