{"record":{"id":"42c102eeda750460","repo":"grafana/k6","slug":"no-cookies-provided","errorCode":null,"errorMessage":"no cookies provided","messagePattern":"no cookies provided","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"internal/js/modules/k6/browser/common/browser_context.go","lineNumber":460,"sourceCode":"\t\t\t\tout <- p\n\t\t\t\treturn\n\t\t\t}\n\t\t}\n\t}\n}\n\nfunc (b *BrowserContext) getSession(id target.SessionID) *Session {\n\treturn b.browser.conn.getSession(id)\n}\n\n// AddCookies adds cookies into this browser context.\n// All pages within this context will have these cookies installed.\nfunc (b *BrowserContext) AddCookies(cookies []*Cookie) error {\n\tb.logger.Debugf(\"BrowserContext:AddCookies\", \"bctxid:%v\", b.id)\n\n\t// skip work if no cookies provided.\n\tif len(cookies) == 0 {\n\t\treturn fmt.Errorf(\"no cookies provided\")\n\t}\n\n\tcookiesToSet := make([]*network.CookieParam, 0, len(cookies))\n\tfor _, c := range cookies {\n\t\tif c.Name == \"\" {\n\t\t\treturn fmt.Errorf(\"cookie name must be set: %#v\", c)\n\t\t}\n\t\tif c.Value == \"\" {\n\t\t\treturn fmt.Errorf(\"cookie value must be set: %#v\", c)\n\t\t}\n\t\t// if URL is not set, both Domain and Path must be provided\n\t\tif c.URL == \"\" && (c.Domain == \"\" || c.Path == \"\") {\n\t\t\tconst msg = \"if cookie URL is not provided, both domain and path must be specified: %#v\"\n\t\t\treturn fmt.Errorf(msg, c)\n\t\t}\n\t\t// calculate the cookie expiration date, session cookie if not set.\n\t\tvar ts *cdp.TimeSinceEpoch\n\t\tif c.Expires > 0 {","sourceCodeStart":442,"sourceCodeEnd":478,"githubUrl":"https://github.com/grafana/k6/blob/01ffac6f245854c1b8adc6a69857c76a15f90022/internal/js/modules/k6/browser/common/browser_context.go#L442-L478","documentation":"AddCookies rejects an empty cookie list before making any CDP call: 'skip work if no cookies provided' is enforced as an error, not a no-op. k6 requires at least one cookie object in the array passed to browserContext.addCookies().","triggerScenarios":"browserContext.addCookies([]) or addCookies() with an empty array; a variable expected to hold cookies from an HTTP response or fixture that is actually empty at runtime.","commonSituations":"Seeding cookies from an upstream HTTP request that returned no Set-Cookie headers; conditional code paths where the cookie list was never populated; parsing a JSON cookie fixture that yielded an empty array.","solutions":["Guard the call: only invoke addCookies when the array is non-empty","Fix the upstream source so the cookie list is actually populated before the browser step","Log the cookie list length before calling to catch empty inputs early"],"exampleFix":"// before\nconst cookies = []; // forgot to populate from response\nctx.addCookies(cookies); // throws 'no cookies provided'\n\n// after\nif (cookies.length > 0) {\n  ctx.addCookies(cookies);\n}","handlingStrategy":"validation","validationCode":"if (!Array.isArray(cookies) || cookies.length === 0) {\n  throw new Error('addCookies requires a non-empty cookie array');\n}\nctx.addCookies(cookies);","typeGuard":"const hasCookies = (arr) => Array.isArray(arr) && arr.length > 0;","tryCatchPattern":null,"preventionTips":["Skip the addCookies call when the list is empty","Assert upstream cookie extraction actually produced entries before the browser step","Log cookies.length before calling to catch empty fixtures early"],"tags":["browser","cookies","validation"],"backgroundTag":"empty-argument-rejected","analyzedSha":"01ffac6f245854c1b8adc6a69857c76a15f90022","analyzedAt":"2026-08-18T03:05:52.393Z","contentChangedAt":"2026-08-18T03:05:52.393Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}