From 0e2738d84952953a64e722bcb34c627ebf63a47d Mon Sep 17 00:00:00 2001 From: Fabio Manganiello Date: Sun, 5 Nov 2023 01:03:40 +0100 Subject: [PATCH] Added `_on_error` handler to `StreamingRoute`. --- platypush/backend/http/app/streaming/_base.py | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/platypush/backend/http/app/streaming/_base.py b/platypush/backend/http/app/streaming/_base.py index d5646751f..aea3d58d5 100644 --- a/platypush/backend/http/app/streaming/_base.py +++ b/platypush/backend/http/app/streaming/_base.py @@ -6,6 +6,7 @@ from typing import Optional from tornado.web import RequestHandler, stream_request_body +from platypush.backend.http.app.utils import logger from platypush.backend.http.app.utils.auth import AuthStatus, get_auth_status from ..mixins import PubSubMixin @@ -121,3 +122,20 @@ class StreamingRoute(RequestHandler, PubSubMixin, ABC): return self.request.connection.stream.closed() # type: ignore return True + + def _on_error(self, e: Exception): + """ + Handler for uncaught exceptions. + """ + if isinstance(e, FileNotFoundError): + self.set_status(404, str(e)) + elif isinstance(e, NotImplementedError): + self.set_status(422, str(e)) + else: + logger().exception(e) + if isinstance(e, AssertionError): + self.set_status(400, str(e)) + else: + self.set_status(500, str(e)) + + self.finish(json.dumps({"error": str(e)}))