{"id":"5df153b1b96b8625","repo":"encode/httpx","slug":"name","errorCode":null,"errorMessage":"{name}","messagePattern":"\\{name\\}","errorType":"exception","errorClass":"KeyError","httpStatus":null,"severity":"error","filePath":"httpx/_models.py","lineNumber":1216,"sourceCode":"        if domain is not None:\n            args.append(domain)\n        if path is not None:\n            assert domain is not None\n            args.append(path)\n        self.jar.clear(*args)\n\n    def update(self, cookies: CookieTypes | None = None) -> None:  # type: ignore\n        cookies = Cookies(cookies)\n        for cookie in cookies.jar:\n            self.jar.set_cookie(cookie)\n\n    def __setitem__(self, name: str, value: str) -> None:\n        return self.set(name, value)\n\n    def __getitem__(self, name: str) -> str:\n        value = self.get(name)\n        if value is None:\n            raise KeyError(name)\n        return value\n\n    def __delitem__(self, name: str) -> None:\n        return self.delete(name)\n\n    def __len__(self) -> int:\n        return len(self.jar)\n\n    def __iter__(self) -> typing.Iterator[str]:\n        return (cookie.name for cookie in self.jar)\n\n    def __bool__(self) -> bool:\n        for _ in self.jar:\n            return True\n        return False\n\n    def __repr__(self) -> str:\n        cookies_repr = \", \".join(","sourceCodeStart":1198,"sourceCodeEnd":1234,"githubUrl":"https://github.com/encode/httpx/blob/b5addb64f0161ff6bfe94c124ef76f6a1fba5254/httpx/_models.py#L1198-L1234","documentation":"Raised as `KeyError(name)` by `Cookies.__getitem__` when no cookie with that name exists in the jar (or all matching values are None). `cookies['x']` is a strict lookup; unlike `cookies.get('x')` it does not return a default, so a missing cookie propagates as a standard Python mapping error.","triggerScenarios":"Indexing `response.cookies['token']` or `client.cookies['token']` when the server never sent a `Set-Cookie: token=...` header, or sent it under a different name/attribute that the jar did not store under that key.","commonSituations":"Assuming a cookie exists before login; typos in the cookie name; cookies scoped to a different path/domain than the request; case-sensitivity mistakes.","solutions":["Use `response.cookies.get('token')` (returns None) or `.get('token', '')` for a default.","Check membership first: `if 'token' in response.cookies:`.","Verify the actual cookie names with `print(list(response.cookies))`.","Confirm the request URL's host/path matches the cookie scope."],"exampleFix":"// before\ntoken = response.cookies['token']  # KeyError if absent\n\n// after\ntoken = response.cookies.get('token')\nif token is None:\n    raise AuthError('missing token cookie')","handlingStrategy":"validation","validationCode":"name = 'token'\nif name not in response.cookies:\n    raise AuthError(f'missing {name} cookie')\nvalue = response.cookies[name]","typeGuard":"import httpx\n\ndef has_cookie(resp: httpx.Response, name: str) -> bool:\n    return name in resp.cookies","tryCatchPattern":"try:\n    value = response.cookies['token']\nexcept KeyError:\n    value = None  # or handle missing-cookie case","preventionTips":["Prefer cookies.get(name) over cookies[name] unless you want strict lookup.","Check 'name' in cookies before indexing.","Verify cookie scope (domain/path) matches the request URL."],"tags":["cookies","key-error","response","client"],"analyzedSha":"b5addb64f0161ff6bfe94c124ef76f6a1fba5254","analyzedAt":"2026-08-04T19:32:56.768Z","schemaVersion":2}