{"record":{"id":"483788f463b90e5e","repo":"locustio/locust","slug":"in-order-to-use-a-with-block-for-requests-you-mus-483788","errorCode":null,"errorMessage":"In order to use a with-block for requests, you must also pass catch_response=True","messagePattern":"In order to use a with-block for requests, you must also pass catch_response=True","errorType":"exception","errorClass":"LocustError","httpStatus":null,"severity":"error","filePath":"locust/contrib/fasthttp.py","lineNumber":721,"sourceCode":"    _manual_result = None\n    _entered = False\n\n    def __init__(self, response, request_event, request_meta, catch_response: bool):\n        # copy data from response to this object (use update to avoid sharing\n        # the dict reference, which creates a GC-reference cycle that Python 3.13+\n        # can collect mid-request — see #3388)\n        self.__dict__.update(response.__dict__)\n        try:\n            self._cached_content = response._cached_content\n        except AttributeError:\n            pass\n        self._request_event = request_event\n        self.request_meta = request_meta\n        self._catch_response = catch_response\n\n    def __enter__(self):\n        if not self._catch_response:\n            raise LocustError(\"In order to use a with-block for requests, you must also pass catch_response=True\")\n        self._entered = True\n        return self\n\n    def __exit__(self, exc, value, traceback):\n        # if the user has already manually marked this response as failure or success\n        # we ignore the default behaviour of letting the response code determine the outcome\n        if self._manual_result is not None:\n            if self._manual_result is True:\n                self.request_meta[\"exception\"] = None\n                self._report_request()\n            elif isinstance(self._manual_result, Exception):\n                self.request_meta[\"exception\"] = self._manual_result\n                self._report_request()\n\n            return exc is None\n\n        if exc:\n            if isinstance(value, ResponseError):","sourceCodeStart":703,"sourceCodeEnd":739,"githubUrl":"https://github.com/locustio/locust/blob/f391a716e12c2c712e80b5835e877b7933397453/locust/contrib/fasthttp.py#L703-L739","documentation":"ResponseContextManager.__enter__ raises LocustError if the request was not made with catch_response=True, because only then is the response wrapped in a context manager that allows manual success/failure marking. Using a with-block without that flag would silently do nothing, so the library fails fast.","triggerScenarios":"Writing 'with self.client.get(\"/url\") as response:' in a FastHttpUser task without catch_response=True in the request kwargs.","commonSituations":"Copy-pasting with-block response validation examples from Locust docs while omitting the catch_response=True argument, often after refactoring an existing plain request call.","solutions":["Add catch_response=True to the request call used in the with-block","Drop the with-block and use the plain response object if manual result marking is not needed"],"exampleFix":"// before\nwith self.client.get(\"/api/item\") as response:\n    if response.status_code != 200:\n        response.failure(\"bad status\")\n// after\nwith self.client.get(\"/api/item\", catch_response=True) as response:\n    if response.status_code != 200:\n        response.failure(\"bad status\")","handlingStrategy":"validation","validationCode":"def with_response(session, method, *args, **kwargs):\n    kwargs.setdefault(\"catch_response\", True)\n    return getattr(session, method)(*args, **kwargs)","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Always pass catch_response=True when using a with-block for requests","Create a small helper/wrapper that injects catch_response=True automatically","Review locustfiles for 'with self.client.' lines missing the flag"],"tags":["python","fasthttp","locust","catch-response"],"backgroundTag":"catch-response-not-enabled","analyzedSha":"f391a716e12c2c712e80b5835e877b7933397453","analyzedAt":"2026-08-29T00:36:13.872Z","schemaVersion":2},"datasetVersion":"2026-08-29T02:17:18.158Z"}