如何查看渠道消费者引发的异常

How to see exceptions raised from a channels consumer

我开始使用 django-channels,我觉得它很棒。然而,调试消费者是痛苦的,因为当消费者内部引发一些异常时,没有任何内容打印到终端,websocket 只是断开连接。

未显示的异常类型不易识别。 AssertionError 以及其他一些系统都是这种情况,例如下面的代码:

class MexicoProgressConsumer(ProgressConsumer):
    def init(self, SSDBConfig, Sub_application):
        subappli = models.Sub_application.objects.get(pk=Sub_application)
        ...

使用错误数量的参数调用此方法不会在控制台上打印任何内容并断开 websocket。如果最后一行的 get 失败,同上。

有没有办法像查看其他异常一样查看这些异常?

我找到了解决问题的办法。我先定义一个装饰器:

import traceback
def catch_exception(f):
    def wrapper(*args, **kwargs):
        try:
            return f(*args, **kwargs)
        except StopConsumer:
            raise
        except Exception as e:
            print(traceback.format_exc().strip('\n'), '<--- from consumer')
            raise
    return wrapper

然后我为我所有的消费者定义一个基础 class,它以这种方式使用这个装饰器:

import inspect
class BaseConsumer(JsonWebsocketConsumer):
    def __getattribute__(self, name):
        value = object.__getattribute__(self, name)
        if inspect.ismethod(value):
            return catch_exception(value)
        return value

但仍然存在 2 个问题:

  • 通常显示的异常出现两次
  • 其他异常重复3、4次! (好像 class 层次结构的每一层都会触发)

第一种情况(KeyError)的例子:

Traceback (most recent call last):
  File "/home/alain/ADN/simutool/dbsimu/consumers.py", line 19, in wrapper
    return f(*args, **kwargs)
  File "/home/alain/ADN/simutool/dbsimu/consumers.py", line 31, in wrapper
    result = f(owner, **kwargs)
  File "/home/alain/ADN/simutool/dbsimu/consumers.py", line 110, in refresh
    data = super().refresh.__wrapped__(self)
  File "/home/alain/ADN/simutool/dbsimu/consumers.py", line 73, in refresh
    pvalue = round(data['toto'] * 100, 1)
KeyError: 'toto' <--- from consumer
Exception in thread Thread-3:
Traceback (most recent call last):
  File "/usr/lib/python3.6/threading.py", line 916, in _bootstrap_inner
    self.run()
  File "/usr/lib/python3.6/threading.py", line 864, in run
    self._target(*self._args, **self._kwargs)
  File "/home/alain/ADN/simutool/dbsimu/utils.py", line 193, in repeat
    self.repeat_func()
  File "/home/alain/ADN/simutool/dbsimu/consumers.py", line 19, in wrapper
    return f(*args, **kwargs)
  File "/home/alain/ADN/simutool/dbsimu/consumers.py", line 31, in wrapper
    result = f(owner, **kwargs)
  File "/home/alain/ADN/simutool/dbsimu/consumers.py", line 110, in refresh
    data = super().refresh.__wrapped__(self)
  File "/home/alain/ADN/simutool/dbsimu/consumers.py", line 73, in refresh
    pvalue = round(data['toto'] * 100, 1)
KeyError: 'toto'

第二种情况的例子(拼写错误的变量):

WebSocket CONNECT /ws/dbsimu/Simuflow_progress/ [127.0.0.1:55866]
Traceback (most recent call last):
  File "/home/alain/ADN/simutool/dbsimu/consumers.py", line 19, in wrapper
    return f(*args, **kwargs)
  File "/home/alain/ADN/simutool/dbsimu/consumers.py", line 57, in receive_json
    return getattr(self, icommand)(**data)
NameError: name 'icommand' is not defined <--- from consumer
Traceback (most recent call last):
  File "/home/alain/ADN/simutool/dbsimu/consumers.py", line 19, in wrapper
    return f(*args, **kwargs)
  File "/home/alain/.local/lib/python3.6/site-packages/channels/generic/websocket.py", line 125, in receive
    self.receive_json(self.decode_json(text_data), **kwargs)
  File "/home/alain/ADN/simutool/dbsimu/consumers.py", line 19, in wrapper
    return f(*args, **kwargs)
  File "/home/alain/ADN/simutool/dbsimu/consumers.py", line 57, in receive_json
    return getattr(self, icommand)(**data)
NameError: name 'icommand' is not defined <--- from consumer
Traceback (most recent call last):
  File "/home/alain/ADN/simutool/dbsimu/consumers.py", line 19, in wrapper
    return f(*args, **kwargs)
  File "/home/alain/.local/lib/python3.6/site-packages/channels/generic/websocket.py", line 60, in websocket_receive
    self.receive(text_data=message["text"])
  File "/home/alain/ADN/simutool/dbsimu/consumers.py", line 19, in wrapper
    return f(*args, **kwargs)
  File "/home/alain/.local/lib/python3.6/site-packages/channels/generic/websocket.py", line 125, in receive
    self.receive_json(self.decode_json(text_data), **kwargs)
  File "/home/alain/ADN/simutool/dbsimu/consumers.py", line 19, in wrapper
    return f(*args, **kwargs)
  File "/home/alain/ADN/simutool/dbsimu/consumers.py", line 57, in receive_json
    return getattr(self, icommand)(**data)
NameError: name 'icommand' is not defined <--- from consumer

如果有人有解决办法,请指教。

从 albar 的 answer 开始,我找到的解决方案是像这样定义一个装饰器

from functools import wraps
from logging import getLogger

from channels.exceptions import AcceptConnection, DenyConnection, StopConsumer

logger = getLogger("foo-logger")

def log_exceptions(f):
    @wraps(f)
    async def wrapper(*args, **kwargs):
        try:
            return await f(*args, **kwargs)
        except (AcceptConnection, DenyConnection, StopConsumer):
            raise
        except Exception as exception:
            if not getattr(exception, "logged_by_wrapper", False):
                logger.error(
                    "Unhandled exception occurred in {}:".format(f.__qualname__),
                    exc_info=exception,
                )
                setattr(exception, "logged_by_wrapper", True)
            raise

    return wrapper

这有几项改进:

  • 使用 functools.wraps 使包装函数更接近原始函数。
  • 使用 async/await 语法,因为我使用的是异步消费者(如果不是,请删除)
  • 不记录 django-channels 故意引发的几个异常。
  • 仅在未设置属性 logged_by_wrapper 时才记录异常。这导致异常仅被记录一次,因为我们在第一次记录后设置了属性。
  • 使用 python 的内置 logging 模块来记录错误。这会自动格式化异常和回溯,因为我们在 exc_info=exception.
  • 中提供了异常

然后我定义了一个 class 装饰器而不是基础 Class 来将其应用于消费者的方法

from inspect import iscoroutinefunction

def log_consumer_exceptions(klass):
    for method_name, method in list(klass.__dict__.items()):
        if iscoroutinefunction(method):
            setattr(klass, method_name, log_exceptions(method))

    return klass

这适用于 log_exceptions 消费者中定义的所有异步方法,但不适用于它继承的方法 - 即仅适用于我们为消费者定制的方法。