{"record":{"id":"c907ba7c31bf9136","repo":"pallets/flask","slug":"exc-class-name-is-not-a-subclass-of-except","errorCode":null,"errorMessage":"'{exc_class.__name__}' is not a subclass of Exception. Handlers can only be registered for Exception classes or HTTP error codes.","messagePattern":"'(.+?)' is not a subclass of Exception\\. Handlers can only be registered for Exception classes or HTTP error codes\\.","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"src/flask/sansio/scaffold.py","lineNumber":689,"sourceCode":"                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\"\n                \" or HTTP error codes.\"\n            )\n\n        if issubclass(exc_class, HTTPException):\n            return exc_class, exc_class.code\n        else:\n            return exc_class, None\n\n\ndef _endpoint_from_view_func(view_func: ft.RouteCallable) -> str:\n    \"\"\"Internal helper that returns the default endpoint for a given\n    function.  This always is the function name.\n    \"\"\"\n    assert view_func is not None, \"expected view func if endpoint is not provided.\"\n    return view_func.__name__\n","sourceCodeStart":671,"sourceCodeEnd":707,"githubUrl":"https://github.com/pallets/flask/blob/3596b1ab61cea85edb8970e83ff61daa073facf8/src/flask/sansio/scaffold.py#L671-L707","documentation":"After confirming the value is a class (not an instance), Flask checks issubclass(exc_class, Exception). A class that is not an Exception subclass (e.g. int, str, a plain object subclass) cannot be used as an error-handler key, so it raises ValueError. Handlers can only target Exception subclasses or HTTP codes.","triggerScenarios":"@app.errorhandler(int); @app.errorhandler(str); @app.errorhandler(SomeDataclass) where the class does not inherit Exception.","commonSituations":"Registering handlers against status enums, plain classes, or built-in types by mistake; confusing an error code class with an Exception subclass.","solutions":["Make the target class inherit from Exception (or a more specific exception type) before registering.","Pass an integer HTTP code or a real Exception subclass instead of an unrelated class.","If you intended to handle a non-exception signal, restructure to raise an exception and handle that."],"exampleFix":"// before\nclass NotFound: ...\napp.register_error_handler(NotFound, handler)\n\n// after\nclass NotFound(Exception): ...\napp.register_error_handler(NotFound, handler)","handlingStrategy":"type-guard","validationCode":"def validate_error_target(target):\n    if isinstance(target, int):\n        return target\n    if isinstance(target, type) and issubclass(target, Exception):\n        return target\n    raise ValueError(f'{target!r} is not an Exception subclass or HTTP code')\n","typeGuard":"def is_valid_error_target(target) -> bool:\n    return (isinstance(target, int)) or (isinstance(target, type) and issubclass(target, Exception))\n","tryCatchPattern":null,"preventionTips":["Make target classes inherit Exception.","Do not register handlers against enums, dataclasses, or built-in types.","Use a registration helper that validates the target."],"tags":["flask","error-handler","validation","exception"],"backgroundTag":null,"analyzedSha":"3596b1ab61cea85edb8970e83ff61daa073facf8","analyzedAt":"2026-08-11T20:11:52.622Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}