{"record":{"id":"7fc083834a6d1ae8","repo":"karatelabs/karate","slug":"js-rejection-reason","errorCode":null,"errorMessage":"<JS rejection reason>","messagePattern":"<JS rejection reason>","errorType":"exception","errorClass":"io.karatelabs.js.Abrupt","httpStatus":null,"severity":"error","filePath":"karate-js/src/main/java/io/karatelabs/js/JsPromiseConstructor.java","lineNumber":165,"sourceCode":"     * itself completes normally with a rejected promise.\n     */\n    private static final class Abrupt extends RuntimeException {\n\n        final transient Object reason;\n\n        Abrupt(Object reason) {\n            super(null, null, false, false);\n            this.reason = reason;\n        }\n    }\n\n    /** Converts a pending cooperative throw into an {@link Abrupt}, clearing the\n     *  flag — from here on the error travels as the rejection reason. */\n    private static void checkAbrupt(Context context) {\n        if (context instanceof CoreContext cc && cc.isError()) {\n            Object reason = cc.getErrorThrown();\n            cc.reset();\n            throw new Abrupt(reason);\n        }\n    }\n\n    /** IteratorClose with the surrounding completion already abrupt: whatever\n     *  {@code return()} does is discarded, the original reason wins. */\n    private static void closeQuietly(JsIterator iter, Context context) {\n        try {\n            iter.close(context, true);\n        } catch (RuntimeException e) {\n            if (e instanceof FlowControlSignal || AsyncSupport.isHostCancellation(e)) {\n                throw e;\n            }\n            // IteratorClose failing is swallowed per spec 7.4.6 step 4\n        }\n        if (context instanceof CoreContext cc && cc.isStopped()) {\n            cc.reset(); // close() re-parks the completion it saw — we own the reason now\n        }\n    }","sourceCodeStart":147,"sourceCodeEnd":183,"githubUrl":"https://github.com/karatelabs/karate/blob/a22eb90246d958d15a47bf436693d0121ad2812d/karate-js/src/main/java/io/karatelabs/js/JsPromiseConstructor.java#L147-L183","documentation":"checkAbrupt converts a pending cooperative throw recorded on the Context into an Abrupt carrying the JS rejection reason, clearing the pending-error flag. Callers (asPromise, collect — e.g. Promise.all/allSettled/race/any iteration) use it so an in-flight script error surfaces as the promise's rejection reason instead of being silently dropped.","triggerScenarios":"During Promise combinator iteration (asPromise/collect), the context has isError() set — a prior JS throw is pending — so the pending error value is taken as the rejection reason and thrown as Abrupt.","commonSituations":"A callback passed to Promise.all's iterator throws mid-iteration; an earlier statement threw and the error is only observed at the next combinator boundary; user code mixing sync throws with async combinators.","solutions":["Find and fix the original JS throw — the Abrupt's payload is that reason; read its message/stack.","Don't swallow errors inside iterator callbacks; let them reject the promise explicitly.","Avoid continuing script execution after an uncaught sync error; check/reset context error state in host code.","Wrap risky sections in try/catch in JS so errors become deliberate rejections."],"exampleFix":"// before\narr.map(() => { throw new Error('boom'); }); // pending error surfaces later as Abrupt\n// after\nPromise.all(arr.map(() => Promise.reject(new Error('boom')))).catch(handle); // explicit rejection","handlingStrategy":"try-catch","validationCode":"// in host code before running combinators: ensure no pending error\nif (context instanceof CoreContext cc && cc.isError()) cc.reset(); // or handle it first","typeGuard":null,"tryCatchPattern":"try { return Promise.all(...); } catch (Abrupt a) { handleRejection(a.value); /* a.value is the original JS rejection reason */ }","preventionTips":["Fix the originating JS throw — the Abrupt payload is that reason's value.","Make iterator callbacks reject promises explicitly instead of throwing sync errors mid-enumeration.","In host embedding code, check and clear context error state between script evaluations.","Wrap user callbacks in try/catch and convert to Promise.reject for predictable handling."],"tags":["promise","rejection","javascript"],"backgroundTag":"unhandled-promise-rejection","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"}