{"record":{"id":"6f3bc253149d40c9","repo":"karatelabs/karate","slug":"structuredclone-requires-at-least-1-argument","errorCode":null,"errorMessage":"structuredClone requires at least 1 argument","messagePattern":"structuredClone requires at least 1 argument","errorType":"exception","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"karate-js/src/main/java/io/karatelabs/js/JsStructuredClone.java","lineNumber":55,"sourceCode":" * {@code Set}, {@code RegExp}, {@code Error} and boxed primitives. Objects are\n * memoized by identity, so a cyclic graph clones to an equally cyclic one\n * rather than being rejected.\n * <p>\n * Functions and symbols raise a {@code DataCloneError}. Anything else the\n * engine cannot deep-copy — host Java values reaching JS through the bridge —\n * is passed through by reference rather than rejected; the alternative would\n * make {@code structuredClone} unusable on any object graph that touches Java.\n */\nfinal class JsStructuredClone {\n\n    private static final byte DATA_ATTRS = JsObject.WRITABLE | JsObject.CONFIGURABLE;\n\n    private JsStructuredClone() {\n    }\n\n    static Object call(Context context, Object[] args) {\n        if (args.length == 0) {\n            throw JsErrorException.typeError(\"structuredClone requires at least 1 argument\");\n        }\n        // args[1] is the options bag carrying `transfer` — nothing in this\n        // engine is transferable, so it is accepted and ignored\n        return clone(args[0], new IdentityHashMap<>(), context instanceof CoreContext cc ? cc : null);\n    }\n\n    private static Object clone(Object value, IdentityHashMap<Object, Object> memo, CoreContext ctx) {\n        String type = Terms.typeOf(value);\n        if (\"function\".equals(type) || \"symbol\".equals(type)) {\n            throw dataCloneError(type);\n        }\n        Object seen = memo.get(value);\n        if (seen != null) {\n            return seen;\n        }\n        if (value instanceof JsPrimitive jp) {\n            return remember(memo, value, boxed(jp));\n        }","sourceCodeStart":37,"sourceCodeEnd":73,"githubUrl":"https://github.com/karatelabs/karate/blob/a22eb90246d958d15a47bf436693d0121ad2812d/karate-js/src/main/java/io/karatelabs/js/JsStructuredClone.java#L37-L73","documentation":"This TypeError is thrown by the structuredClone built-in when invoked with no arguments. structuredClone performs a deep clone of its first argument, so a value to clone is mandatory; the spec has no zero-argument form. Note args[1] (the options bag with transfer) is accepted but ignored in this engine since nothing is transferable.","triggerScenarios":"Calling structuredClone() with an empty argument list, e.g. via a dynamic dispatch Function.apply with an empty array, or a helper that conditionally omits the value.","commonSituations":"Spread/apply patterns where the source array is empty; wrappers forwarding arguments without checking arity; test code probing the API surface.","solutions":["Always pass the value: structuredClone(value)","Guard arity before calling: if (args.length > 0) structuredClone(args[0])","Handle the TypeError in try/catch when arity is dynamic"],"exampleFix":"// before\nstructuredClone(...args); // TypeError when args is empty\n// after\nif (args.length > 0) structuredClone(args[0]);","handlingStrategy":"validation","validationCode":"const safeStructuredClone = (args) => args.length > 0 ? structuredClone(args[0]) : undefined;","typeGuard":"const canClone = (args) => Array.isArray(args) && args.length > 0;","tryCatchPattern":"try { return structuredClone(v); } catch (e) { if (e instanceof TypeError && v === undefined) return v; throw e; }","preventionTips":["Check argument arrays are non-empty before apply/spread into structuredClone","Wrap dynamic-dispatch calls with an arity check","Remember transfer options are unsupported here — do not rely on transfer semantics"],"tags":["javascript","structuredclone","type-error","arguments"],"backgroundTag":"missing-required-argument","analyzedSha":"a22eb90246d958d15a47bf436693d0121ad2812d","analyzedAt":"2026-09-12T09:01:00.220Z","contentChangedAt":"2026-09-12T09:01:00.220Z","schemaVersion":2},"datasetVersion":"2026-09-19T12:17:13.211Z"}