{"record":{"id":"c104959419c82ea7","repo":"pallets/flask","slug":"exc-class-or-code-is-not-a-recognized-http-err","errorCode":null,"errorMessage":"'{exc_class_or_code}' is not a recognized HTTP error code. Use a subclass of HTTPException with that code instead.","messagePattern":"'(.+?)' is not a recognized HTTP error code\\. Use a subclass of HTTPException with that code instead\\.","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"src/flask/sansio/scaffold.py","lineNumber":673,"sourceCode":"\n    @staticmethod\n    def _get_exc_class_and_code(\n        exc_class_or_code: type[Exception] | int,\n    ) -> tuple[type[Exception], int | None]:\n        \"\"\"Get the exception class being handled. For HTTP status codes\n        or ``HTTPException`` subclasses, return both the exception and\n        status code.\n\n        :param exc_class_or_code: Any exception class, or an HTTP status\n            code as an integer.\n        \"\"\"\n        exc_class: type[Exception]\n\n        if isinstance(exc_class_or_code, int):\n            try:\n                exc_class = default_exceptions[exc_class_or_code]\n            except KeyError:\n                raise ValueError(\n                    f\"'{exc_class_or_code}' is not a recognized HTTP\"\n                    \" error code. Use a subclass of HTTPException with\"\n                    \" that code instead.\"\n                ) from None\n        else:\n            exc_class = exc_class_or_code\n\n        if isinstance(exc_class, Exception):\n            raise TypeError(\n                f\"{exc_class!r} is an instance, not a class. Handlers\"\n                \" can only be registered for Exception classes or HTTP\"\n                \" error codes.\"\n            )\n\n        if not issubclass(exc_class, Exception):\n            raise ValueError(\n                f\"'{exc_class.__name__}' is not a subclass of Exception.\"\n                \" Handlers can only be registered for Exception classes\"","sourceCodeStart":655,"sourceCodeEnd":691,"githubUrl":"https://github.com/pallets/flask/blob/3596b1ab61cea85edb8970e83ff61daa073facf8/src/flask/sansio/scaffold.py#L655-L691","documentation":"_register_error_handler looks up integer codes in Werkzeug's default_exceptions map (which only contains the standard HTTP error codes 400-418, 421-426, 428-431, 444, 449, 451, 455-456, 500-508, 510-511). An integer outside that set raises ValueError suggesting a subclass of HTTPException instead.","triggerScenarios":"Calling @app.errorhandler(999); @app.errorhandler(200); @app.errorhandler(419) or any non-standard code; passing a custom status code integer that has no default exception class.","commonSituations":"Using a custom non-RFC status code; typos in the code; app-specific codes like 460/520 that Werkzeug does not ship.","solutions":["Define a custom HTTPException subclass with .code set to your code, and register that class instead of the integer.","Use a standard HTTP error code that Werkzeug knows.","Register the handler against HTTPException generally if you want to catch all HTTP errors."],"exampleFix":"// before\n@app.errorhandler(520)\ndef handle_520(e): return 'oops', 520\n\n// after\nfrom werkzeug.exceptions import HTTPException\nclass Err520(HTTPException):\n    code = 520\n\n@app.errorhandler(Err520)\ndef handle_520(e): return 'oops', 520","handlingStrategy":"validation","validationCode":"from werkzeug.exceptions import default_exceptions\n\ndef validate_error_code(code: int) -> int:\n    if code not in default_exceptions:\n        raise ValueError(f'{code} is not a known HTTP error code; subclass HTTPException')\n    return code\n","typeGuard":"def is_known_http_error_code(code) -> bool:\n    return isinstance(code, int) and code in default_exceptions\n","tryCatchPattern":null,"preventionTips":["Register handlers against known codes or HTTPException subclasses, not arbitrary ints.","Create a custom HTTPException subclass for app-specific codes.","Keep a constant list of supported codes for config-driven handlers."],"tags":["flask","error-handler","http","validation"],"backgroundTag":null,"analyzedSha":"3596b1ab61cea85edb8970e83ff61daa073facf8","analyzedAt":"2026-08-11T20:11:52.622Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}