{"record":{"id":"8445163b561dff4c","repo":"karatelabs/karate","slug":"string-raw-template-must-be-an-object","errorCode":null,"errorMessage":"String.raw template must be an object","messagePattern":"String\\.raw template must be an object","errorType":"exception","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"karate-js/src/main/java/io/karatelabs/js/JsStringConstructor.java","lineNumber":92,"sourceCode":"                    throw JsErrorException.rangeError(\"Invalid code point \" + num);\n                }\n                sb.appendCodePoint(n);\n            }\n        }\n        return sb.toString();\n    }\n\n    // Spec §22.1.2.4 — String.raw(template, ...substitutions). Walks\n    // template.raw[k] for k in [0, length), interleaving substitutions[k] from\n    // the second arg onward. Coercion goes through the spec ToString helper so\n    // host objects with a JS toString return user-visible strings, and\n    // non-array-like raw values (length=NaN/0) fall through to the empty\n    // string per §22.1.2.4 step 6 / 8.\n    private Object raw(Context context, Object[] args) {\n        Object template = args.length > 0 ? args[0] : Terms.UNDEFINED;\n        Terms.requireObjectCoercible(template, \"String.raw\");\n        if (!(template instanceof ObjectLike templateObj)) {\n            throw JsErrorException.typeError(\"String.raw template must be an object\");\n        }\n        Object rawObj = templateObj.getMember(\"raw\");\n        Terms.requireObjectCoercible(rawObj, \"String.raw .raw\");\n        if (!(rawObj instanceof ObjectLike raw)) {\n            throw JsErrorException.typeError(\"String.raw .raw must be an object\");\n        }\n        CoreContext cc = context instanceof CoreContext c ? c : null;\n        // Spec ToLength(raw.length): NaN / negative / undefined → 0.\n        double rawLen = Terms.objectToNumber(raw.getMember(\"length\")).doubleValue();\n        if (Double.isNaN(rawLen) || rawLen <= 0) return \"\";\n        long length = rawLen >= Integer.MAX_VALUE ? Integer.MAX_VALUE : (long) rawLen;\n        StringBuilder sb = new StringBuilder();\n        for (long i = 0; i < length; i++) {\n            sb.append(Terms.toStringCoerce(raw.getMember(Long.toString(i)), cc));\n            if (i + 1 == length) break;\n            // substitutions are positional starting at args[1].\n            int subIdx = (int) (i + 1);\n            if (subIdx < args.length) {","sourceCodeStart":74,"sourceCodeEnd":110,"githubUrl":"https://github.com/karatelabs/karate/blob/a22eb90246d958d15a47bf436693d0121ad2812d/karate-js/src/main/java/io/karatelabs/js/JsStringConstructor.java#L74-L110","documentation":"This TypeError is thrown by String.raw when its first argument is not an object. String.raw is designed for tagged template literals where the first argument is a template object with a 'raw' array property. Passing a non-object (string, number, undefined via requireObjectCoercible pass, etc.) makes the template shape invalid.","triggerScenarios":"Calling String.raw directly with a plain value: String.raw('abc') or String.raw(42). Note undefined/null fail earlier at requireObjectCoercible with a different message; this error fires for coercible-but-non-object values like strings and numbers.","commonSituations":"Calling String.raw as a normal string function instead of a tag; refactorings that removed the template literal; library code forwarding user values into String.raw.","solutions":["Use String.raw as a template tag: String.raw`text` instead of String.raw('text')","Pass an object with a raw property: String.raw({ raw: ['a','b'] })","Guard the argument: if (typeof v !== 'object' || v === null) fall back to String(v)"],"exampleFix":"// before\nconst s = String.raw('a\\nb'); // TypeError\n// after\nconst s = String.raw`a\\nb`;","handlingStrategy":"type-guard","validationCode":"const isTemplateObj = (v) => v !== null && typeof v === 'object' && v.raw !== undefined;","typeGuard":"const canStringRaw = (v) => v !== null && typeof v === 'object' && Array.isArray(v.raw);","tryCatchPattern":"try { return String.raw(templateObj); } catch (e) { if (e instanceof TypeError) return String(templateObj); throw e; }","preventionTips":["Only use String.raw as a template tag (String.raw`...`), never as a plain function","Validate template objects have a raw property before forwarding them","When synthesizing template objects, mimic the runtime shape: { raw: [...], cooked: [...] }"],"tags":["javascript","string","type-error","template-literals"],"backgroundTag":"invalid-argument-value","analyzedSha":"a22eb90246d958d15a47bf436693d0121ad2812d","analyzedAt":"2026-09-12T09:01:00.220Z","contentChangedAt":"2026-09-12T09:01:00.220Z","schemaVersion":2},"datasetVersion":"2026-09-16T19:17:19.609Z"}