isik-kaplan/django-http-exceptions

Exception classes can't be extended

Roukanken42 opened this issue · 1 comments

Trying to extend HTTPExceptions for custom functionality fails with KeyError

Expectations - following code (or similar) should print YAY:

class CustomHTTPException(HTTPException):
    def my_custom_method(self):
        return "YAY"


class CustomHTTPExceptions(HTTPExceptions):
    BASE_EXCEPTION = CustomHTTPException


print(CustomHTTPExceptions.BAD_REQUEST.my_custom_method())

Instead it fails with

  File "test.py", line 6, in <module>
    class CustomHTTPExceptions(HTTPExceptions):
  File "site-packages\django_http_exceptions\exceptions.py", line 33, in __new__
    _transform = classdict['__transform__']
KeyError: '__transform__'

Due to crazy nature of metaclasses and mro, I tried a different approach and with 16e92f9 introduced a new method HTTPExceptions.register_base_exception which accepts a class that inherits from HttpException.

Hope it works for you!