简体   繁体   English

龙卷风websocket处理程序未捕获的异常

[英]tornado websocket handler uncaught exception

I'm trying to catch uncaught exceptions in my WebSocketHandler. 我正在尝试在WebSocketHandler中捕获未捕获的异常。 But Tornado's WebSocketHandler does not have the same exception handling interface as the HTTP RequestHandler. 但是Tornado的WebSocketHandler没有与HTTP RequestHandler相同的异常处理接口。

In the above source, it looks like exceptions are caught and logged here: 在以上源代码中,看起来好像捕获了异常并在此处记录了异常:

def _run_callback(self, callback, *args, **kwargs):
    """Runs the given callback with exception handling.

    On error, aborts the websocket connection and returns False.
    """
    try:
        callback(*args, **kwargs)
    except Exception:
        app_log.error("Uncaught exception in %s",
                      self.request.path, exc_info=True)
        self._abort()

Instead of hacking the handler and subclassing WebSocketProtocol which implements the above method, is there a better way to catch exceptions in the WebSocketHandler ? 除了破解实现上述方法的处理程序并将其子类化WebSocketProtocol之外,还有没有更好的方法来捕获WebSocketHandler异常?

Subclassing and overriding WebSocketProtocol._run_callback is currently the best way to do this from a base class. 子类化和重写WebSocketProtocol._run_callback当前是从基类执行此操作的最佳方法。 But since it's cumbersome to subclass and override this method, it's probably better to handle this at the application level until proper support can be added to the framework . 但是,由于继承和覆盖此方法很麻烦,因此最好在应用程序级别进行处理,直到可以向框架中添加适当的支持为止。 Either wrap all your callbacks in a big try/except block or if you have enough of them that this gets irritating, you can make your own exception-catching decorator. 可以将所有回调包装在一个较大的try / except块中,或者如果您有足够的回调使它们烦人,则可以制作自己的异常装饰器。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM