Django Channels - Catch unhandled exceptions in HTTP consumer

17 views
Skip to first unread message

notorious no

unread,
Feb 5, 2019, 7:17:50 AM2/5/19
to Django users
While working with `AsyncHttpConsumer` objects, I noticed that uncaught exceptions cause the response to hang. In most cases, my code handles exceptions but in the off chance that an exception slips by, I'd like to catch it. Here's an example consumer that hangs:


```
class Example(AsyncHttpConsumer):
    async def handle(self, body):
        await self.send_response(500, 'this should be bytes')    # param 2 should be bytes

```


I tried to use a middleware that has a `process_exception()` but then I found this issue https://github.com/django/channels/issues/407.
Then I stumbled upon `handle_uncaught_exception()` but I don't know how to trigger it from the consumer.
Is there a way I can catch unhandled exceptions? The issue says to "hook into the logger", but I don't see any documentation on how to do that.
I would imagine there's a error_back or try/except on the `consumer.handle()` call that I can hook into as well.
Reply all
Reply to author
Forward
0 new messages