{"record":{"id":"ab0294e6a8f57835","repo":"karatelabs/karate","slug":"cache-requires-key-fn","errorCode":null,"errorMessage":"cache() requires (key, fn)","messagePattern":"cache\\(\\) requires \\(key, fn\\)","errorType":"exception","errorClass":"RuntimeException","httpStatus":null,"severity":"error","filePath":"karate-core/src/main/java/io/karatelabs/http/HttpRequest.java","lineNumber":755,"sourceCode":"            return proceed(targetUrl);\n        };\n    }\n\n    /**\n     * Per-request memoization. {@code request.cache('key', () => compute())}\n     * invokes the function on the first call with that key, stores the result\n     * on this request, and returns the same value on subsequent calls. The\n     * store dies with the request — never crosses request boundaries.\n     * <p>\n     * The two-argument form is the only supported shape. If the key is\n     * missing, the second argument must be a function (otherwise a runtime\n     * error is raised). A cached {@code null} or {@code undefined} return\n     * value is still a cache hit and does not re-invoke the function.\n     */\n    private JavaCallable cache() {\n        return (context, args) -> {\n            if (args.length < 2) {\n                throw new RuntimeException(\"cache() requires (key, fn)\");\n            }\n            String key = args[0] + \"\";\n            if (cacheStore != null && cacheStore.containsKey(key)) {\n                return cacheStore.get(key);\n            }\n            if (!(args[1] instanceof JavaCallable fn)) {\n                throw new RuntimeException(\"cache() second argument must be a function: \" + key);\n            }\n            Object value = fn.call(context);\n            if (cacheStore == null) {\n                cacheStore = new HashMap<>();\n            }\n            cacheStore.put(key, value);\n            return value;\n        };\n    }\n\n    @Override","sourceCodeStart":737,"sourceCodeEnd":773,"githubUrl":"https://github.com/karatelabs/karate/blob/a22eb90246d958d15a47bf436693d0121ad2812d/karate-core/src/main/java/io/karatelabs/http/HttpRequest.java#L737-L773","documentation":"HttpRequest.cache() is a memoization helper exposed to scripts: it takes a cache key and a function, returning the cached value on a hit or invoking the function and caching the result. Called with fewer than two arguments it throws this RuntimeException because it cannot perform either the key lookup or the lazy evaluation.","triggerScenarios":"Calling cache() with 0 or 1 argument, e.g. request.cache('mykey') without the value-producing function, or cache() entirely bare — usually a dropped argument in a chained script expression or a partial refactor.","commonSituations":"Caching expensive setup calls (tokens, fixtures) in HTTP scripts where the closure argument was accidentally deleted; users confusing this cache() with Karate's karate.cache-style helpers with different signatures.","solutions":["Supply both arguments: request.cache('key', () => expensiveCall())","If you only meant to read a cached value, guard the read yourself or use the cache store API directly","Ensure the second argument is actually a function (a plain value will trigger the companion 'second argument must be a function' error)","Check for script minification/refactor tooling that dropped trailing lambda arguments"],"exampleFix":"// before\nrequest.cache(\"token\"); // throws\n// after\nrequest.cache(\"token\", () => karate.callSingle(\"classpath:auth.feature\"));","handlingStrategy":"validation","validationCode":"if (arguments.length < 2) throw new Error('cache() requires (key, fn)');\nrequest.cache(key, fn);","typeGuard":"function cacheArgsOk(key, fn) { return key != null && typeof fn === 'function'; }","tryCatchPattern":null,"preventionTips":["Always call cache() with a string key and a zero-arg function","Never pass a pre-computed value directly; wrap it in a function","Review chained script expressions after refactors to ensure the lambda wasn't dropped"],"tags":["http","cache","missing-argument","dsl"],"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-16T19:17:19.609Z"}