{"record":{"id":"b2dc1b7d443e5028","repo":"grafana/k6","slug":"cookie-name-must-be-set-v","errorCode":null,"errorMessage":"cookie name must be set: %#v","messagePattern":"cookie name must be set: %#v","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"internal/js/modules/k6/browser/common/browser_context.go","lineNumber":466,"sourceCode":"\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 {\n\t\t\tt := cdp.TimeSinceEpoch(time.Unix(c.Expires, 0))\n\t\t\tts = &t\n\t\t}\n\t\tcookiesToSet = append(cookiesToSet, &network.CookieParam{\n\t\t\tName:     c.Name,\n\t\t\tValue:    c.Value,","sourceCodeStart":448,"sourceCodeEnd":484,"githubUrl":"https://github.com/grafana/k6/blob/01ffac6f245854c1b8adc6a69857c76a15f90022/internal/js/modules/k6/browser/common/browser_context.go#L448-L484","documentation":"Client-side validation in AddCookies: every cookie must have a non-empty Name before the Storage.setCookies CDP call. The offending cookie is printed with %#v so you can see all its fields. No network traffic happens — the batch is rejected locally.","triggerScenarios":"browserContext.addCookies([{ value: 'v', url: 'https://example.com' }]) — name omitted; name built from a variable that is undefined/empty (e.g. a typo'd property like nme); name is an empty string from parsed data.","commonSituations":"Building cookies dynamically from HTTP responses or fixtures where the name field is missing or mis-keyed; destructuring mistakes when mapping Set-Cookie strings; cookies constructed from config with optional fields.","solutions":["Ensure every cookie object has a non-empty name string","Validate the array before the call: every item needs name, value, and url or (domain and path)","Log the cookie objects before calling to spot mis-keyed or empty fields"],"exampleFix":"// before\nctx.addCookies([{ nme: 'session', value: 'abc', url: 'https://example.com' }]); // typo: nme\n\n// after\nctx.addCookies([{ name: 'session', value: 'abc', url: 'https://example.com' }]);","handlingStrategy":"validation","validationCode":"function validateCookies(cookies) {\n  for (const c of cookies) {\n    if (typeof c.name !== 'string' || c.name === '') {\n      throw new Error(`cookie missing name: ${JSON.stringify(c)}`);\n    }\n  }\n}\nvalidateCookies([{ name: 'session', value: 'abc', url: 'https://example.com' }]);\nctx.addCookies([{ name: 'session', value: 'abc', url: 'https://example.com' }]);","typeGuard":"const isNamedCookie = (c) =>\n  c != null && typeof c === 'object' && typeof c.name === 'string' && c.name.length > 0;","tryCatchPattern":null,"preventionTips":["Check every cookie has a non-empty name before the call","Build cookies from typed helpers so a missing name fails at construction time","Log cookie objects before calling to catch mis-keyed fields (nme vs name)"],"tags":["browser","cookies","validation"],"backgroundTag":"cookie-validation-failed","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"}