2017-01-13 00:54:34 +00:00
|
|
|
from .response import text, html
|
2016-12-30 19:48:17 +00:00
|
|
|
from .log import log
|
2017-01-13 00:54:34 +00:00
|
|
|
from traceback import format_exc, extract_tb
|
|
|
|
import sys
|
|
|
|
|
|
|
|
TRACEBACK_STYLE = '''
|
|
|
|
<style>
|
|
|
|
body {
|
|
|
|
padding: 20px;
|
|
|
|
font-family: Arial, sans-serif;
|
|
|
|
}
|
|
|
|
|
|
|
|
p {
|
|
|
|
margin: 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
.summary {
|
|
|
|
padding: 10px;
|
|
|
|
}
|
|
|
|
|
|
|
|
h1 {
|
|
|
|
margin-bottom: 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
h3 {
|
|
|
|
margin-top: 10px;
|
|
|
|
}
|
|
|
|
|
|
|
|
h3 code {
|
|
|
|
font-size: 24px;
|
|
|
|
}
|
|
|
|
|
|
|
|
.frame-line > * {
|
|
|
|
padding: 5px 10px;
|
|
|
|
}
|
|
|
|
|
|
|
|
.frame-line {
|
|
|
|
margin-bottom: 5px;
|
|
|
|
}
|
|
|
|
|
|
|
|
.frame-code {
|
|
|
|
font-size: 16px;
|
|
|
|
padding-left: 30px;
|
|
|
|
}
|
|
|
|
|
|
|
|
.tb-wrapper {
|
|
|
|
border: 1px solid #f3f3f3;
|
|
|
|
}
|
|
|
|
|
|
|
|
.tb-header {
|
|
|
|
background-color: #f3f3f3;
|
|
|
|
padding: 5px 10px;
|
|
|
|
}
|
|
|
|
|
|
|
|
.frame-descriptor {
|
|
|
|
background-color: #e2eafb;
|
|
|
|
}
|
|
|
|
|
|
|
|
.frame-descriptor {
|
|
|
|
font-size: 14px;
|
|
|
|
}
|
|
|
|
</style>
|
|
|
|
'''
|
|
|
|
|
|
|
|
TRACEBACK_WRAPPER_HTML = '''
|
|
|
|
<html>
|
|
|
|
<head>
|
|
|
|
{style}
|
|
|
|
</head>
|
|
|
|
<body>
|
|
|
|
<h1>{exc_name}</h1>
|
|
|
|
<h3><code>{exc_value}</code></h3>
|
|
|
|
<div class="tb-wrapper">
|
|
|
|
<p class="tb-header">Traceback (most recent call last):</p>
|
|
|
|
{frame_html}
|
|
|
|
<p class="summary">
|
|
|
|
<b>{exc_name}: {exc_value}</b>
|
|
|
|
while handling uri <code>{uri}</code>
|
|
|
|
</p>
|
|
|
|
</div>
|
|
|
|
</body>
|
|
|
|
</html>
|
|
|
|
'''
|
|
|
|
|
|
|
|
TRACEBACK_LINE_HTML = '''
|
|
|
|
<div class="frame-line">
|
|
|
|
<p class="frame-descriptor">
|
|
|
|
File {0.filename}, line <i>{0.lineno}</i>,
|
|
|
|
in <code><b>{0.name}</b></code>
|
|
|
|
</p>
|
|
|
|
<p class="frame-code"><code>{0.line}</code></p>
|
|
|
|
</div>
|
|
|
|
'''
|
|
|
|
|
|
|
|
INTERNAL_SERVER_ERROR_HTML = '''
|
|
|
|
<h1>Internal Server Error</h1>
|
|
|
|
<p>
|
|
|
|
The server encountered an internal error and cannot complete
|
|
|
|
your request.
|
|
|
|
</p>
|
|
|
|
'''
|
2016-10-03 04:47:15 +01:00
|
|
|
|
2016-10-15 03:53:49 +01:00
|
|
|
|
2016-10-03 05:11:38 +01:00
|
|
|
class SanicException(Exception):
|
2016-10-15 03:53:49 +01:00
|
|
|
def __init__(self, message, status_code=None):
|
|
|
|
super().__init__(message)
|
|
|
|
if status_code is not None:
|
|
|
|
self.status_code = status_code
|
|
|
|
|
2016-10-03 05:11:38 +01:00
|
|
|
|
|
|
|
class NotFound(SanicException):
|
2016-10-15 03:53:49 +01:00
|
|
|
status_code = 404
|
|
|
|
|
|
|
|
|
2016-10-03 05:11:38 +01:00
|
|
|
class InvalidUsage(SanicException):
|
2016-10-15 03:53:49 +01:00
|
|
|
status_code = 400
|
|
|
|
|
|
|
|
|
2016-10-03 05:11:38 +01:00
|
|
|
class ServerError(SanicException):
|
2016-10-15 03:53:49 +01:00
|
|
|
status_code = 500
|
|
|
|
|
2016-10-03 04:47:15 +01:00
|
|
|
|
2017-02-02 17:21:14 +00:00
|
|
|
class URLBuildError(SanicException):
|
|
|
|
status_code = 500
|
|
|
|
|
|
|
|
|
2016-10-24 09:21:06 +01:00
|
|
|
class FileNotFound(NotFound):
|
|
|
|
status_code = 404
|
|
|
|
|
|
|
|
def __init__(self, message, path, relative_url):
|
|
|
|
super().__init__(message)
|
|
|
|
self.path = path
|
|
|
|
self.relative_url = relative_url
|
|
|
|
|
|
|
|
|
2016-11-26 04:55:45 +00:00
|
|
|
class RequestTimeout(SanicException):
|
|
|
|
status_code = 408
|
|
|
|
|
|
|
|
|
2016-12-04 01:50:32 +00:00
|
|
|
class PayloadTooLarge(SanicException):
|
|
|
|
status_code = 413
|
|
|
|
|
|
|
|
|
2016-10-03 04:47:15 +01:00
|
|
|
class Handler:
|
2016-10-15 03:53:49 +01:00
|
|
|
handlers = None
|
2017-01-31 05:29:16 +00:00
|
|
|
cached_handlers = None
|
|
|
|
_missing = object()
|
2016-10-15 03:53:49 +01:00
|
|
|
|
2017-01-25 01:18:58 +00:00
|
|
|
def __init__(self):
|
2017-01-31 05:29:16 +00:00
|
|
|
self.handlers = []
|
|
|
|
self.cached_handlers = {}
|
2017-01-25 01:18:58 +00:00
|
|
|
self.debug = False
|
2016-10-15 03:53:49 +01:00
|
|
|
|
2017-01-13 00:54:34 +00:00
|
|
|
def _render_traceback_html(self, exception, request):
|
|
|
|
exc_type, exc_value, tb = sys.exc_info()
|
|
|
|
frames = extract_tb(tb)
|
|
|
|
|
|
|
|
frame_html = []
|
|
|
|
for frame in frames:
|
|
|
|
frame_html.append(TRACEBACK_LINE_HTML.format(frame))
|
|
|
|
|
|
|
|
return TRACEBACK_WRAPPER_HTML.format(
|
|
|
|
style=TRACEBACK_STYLE,
|
|
|
|
exc_name=exc_type.__name__,
|
|
|
|
exc_value=exc_value,
|
|
|
|
frame_html=''.join(frame_html),
|
|
|
|
uri=request.url)
|
|
|
|
|
2016-10-15 03:53:49 +01:00
|
|
|
def add(self, exception, handler):
|
2017-01-31 05:29:16 +00:00
|
|
|
self.handlers.append((exception, handler))
|
|
|
|
|
|
|
|
def lookup(self, exception):
|
|
|
|
handler = self.cached_handlers.get(exception, self._missing)
|
|
|
|
if handler is self._missing:
|
|
|
|
for exception_class, handler in self.handlers:
|
|
|
|
if isinstance(exception, exception_class):
|
|
|
|
self.cached_handlers[type(exception)] = handler
|
|
|
|
return handler
|
|
|
|
self.cached_handlers[type(exception)] = None
|
|
|
|
handler = None
|
|
|
|
return handler
|
2016-10-15 03:53:49 +01:00
|
|
|
|
|
|
|
def response(self, request, exception):
|
|
|
|
"""
|
2016-10-16 02:28:24 +01:00
|
|
|
Fetches and executes an exception handler and returns a response object
|
2016-12-25 09:43:45 +00:00
|
|
|
|
2016-10-15 03:53:49 +01:00
|
|
|
:param request: Request
|
|
|
|
:param exception: Exception to handle
|
|
|
|
:return: Response object
|
|
|
|
"""
|
2017-01-31 05:29:16 +00:00
|
|
|
handler = self.lookup(exception)
|
2016-12-30 19:48:17 +00:00
|
|
|
try:
|
2017-01-31 05:29:16 +00:00
|
|
|
response = handler and handler(
|
|
|
|
request=request, exception=exception)
|
|
|
|
if response is None:
|
|
|
|
response = self.default(request=request, exception=exception)
|
2016-12-30 19:48:17 +00:00
|
|
|
except:
|
2017-01-17 11:17:42 +00:00
|
|
|
log.error(format_exc())
|
2017-01-25 01:18:58 +00:00
|
|
|
if self.debug:
|
2016-12-30 19:48:17 +00:00
|
|
|
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)
|
2016-10-15 03:53:49 +01:00
|
|
|
return response
|
|
|
|
|
|
|
|
def default(self, request, exception):
|
2017-01-17 11:17:42 +00:00
|
|
|
log.error(format_exc())
|
2017-01-27 10:11:29 +00:00
|
|
|
if isinstance(exception, SanicException):
|
2016-10-16 14:01:59 +01:00
|
|
|
return text(
|
2016-12-30 19:48:17 +00:00
|
|
|
'Error: {}'.format(exception),
|
2016-10-16 14:01:59 +01:00
|
|
|
status=getattr(exception, 'status_code', 500))
|
2017-01-25 01:18:58 +00:00
|
|
|
elif self.debug:
|
2017-01-13 00:54:34 +00:00
|
|
|
html_output = self._render_traceback_html(exception, request)
|
|
|
|
|
2016-12-30 19:48:17 +00:00
|
|
|
response_message = (
|
|
|
|
'Exception occurred while handling uri: "{}"\n{}'.format(
|
|
|
|
request.url, format_exc()))
|
|
|
|
log.error(response_message)
|
2017-01-13 00:54:34 +00:00
|
|
|
return html(html_output, status=500)
|
2016-10-15 03:53:49 +01:00
|
|
|
else:
|
2017-01-13 00:54:34 +00:00
|
|
|
return html(INTERNAL_SERVER_ERROR_HTML, status=500)
|