{"record":{"id":"482d0114c601ca01","repo":"grafana/k6","slug":"msg","errorCode":null,"errorMessage":"{msg}","messagePattern":"\\{msg\\}","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"internal/js/modules/k6/k6.go","lineNumber":68,"sourceCode":"\treturn &K6{vu: vu}\n}\n\n// Exports returns the exports of the k6 module.\nfunc (mi *K6) Exports() modules.Exports {\n\treturn modules.Exports{\n\t\tNamed: map[string]any{\n\t\t\t\"check\":      mi.Check,\n\t\t\t\"fail\":       mi.Fail,\n\t\t\t\"group\":      mi.Group,\n\t\t\t\"randomSeed\": mi.RandomSeed,\n\t\t\t\"sleep\":      mi.Sleep,\n\t\t},\n\t}\n}\n\n// Fail is a fancy way of saying `throw \"something\"`.\nfunc (*K6) Fail(msg string) (sobek.Value, error) {\n\treturn sobek.Undefined(), errors.New(msg)\n}\n\n// Sleep waits the provided seconds before continuing the execution.\nfunc (mi *K6) Sleep(secs float64) {\n\tctx := mi.vu.Context()\n\ttimer := time.NewTimer(time.Duration(secs * float64(time.Second)))\n\tselect {\n\tcase <-timer.C:\n\tcase <-ctx.Done():\n\t\ttimer.Stop()\n\t}\n}\n\n// RandomSeed sets the seed to the random generator used for this VU.\nfunc (mi *K6) RandomSeed(seed int64) {\n\trandSource := rand.New(rand.NewSource(seed)).Float64 //nolint:gosec\n\tmi.vu.Runtime().SetRandSource(randSource)\n}","sourceCodeStart":50,"sourceCodeEnd":86,"githubUrl":"https://github.com/grafana/k6/blob/93accf6570dcd306ca5e99cc44c393ee3797761b/internal/js/modules/k6/k6.go#L50-L86","documentation":"`k6.fail(msg)` (internal/js/modules/k6/k6.go:66) is a deliberate abort primitive: \"a fancy way of saying throw 'something'\". It returns `errors.New(msg)`, so sobek throws an exception whose message is exactly the string you passed. The iteration is marked as failed and, by default, the VU stops the iteration (configurable via throw/usage options).","triggerScenarios":"Calling `k6.fail('threshold breached')` anywhere in VU code — directly, inside helpers, or inside check/group callbacks. It is frequently used with custom threshold logic or to abort a scenario conditionally.","commonSituations":"Intentional abort when an application precondition fails; guard clauses that misuse k6.fail for control flow; copied scripts where the message is dynamic and unexpectedly triggers.","solutions":["If the failure is intentional, verify the condition that triggers it and fix the test/target","Wrap the call site in try/catch when the abort should be recoverable or only mark a check","Replace k6.fail with check() when you want to record a failure without aborting the iteration"],"exampleFix":"// before\nif (res.status !== 200) { k6.fail(`unexpected status ${res.status}`); }\n\n// after (record without aborting)\ncheck(res, { 'status is 200': (r) => r.status === 200 });\n\n// after (recoverable abort)\ntry { if (res.status !== 200) k6.fail(`status ${res.status}`); }\ncatch (e) { console.warn('iteration aborted:', e.message); }","handlingStrategy":"try-catch","validationCode":null,"typeGuard":null,"tryCatchPattern":"try {\n  if (shouldAbort()) k6.fail(abortReason);\n} catch (e) {\n  console.warn(`iteration aborted: ${e.message}`);\n  // optionally record a custom metric instead of failing the iteration\n}","preventionTips":["Prefer check() when you want to record failure without aborting the iteration","Keep k6.fail messages static and greppable so unexpected aborts are easy to trace","Treat any k6.fail as a deliberate contract: search the repo for the message before assuming a k6 bug"],"tags":["k6-api","control-flow","abort","javascript"],"backgroundTag":null,"analyzedSha":"93accf6570dcd306ca5e99cc44c393ee3797761b","analyzedAt":"2026-08-15T21:23:27.118Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}