From af2cf771d4a3d6271e73e93c9ff64280c1f17fba Mon Sep 17 00:00:00 2001 From: Channel Cat Date: Sun, 2 Oct 2016 21:11:38 -0700 Subject: [PATCH] Added better debug info --- sanic/exceptions.py | 24 +++++++++++++++++------- sanic/sanic.py | 15 +++++++++++---- sanic/server.py | 6 +++++- test.py | 2 +- 4 files changed, 34 insertions(+), 13 deletions(-) diff --git a/sanic/exceptions.py b/sanic/exceptions.py index c648512d..f3f191cf 100644 --- a/sanic/exceptions.py +++ b/sanic/exceptions.py @@ -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)) \ No newline at end of file + 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) \ No newline at end of file diff --git a/sanic/sanic.py b/sanic/sanic.py index 86d1df67..53604791 100644 --- a/sanic/sanic.py +++ b/sanic/sanic.py @@ -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) \ No newline at end of file diff --git a/sanic/server.py b/sanic/server.py index 4da74b6d..ed0b6755 100644 --- a/sanic/server.py +++ b/sanic/server.py @@ -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) diff --git a/test.py b/test.py index 0b0073bd..d5a70f68 100644 --- a/test.py +++ b/test.py @@ -28,4 +28,4 @@ def test(request): def test(request): return json({ "parsed": True, "args": request.args, "url": request.url, "query_string": request.query_string }) -app.run(host="0.0.0.0") \ No newline at end of file +app.run(host="0.0.0.0", debug=True) \ No newline at end of file