Add more verbose error handling

* Adds logging to error messages in debug mode as pointed out in PR #249,
while also improving the debug message.
This commit is contained in:
Eli Uriegas 2016-12-30 13:48:17 -06:00
parent 3ada6f358c
commit 7a8fd6b0df

View File

@ -1,4 +1,5 @@
from .response import text from .response import text
from .log import log
from traceback import format_exc from traceback import format_exc
@ -56,18 +57,31 @@ class Handler:
:return: Response object :return: Response object
""" """
handler = self.handlers.get(type(exception), self.default) handler = self.handlers.get(type(exception), self.default)
response = handler(request=request, exception=exception) try:
response = handler(request=request, exception=exception)
except:
if self.sanic.debug:
response_message = (
'Exception raised in exception handler "{}" '
'for uri: "{}"\n{}').format(
handler.__name__, request.url, format_exc())
log.error(response_message)
return text(response_message, 500)
else:
return text('An error occurred while handling an error', 500)
return response return response
def default(self, request, exception): def default(self, request, exception):
if issubclass(type(exception), SanicException): if issubclass(type(exception), SanicException):
return text( return text(
"Error: {}".format(exception), 'Error: {}'.format(exception),
status=getattr(exception, 'status_code', 500)) status=getattr(exception, 'status_code', 500))
elif self.sanic.debug: elif self.sanic.debug:
return text( response_message = (
"Error: {}\nException: {}".format( 'Exception occurred while handling uri: "{}"\n{}'.format(
exception, format_exc()), status=500) request.url, format_exc()))
log.error(response_message)
return text(response_message, status=500)
else: else:
return text( return text(
"An error occurred while generating the request", status=500) 'An error occurred while generating the response', status=500)