Qix- / better-exceptions

Pretty and useful exceptions in Python, automatically.
MIT License
4.59k stars 203 forks source link

Include Django middleware in better-exceptions #108

Closed sumanthratna closed 3 years ago

sumanthratna commented 3 years ago

Rationale

this would remove a lot of redundant code across Django projects

Proposed API

# better_exceptions/integrations/django.py (this repo)

from sys import exc_info
from better_exceptions import excepthook

class BetterExceptionsMiddleware:
    def __init__(self, get_response):
        self.get_response = get_response

    def __call__(self, request):
        return self.get_response(request)

    def process_exception(self, _, exception):
        _, _, traceback = exc_info()
        excepthook(exception.__class__, exception, traceback)
# settings.py (user's Django project)

# ...
INSTALLED_APPS = [
    # ...
    "better_exceptions",
]
# ...
MIDDLEWARE = [
    # ...
    "better_exceptions.integrations.django.BetterExceptionsMiddleware",
]
# ...

This should be really easy to implement but I want to make sure you're (@Qix-) okay with maintaining this code.

Qix- commented 3 years ago

I haven't touched Django in a long time; how does Django pick this up? Does this interfere with any of the existing code?

If not, then a PR would be welcome :)

sumanthratna commented 3 years ago

I haven't touched Django in a long time; how does Django pick this up? Does this interfere with any of the existing code?

If not, then a PR would be welcome :)

To my knowledge, this should work pretty smoothly. re: existing code, I don't think so—the two code blocks I have in the original post should be all that's needed to get this feature ready to go.

I'll create a PR for this. @Qix- should I also create a unit test? We'll need to add Django as a CI dependency if so

Qix- commented 3 years ago

@sumanthratna normally Id say yes but in this case no, its OK.