app: return HTTP errors in json format

Signed-off-by:  Eric Callahan <arksine.code@gmail.com>
This commit is contained in:
Arksine 2021-04-13 17:20:21 -04:00
parent 14991ac3b9
commit 0ce53bd98f
1 changed files with 22 additions and 0 deletions

View File

@ -9,6 +9,7 @@ import mimetypes
import logging
import json
import datetime
import traceback
import tornado
import tornado.iostream
import tornado.httputil
@ -299,6 +300,13 @@ class AuthorizedRequestHandler(tornado.web.RequestHandler):
conn = wsm.get_websocket(conn_id)
return conn
def write_error(self, status_code, **kwargs):
err = {'code': status_code, 'message': self._reason}
if 'exc_info' in kwargs:
err['traceback'] = "\n".join(
traceback.format_exception(*kwargs['exc_info']))
self.finish({'error': err})
# Due to the way Python treats multiple inheritance its best
# to create a separate authorized handler for serving files
class AuthorizedFileHandler(tornado.web.StaticFileHandler):
@ -329,6 +337,13 @@ class AuthorizedFileHandler(tornado.web.StaticFileHandler):
else:
super(AuthorizedFileHandler, self).options()
def write_error(self, status_code, **kwargs):
err = {'code': status_code, 'message': self._reason}
if 'exc_info' in kwargs:
err['traceback'] = "\n".join(
traceback.format_exception(*kwargs['exc_info']))
self.finish({'error': err})
class DynamicRequestHandler(AuthorizedRequestHandler):
def initialize(self, callback, methods, need_object_parser=False,
is_remote=True, wrap_result=True):
@ -624,3 +639,10 @@ class AuthorizedErrorHandler(AuthorizedRequestHandler):
def check_xsrf_cookie(self):
pass
def write_error(self, status_code, **kwargs):
err = {'code': status_code, 'message': self._reason}
if 'exc_info' in kwargs:
err['traceback'] = "\n".join(
traceback.format_exception(*kwargs['exc_info']))
self.finish({'error': err})