{"record":{"id":"2a39101cf31365c2","repo":"pentaho/pentaho-kettle","slug":"the-function-call-ltrim-is-not-valid","errorCode":null,"errorMessage":"The function call ltrim is not valid : ","messagePattern":"The function call ltrim is not valid : ","errorType":"exception","errorClass":"RuntimeException","httpStatus":null,"severity":"error","filePath":"engine/src/main/java/org/pentaho/di/trans/steps/script/ScriptAddedFunctions.java","lineNumber":754,"sourceCode":"    return Boolean.valueOf( bRC );\n  }\n\n  public static String ltrim( ScriptEngine actualContext, Bindings actualObject, Object[] ArgList,\n    Object FunctionContext ) {\n    try {\n      if ( ArgList.length == 1 ) {\n        if ( isNull( ArgList[0] ) ) {\n          return null;\n        } else if ( isUndefined( ArgList[0] ) ) {\n          return (String) undefinedValue;\n        }\n        String strValueToTrim = (String) ArgList[0];\n        return strValueToTrim.replaceAll( \"^\\\\s+\", \"\" );\n      } else {\n        throw new RuntimeException( \"The function call ltrim requires 1 argument.\" );\n      }\n    } catch ( Exception e ) {\n      throw new RuntimeException( \"The function call ltrim is not valid : \" + e.getMessage() );\n    }\n  }\n\n  public static String rtrim( ScriptEngine actualContext, Bindings actualObject, Object[] ArgList,\n    Object FunctionContext ) {\n    try {\n      if ( ArgList.length == 1 ) {\n        if ( isNull( ArgList[0] ) ) {\n          return null;\n        } else if ( isUndefined( ArgList[0] ) ) {\n          return (String) undefinedValue;\n        }\n        String strValueToTrim = (String) ArgList[0];\n        return strValueToTrim.replaceAll( \"\\\\s+$\", \"\" );\n      } else {\n        throw new RuntimeException( \"The function call rtrim requires 1 argument.\" );\n      }\n    } catch ( Exception e ) {","sourceCodeStart":736,"sourceCodeEnd":772,"githubUrl":"https://github.com/pentaho/pentaho-kettle/blob/f3058517a153da500bf4551f46d79b91bf8ec552/engine/src/main/java/org/pentaho/di/trans/steps/script/ScriptAddedFunctions.java#L736-L772","documentation":"ltrim wraps all processing in a try/catch; any exception (most commonly a ClassCastException when ArgList[0] is not a String) is rethrown as RuntimeException('The function call ltrim is not valid : ' + e.getMessage()). Unlike the other wrappers it uses getMessage(), which is often null, yielding a message that ends with a dangling colon and no cause.","triggerScenarios":"Calling ltrim with a non-string first argument (number, date, null) so the (String) cast or replaceAll call throws inside the try block.","commonSituations":"Passing numeric stream fields directly without String() conversion; passing null dates; empty bindings where the variable is undefined (use isUndefined first).","solutions":["Convert the argument to a string first: ltrim(String(value)).","Because getMessage() may be null, log e in the script or test the argument type before calling to get a clearer failure.","Guard null/undefined with isUndefined(ArgList[0]) before calling ltrim.","Coerce numeric fields with str2num/str functions or JavaScript String() before trimming."],"exampleFix":"// before\nvar trimmed = ltrim(idField); // idField is a number\n// after\nvar trimmed = ltrim(String(idField));","handlingStrategy":"type-guard","validationCode":"if (str === null || str === undefined) return str;\nif (typeof str !== 'string') str = String(str);\nvar trimmed = ltrim(str);","typeGuard":"function isNonEmptyString(v) { return typeof v === 'string' && v.length > 0; }","tryCatchPattern":"try {\n  var trimmed = ltrim(str);\n} catch (e) {\n  // getMessage() may be null; wrap with argument info\n  throw new Error('ltrim failed on value: ' + str + ' (' + e + ')');\n}","preventionTips":["Convert non-string fields with String(value) before trimming.","Check isUndefined(ArgList[0]) for potentially missing stream fields.","Watch for the dangling-colon message: it means a non-string argument reached the cast.","Normalize numeric and date fields to strings early in the script."],"tags":["javascript","type-cast","poor-error-message","pentaho-kettle"],"backgroundTag":"type-mismatch","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"}