Try not to stringify exception in logging messages

This just fixes the worst offenders that trip up error reporting tools
like Sentry.io
This commit is contained in:
Markus Unterwaditzer 2018-09-29 22:20:20 +02:00
parent 076cf51fb2
commit 0e33d46ead
4 changed files with 21 additions and 19 deletions

View File

@ -41,4 +41,4 @@ if __name__ == "__main__":
" Example Module: project.sanic_server.app" " Example Module: project.sanic_server.app"
.format(e.name)) .format(e.name))
except ValueError as e: except ValueError as e:
logger.error("{}".format(e)) logger.exception("Failed to run app")

View File

@ -1,5 +1,5 @@
import sys import sys
from traceback import format_exc, extract_tb from traceback import extract_tb
from sanic.exceptions import ( from sanic.exceptions import (
ContentRangeError, ContentRangeError,
@ -83,16 +83,16 @@ class ErrorHandler:
if response is None: if response is None:
response = self.default(request, exception) response = self.default(request, exception)
except Exception: except Exception:
self.log(format_exc()) try:
if self.debug: url = repr(request.url)
url = getattr(request, 'url', 'unknown') except AttributeError:
url = "unknown"
response_message = ('Exception raised in exception handler ' response_message = ('Exception raised in exception handler '
'"%s" for uri: "%s"\n%s') '"%s" for uri: %s')
logger.error(response_message, logger.exception(response_message, handler.__name__, url)
handler.__name__, url, format_exc())
return text(response_message % ( if self.debug:
handler.__name__, url, format_exc()), 500) return text(response_message % (handler.__name__, url), 500)
else: else:
return text('An error occurred while handling an error', 500) return text('An error occurred while handling an error', 500)
return response return response
@ -105,7 +105,14 @@ class ErrorHandler:
getattr(logger, level)(message) getattr(logger, level)(message)
def default(self, request, exception): def default(self, request, exception):
self.log(format_exc()) try:
url = repr(request.url)
except AttributeError:
url = "unknown"
response_message = ('Exception occurred while handling uri: %s')
logger.exception(response_message, url)
if issubclass(type(exception), SanicException): if issubclass(type(exception), SanicException):
return text( return text(
'Error: {}'.format(exception), 'Error: {}'.format(exception),
@ -115,9 +122,6 @@ class ErrorHandler:
elif self.debug: elif self.debug:
html_output = self._render_traceback_html(exception, request) html_output = self._render_traceback_html(exception, request)
response_message = ('Exception occurred while handling uri: '
'"%s"\n%s')
logger.error(response_message, request.url, format_exc())
return html(html_output, status=500) return html(html_output, status=500)
else: else:
return html(INTERNAL_SERVER_ERROR_HTML, status=500) return html(INTERNAL_SERVER_ERROR_HTML, status=500)

View File

@ -441,7 +441,7 @@ class HttpProtocol(asyncio.Protocol):
logger.error("Transport closed @ %s and exception " logger.error("Transport closed @ %s and exception "
"experienced during error handling", "experienced during error handling",
self.transport.get_extra_info('peername')) self.transport.get_extra_info('peername'))
logger.debug('Exception:\n%s', traceback.format_exc()) logger.debug('Exception:', exc_info=True)
else: else:
exception = ServerError(message) exception = ServerError(message)
self.write_error(exception) self.write_error(exception)

View File

@ -1,4 +1,3 @@
import traceback
from json import JSONDecodeError from json import JSONDecodeError
from sanic.log import logger from sanic.log import logger
from sanic.exceptions import MethodNotSupported from sanic.exceptions import MethodNotSupported
@ -73,8 +72,7 @@ class SanicTestClient:
**request_kwargs) **request_kwargs)
results[-1] = response results[-1] = response
except Exception as e: except Exception as e:
logger.error( logger.exception('Exception')
'Exception:\n{}'.format(traceback.format_exc()))
exceptions.append(e) exceptions.append(e)
self.app.stop() self.app.stop()