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
|
status_code = 404
|
||||||
class InvalidUsage(Exception):
|
class InvalidUsage(SanicException):
|
||||||
status_code = 400
|
status_code = 400
|
||||||
class ServerError(Exception):
|
class ServerError(SanicException):
|
||||||
status_code = 500
|
status_code = 500
|
||||||
|
|
||||||
class Handler:
|
class Handler:
|
||||||
handlers = None
|
handlers = None
|
||||||
|
debug = False
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
self.handlers = {}
|
self.handlers = {}
|
||||||
|
|
||||||
|
@ -20,9 +25,14 @@ class Handler:
|
||||||
if handler:
|
if handler:
|
||||||
response = handler(request, exception)
|
response = handler(request, exception)
|
||||||
else:
|
else:
|
||||||
response = Handler.default(request, exception)
|
response = Handler.default(request, exception, self.debug)
|
||||||
return response
|
return response
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def default(request, exception):
|
def default(request, exception, debug):
|
||||||
return html("Error: {}".format(exception), status=getattr(exception, 'status_code', 500))
|
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:
|
class Sanic:
|
||||||
name = None
|
name = None
|
||||||
|
debug = None
|
||||||
router = None
|
router = None
|
||||||
error_handler = None
|
error_handler = None
|
||||||
routes = []
|
routes = []
|
||||||
|
@ -15,15 +16,21 @@ class Sanic:
|
||||||
self.router = router or Router()
|
self.router = router or Router()
|
||||||
self.error_handler = error_handler or Handler()
|
self.error_handler = error_handler or Handler()
|
||||||
|
|
||||||
def route(self, *args, **kwargs):
|
def route(self, uri):
|
||||||
def response(handler):
|
def response(handler):
|
||||||
self.add_route(handler, *args, **kwargs)
|
self.router.add(uri=uri, handler=handler)
|
||||||
return handler
|
return handler
|
||||||
|
|
||||||
return response
|
return response
|
||||||
|
|
||||||
def add_route(self, handler, *args, **kwargs):
|
def handler(self, *args, **kwargs):
|
||||||
self.router.add(*args, **kwargs, handler=handler)
|
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):
|
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)
|
return serve(sanic=self, host=host, port=port, debug=debug)
|
|
@ -8,6 +8,7 @@ import logging
|
||||||
from inspect import iscoroutine
|
from inspect import iscoroutine
|
||||||
from ujson import loads as json_loads
|
from ujson import loads as json_loads
|
||||||
from urllib.parse import parse_qs
|
from urllib.parse import parse_qs
|
||||||
|
from traceback import format_exc
|
||||||
|
|
||||||
import httptools
|
import httptools
|
||||||
try:
|
try:
|
||||||
|
@ -157,7 +158,10 @@ class HttpProtocol(asyncio.Protocol):
|
||||||
try:
|
try:
|
||||||
response = self.sanic.error_handler.response(request, e)
|
response = self.sanic.error_handler.response(request, e)
|
||||||
except Exception as 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)
|
self.write_response(request, response)
|
||||||
|
|
||||||
|
|
2
test.py
2
test.py
|
@ -28,4 +28,4 @@ def test(request):
|
||||||
def test(request):
|
def test(request):
|
||||||
return json({ "parsed": True, "args": request.args, "url": request.url, "query_string": request.query_string })
|
return json({ "parsed": True, "args": request.args, "url": request.url, "query_string": request.query_string })
|
||||||
|
|
||||||
app.run(host="0.0.0.0")
|
app.run(host="0.0.0.0", debug=True)
|
Loading…
Reference in New Issue
Block a user