{"record":{"id":"888beb14b8bd565b","repo":"pentaho/pentaho-kettle","slug":"e-tostring-scriptvaluesaddedfunctions","errorCode":null,"errorMessage":"e.toString()","messagePattern":"e\\.toString\\(\\)","errorType":"exception","errorClass":"RhinoRuntimeError","httpStatus":null,"severity":"error","filePath":"engine/src/main/java/org/pentaho/di/trans/steps/scriptvalues_mod/ScriptValuesAddedFunctions.java","lineNumber":1389,"sourceCode":"        try {\n          if ( isNull( ArgList, new int[] { 0, 1, 2 } ) ) {\n            return null;\n          } else if ( isUndefined( ArgList, new int[] { 0, 1, 2 } ) ) {\n            return (String) Context.getUndefinedValue();\n          }\n          double sArg1 = Context.toNumber( ArgList[0] );\n          if ( Double.isNaN( sArg1 ) ) {\n            throw Context.reportRuntimeError( \"The first Argument must be a Number.\" );\n          }\n          String sArg2 = Context.toString( ArgList[1] );\n          String sArg3 = Context.toString( ArgList[2] );\n          if ( sArg3.length() == 2 ) {\n            DecimalFormatSymbols dfs = new DecimalFormatSymbols( EnvUtil.createLocale( sArg3.toLowerCase() ) );\n            DecimalFormat formatter = new DecimalFormat( sArg2, dfs );\n            sRC = formatter.format( sArg1 );\n          }\n        } catch ( Exception e ) {\n          throw Context.reportRuntimeError( e.toString() );\n        }\n        break;\n      default:\n        throw Context.reportRuntimeError( \"The function call num2str requires 1, 2, or 3 arguments.\" );\n    }\n\n    return sRC;\n  }\n\n  // Converts the given String to a JScript Numeric\n  public static Object str2num( Context actualContext, Scriptable actualObject, Object[] ArgList,\n    Function FunctionContext ) {\n    double dRC = 0.00;\n    switch ( ArgList.length ) {\n      case 0:\n        throw Context.reportRuntimeError( \"The function call str2num requires at least 1 argument.\" );\n      case 1:\n        try {","sourceCodeStart":1371,"sourceCodeEnd":1407,"githubUrl":"https://github.com/pentaho/pentaho-kettle/blob/f3058517a153da500bf4551f46d79b91bf8ec552/engine/src/main/java/org/pentaho/di/trans/steps/scriptvalues_mod/ScriptValuesAddedFunctions.java#L1371-L1407","documentation":"The 3-argument form of num2str wraps all its work in a catch (Exception e) and rethrows the exception's toString() as a Rhino runtime error. Any failure — invalid pattern, invalid locale code from EnvUtil.createLocale, DecimalFormat construction errors — surfaces as this opaque e.toString() message rather than a friendly text.","triggerScenarios":"num2str(number, pattern, lang) throwing any Exception: invalid DecimalFormat pattern in arg 2, or a language code in arg 3 that EnvUtil.createLocale rejects (e.g. num2str(1.5, \"#.##\", \"xyz\") with an unprocessable code), or any other internal exception during formatting.","commonSituations":"Typo in the language code or passing a full locale like \"de_DE\" when a 2-letter code is expected (length must be exactly 2 to take the locale branch); malformed pattern strings; dynamic pattern values from fields that are empty strings.","solutions":["Read the e.toString() content to identify whether the pattern or the locale code failed.","Pass exactly a 2-character lowercase language code as third argument (e.g. \"en\", \"de\", \"fr\").","Validate the DecimalFormat pattern independently (arg 2) — common cause.","Simplify: drop the third argument and use the 2-argument form if locale-specific symbols are not essential."],"exampleFix":"// before\nvar s = num2str(value, \"#,##0.00\", \"de_DE\"); // length 5, or bad pattern\n\n// after\nvar s = num2str(value, \"#,##0.00\", \"de\"); // 2-letter code, valid pattern","handlingStrategy":"validation","validationCode":"function validateNum2str3(value, pattern, lang) {\n  return typeof value === 'number' && !isNaN(value) &&\n         typeof pattern === 'string' && pattern.length > 0 &&\n         typeof lang === 'string' && lang.length === 2;\n}\nif (validateNum2str3(v, p, l)) { var s = num2str(v, p, l); }","typeGuard":null,"tryCatchPattern":"// The error text is e.toString(); inspect it for DecimalFormat or locale clues.\n// Defensive approach: validate args first; avoid triggering the opaque catch.","preventionTips":["Validate all three arguments before calling.","Read e.toString() carefully — it contains the real Java exception.","Use the 2-argument form unless locale symbols are truly needed."],"tags":["javascript","kettle","locale","decimalformat","opaque-error"],"backgroundTag":"invalid-argument-format","analyzedSha":"f3058517a153da500bf4551f46d79b91bf8ec552","analyzedAt":"2026-09-13T14:04:16.340Z","contentChangedAt":"2026-09-13T14:04:16.340Z","schemaVersion":2},"datasetVersion":"2026-09-20T23:17:15.980Z"}