{"record":{"id":"77af1ea60403b000","repo":"pallets/flask","slug":"exc-class-r-is-an-instance-not-a-class-handler","errorCode":null,"errorMessage":"{exc_class!r} is an instance, not a class. Handlers can only be registered for Exception classes or HTTP error codes.","messagePattern":"(.+?) is an instance, not a class\\. Handlers can only be registered for Exception classes or HTTP error codes\\.","errorType":"exception","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"src/flask/sansio/scaffold.py","lineNumber":682,"sourceCode":"        :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\"\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","sourceCodeStart":664,"sourceCodeEnd":700,"githubUrl":"https://github.com/pallets/flask/blob/3596b1ab61cea85edb8970e83ff61daa073facf8/src/flask/sansio/scaffold.py#L664-L700","documentation":"_register_error_handler expects a class (or an int code). If the value passes the int check but isinstance(value, Exception) is True, it means the user passed an instance rather than a class, so Flask raises TypeError. Handlers must be registered against exception classes, not instances of them.","triggerScenarios":"@app.errorhandler(ValueError('x')); app.register_error_handler(SomeException()); passing an instantiated exception object instead of the class.","commonSituations":"Mistakenly instantiating the exception; refactoring that replaced a class reference with a constructed instance; dynamic code that builds exception objects for registration.","solutions":["Pass the class itself, not an instance: errorhandler(ValueError) not errorhandler(ValueError()).","If the value comes from a variable, ensure it is a type: use `exc if isinstance(exc, type) else type(exc)`.","Double-check that you are not accidentally calling the constructor."],"exampleFix":"// before\n@app.errorhandler(MyException('msg'))\ndef handle(e): ...\n\n// after\n@app.errorhandler(MyException)\ndef handle(e): ...","handlingStrategy":"type-guard","validationCode":"def to_error_class(exc):\n    if isinstance(exc, type) and issubclass(exc, Exception):\n        return exc\n    if isinstance(exc, Exception):\n        return type(exc)\n    raise TypeError('pass an Exception class, not an instance')\n","typeGuard":"def is_exception_class(obj) -> bool:\n    return isinstance(obj, type) and issubclass(obj, Exception)\n","tryCatchPattern":null,"preventionTips":["Pass the class, never an instance, to errorhandler.","Normalize dynamic handler targets through a to_error_class helper.","Code-review registrations that build exception objects."],"tags":["flask","error-handler","type-error","validation"],"backgroundTag":null,"analyzedSha":"3596b1ab61cea85edb8970e83ff61daa073facf8","analyzedAt":"2026-08-11T20:11:52.622Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}