{"record":{"id":"3df01d92cc957649","repo":"locustio/locust","slug":"locustbadstatuscode","errorCode":null,"errorMessage":"LocustBadStatusCode","messagePattern":"LocustBadStatusCode","errorType":"error_code","errorClass":"LocustBadStatusCode","httpStatus":null,"severity":"error","filePath":"locust/contrib/fasthttp.py","lineNumber":690,"sourceCode":"\n    def __init__(self, client_pool: HTTPClientPool | None = None, **kwargs):\n        super().__init__(**kwargs)\n\n        if client_pool is not None:\n            self.clientpool = client_pool\n\n    def _urlopen(self, request):\n        \"\"\"Override _urlopen() in order to make it use the response_type attribute\"\"\"\n        client = self.clientpool.get_client(request.url_split)\n        resp = client.request(\n            request.method, request.url_split.request_uri, body=request.payload, headers=request.headers\n        )\n        return self.response_type(resp, request=request, sent_request=resp._sent_request)\n\n    def _verify_status(self, status_code, url=None):\n        \"\"\"Hook for subclassing\"\"\"\n        if status_code not in self.valid_response_codes:\n            raise LocustBadStatusCode(url, code=status_code)\n\n\nclass ResponseContextManager(FastResponse):\n    \"\"\"\n    A Response class that also acts as a context manager that provides the ability to manually\n    control if an HTTP request should be marked as successful or a failure in Locust's statistics\n\n    This class is a subclass of :py:class:`FastResponse <locust.contrib.fasthttp.FastResponse>`\n    with two additional methods: :py:meth:`success <locust.contrib.fasthttp.ResponseContextManager.success>`\n    and :py:meth:`failure <locust.contrib.fasthttp.ResponseContextManager.failure>`.\n    \"\"\"\n\n    _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+","sourceCodeStart":672,"sourceCodeEnd":708,"githubUrl":"https://github.com/locustio/locust/blob/f391a716e12c2c712e80b5835e877b7933397453/locust/contrib/fasthttp.py#L672-L708","documentation":"_verify_status is a hook in FastHttpSession that raises LocustBadStatusCode when the HTTP status code of a response is not in the session's valid_response_codes (by default 2xx). It signals the request will be marked as a failure in Locust statistics.","triggerScenarios":"self.client.get()/request() returning a status code outside valid_response_codes; or a custom FastHttpSession subclass whose _verify_status override raises it deliberately.","commonSituations":"Hitting endpoints that return 3xx redirects not followed, 404s on stale test data, 5xx server errors under load, or misconfigured base_url leading to unexpected responses.","solutions":["Check the URL and request setup to ensure the expected success status is returned","Adjust valid_response_codes on the FastHttpSession if the status is legitimately acceptable","Handle the response manually with catch_response=True and response.success() to override the default code-based verdict"],"exampleFix":"// before\nclass MySession(FastHttpSession):\n    pass\n// after\nclass MySession(FastHttpSession):\n    valid_response_codes = [200, 201, 404, 429]","handlingStrategy":"try-catch","validationCode":"# check status before relying on default verdict\nresp = self.client.get(\"/api/item\")\nif resp.status_code not in self.client.valid_response_codes:\n    # handle or override with catch_response=True flow","typeGuard":null,"tryCatchPattern":"from locust.exception import LocustBadStatusCode\ntry:\n    resp = self.client.get(\"/api/item\")\nexcept LocustBadStatusCode as e:\n    self.environment.events.request.fire(...)  # or log and continue\n","preventionTips":["Extend valid_response_codes when endpoints legitimately return non-2xx codes","Use catch_response=True to take manual control of verdicts for expected error codes","Verify base_url and test data so requests hit valid endpoints"],"tags":["python","fasthttp","http","status-code"],"backgroundTag":"unexpected-http-status","analyzedSha":"f391a716e12c2c712e80b5835e877b7933397453","analyzedAt":"2026-08-29T00:36:13.872Z","schemaVersion":2},"datasetVersion":"2026-08-29T02:17:18.158Z"}