Added better debug info
This commit is contained in:
parent
b59dc2729f
commit
af2cf771d4
|
@ -1,14 +1,19 @@
|
|||
from .response import html
|
||||
from .response import text
|
||||
from traceback import format_exc
|
||||
|
||||
class NotFound(Exception):
|
||||
class SanicException(Exception):
|
||||
pass
|
||||
|
||||
class NotFound(SanicException):
|
||||
status_code = 404
|
||||
class InvalidUsage(Exception):
|
||||
class InvalidUsage(SanicException):
|
||||
status_code = 400
|
||||
class ServerError(Exception):
|
||||
class ServerError(SanicException):
|
||||
status_code = 500
|
||||
|
||||
class Handler:
|
||||
handlers = None
|
||||
debug = False
|
||||
def __init__(self):
|
||||
self.handlers = {}
|
||||
|
||||
|
@ -20,9 +25,14 @@ class Handler:
|
|||
if handler:
|
||||
response = handler(request, exception)
|
||||
else:
|
||||
response = Handler.default(request, exception)
|
||||
response = Handler.default(request, exception, self.debug)
|
||||
return response
|
||||
|
||||
@staticmethod
|
||||
def default(request, exception):
|
||||
return html("Error: {}".format(exception), status=getattr(exception, 'status_code', 500))
|
||||
def default(request, exception, debug):
|
||||
if issubclass(type(exception), SanicException):
|
||||
return text("Error: {}".format(exception), status=getattr(exception, 'status_code', 500))
|
||||
elif debug:
|
||||
return text("Error: {}\nException: {}".format(exception, format_exc()), status=500)
|
||||
else:
|
||||
return text("An error occurred while generating the request", status=500)
|
|
@ -6,6 +6,7 @@ from .log import log
|
|||
|
||||
class Sanic:
|
||||
name = None
|
||||
debug = None
|
||||
router = None
|
||||
error_handler = None
|
||||
routes = []
|
||||
|
@ -15,15 +16,21 @@ class Sanic:
|
|||
self.router = router or Router()
|
||||
self.error_handler = error_handler or Handler()
|
||||
|
||||
def route(self, *args, **kwargs):
|
||||
def route(self, uri):
|
||||
def response(handler):
|
||||
self.add_route(handler, *args, **kwargs)
|
||||
self.router.add(uri=uri, handler=handler)
|
||||
return handler
|
||||
|
||||
return response
|
||||
|
||||
def add_route(self, handler, *args, **kwargs):
|
||||
self.router.add(*args, **kwargs, handler=handler)
|
||||
def handler(self, *args, **kwargs):
|
||||
def response(handler):
|
||||
self.error_handler.add(*args, **kwargs)
|
||||
return handler
|
||||
|
||||
return response
|
||||
|
||||
def run(self, host="127.0.0.1", port=8000, debug=False):
|
||||
self.error_handler.debug=True
|
||||
self.debug = debug
|
||||
return serve(sanic=self, host=host, port=port, debug=debug)
|
|
@ -8,6 +8,7 @@ import logging
|
|||
from inspect import iscoroutine
|
||||
from ujson import loads as json_loads
|
||||
from urllib.parse import parse_qs
|
||||
from traceback import format_exc
|
||||
|
||||
import httptools
|
||||
try:
|
||||
|
@ -157,7 +158,10 @@ class HttpProtocol(asyncio.Protocol):
|
|||
try:
|
||||
response = self.sanic.error_handler.response(request, e)
|
||||
except Exception as e:
|
||||
response = HTTPResponse("Error while handling error: {}".format(e))
|
||||
if self.sanic.debug:
|
||||
response = HTTPResponse("Error while handling error: {}\nStack: {}".format(e, format_exc()))
|
||||
else:
|
||||
response = HTTPResponse("An error occured while handling an error")
|
||||
|
||||
self.write_response(request, response)
|
||||
|
||||
|
|
Loading…
Reference in New Issue
Block a user