{"record":{"id":"ba3c28971cd68e6d","repo":"karatelabs/karate","slug":"result-message-optionally-prefixed-with-source-from-context","errorCode":null,"errorMessage":"${result.message optionally prefixed with source from context}","messagePattern":"\\$\\{result\\.message optionally prefixed with source from context\\}","errorType":"exception","errorClass":"AssertionError","httpStatus":null,"severity":"error","filePath":"karate-core/src/main/java/io/karatelabs/match/Expect.java","lineNumber":154,"sourceCode":"    private void handleResult(Context context, Result result) {\n        if (negated) {\n            result = result.pass ? Result.fail(\"expected condition to be false\") : Result.PASS;\n        }\n        // Use context supplier if context is null and supplier is available\n        Context effectiveContext = context;\n        if (effectiveContext == null && contextSupplier != null) {\n            effectiveContext = contextSupplier.get();\n        }\n        if (onResult != null) {\n            onResult.accept(effectiveContext, result);\n        }\n        if (!result.pass && throwOnFailure) {\n            String message = result.message;\n            String source = getSourceFromContext(effectiveContext);\n            if (source != null) {\n                message = source + \"\\n\" + message;\n            }\n            throw new AssertionError(message);\n        }\n    }\n\n    private static Result evaluate(Object actual, Match.Type type, Object expected) {\n        try (Value value = Match.evaluate(actual, null, null)) {\n            return value.is(type, expected);\n        }\n    }\n\n    private JavaCallable match(Match.Type type) {\n        return (context, args) -> {\n            Result result = evaluate(subject, type, args[0]);\n            handleResult(context, result);\n            return null;\n        };\n    }\n\n    private JavaCallable matchChainable(Match.Type type) {","sourceCodeStart":136,"sourceCodeEnd":172,"githubUrl":"https://github.com/karatelabs/karate/blob/a22eb90246d958d15a47bf436693d0121ad2812d/karate-core/src/main/java/io/karatelabs/match/Expect.java#L136-L172","documentation":"This is Karate's expect() assertion failure. Expect.handleResult evaluates the match result and, when it did not pass and throwOnFailure is set, throws an AssertionError whose message is the assertion's own message optionally prefixed by the current source (e.g. script location) from the scenario context.","triggerScenarios":"Any expect(...)... assertion that evaluates to a failed Result while throwOnFailure is true, e.g. expect(foo).to.equal('bar') when foo != 'bar', expect(list).to.contain(x) when the element is missing, or expect(obj).to.have.keys(...) mismatch.","commonSituations":"Failing API assertions in karate feature scripts; response data changed upstream; typos in expected keys or values; type differences between actual and expected (string vs number).","solutions":["Read the assertion message (and the source prefix) to identify which expect() statement failed and compare actual vs expected values","Fix the expectation or investigate why the actual value differs (call the endpoint/inspect data)","If the assertion is intentionally exploratory, wrap it so failure is non-fatal or use a soft-assert onResult instead of throwing"],"exampleFix":"// before\nexpect(response.name).to.equal('jane') // AssertionError: actual 'John', expected: 'jane'\n// after\nexpect(response.name.toLowerCase()).to.equal('jane')","handlingStrategy":"try-catch","validationCode":"// Karate JS\nif (response.status == 200 && response.body.items != null) {\n  expect(response.body.items.length).to.be.at.least(1);\n}","typeGuard":"function isObject(v) { return v != null && typeof v === 'object'; }","tryCatchPattern":"// JS in Karate\ntry {\n  expect(response.name).to.equal('jane');\n} catch (e) {\n  karate.log('assertion failed: ' + e.message);\n  // decide whether to fail the scenario or record softly\n}","preventionTips":["Validate response shape before asserting on fields","Use match with #present/#notnull markers for defensive assertions","Keep expected values in fixtures/variables instead of inline literals to ease drift","Check the source prefix in the message to locate the failing line quickly"],"tags":["assertion","test-failure","expect"],"backgroundTag":"schema-validation-failed","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"}