2018-09-25 18:46:40 +01:00
|
|
|
from sanic.helpers import STATUS_CODES
|
2017-05-19 21:35:04 +01:00
|
|
|
|
2018-10-18 05:20:16 +01:00
|
|
|
|
2018-10-14 01:55:33 +01:00
|
|
|
TRACEBACK_STYLE = """
|
2017-01-13 00:54:34 +00:00
|
|
|
<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;
|
|
|
|
}
|
|
|
|
|
2017-05-01 17:56:33 +01:00
|
|
|
.tb-border {
|
|
|
|
padding-top: 20px;
|
|
|
|
}
|
|
|
|
|
2017-01-13 00:54:34 +00:00
|
|
|
.frame-descriptor {
|
|
|
|
background-color: #e2eafb;
|
|
|
|
}
|
|
|
|
|
|
|
|
.frame-descriptor {
|
|
|
|
font-size: 14px;
|
|
|
|
}
|
|
|
|
</style>
|
2018-10-14 01:55:33 +01:00
|
|
|
"""
|
2017-01-13 00:54:34 +00:00
|
|
|
|
2018-10-14 01:55:33 +01:00
|
|
|
TRACEBACK_WRAPPER_HTML = """
|
2017-01-13 00:54:34 +00:00
|
|
|
<html>
|
|
|
|
<head>
|
|
|
|
{style}
|
|
|
|
</head>
|
|
|
|
<body>
|
2017-05-01 17:56:33 +01:00
|
|
|
{inner_html}
|
|
|
|
<div class="summary">
|
|
|
|
<p>
|
2017-01-13 00:54:34 +00:00
|
|
|
<b>{exc_name}: {exc_value}</b>
|
2017-03-03 16:49:35 +00:00
|
|
|
while handling path <code>{path}</code>
|
2017-01-13 00:54:34 +00:00
|
|
|
</p>
|
|
|
|
</div>
|
|
|
|
</body>
|
|
|
|
</html>
|
2018-10-14 01:55:33 +01:00
|
|
|
"""
|
2017-01-13 00:54:34 +00:00
|
|
|
|
2018-10-14 01:55:33 +01:00
|
|
|
TRACEBACK_WRAPPER_INNER_HTML = """
|
2017-05-01 17:56:33 +01:00
|
|
|
<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}
|
|
|
|
</div>
|
2018-10-14 01:55:33 +01:00
|
|
|
"""
|
2017-05-01 17:56:33 +01:00
|
|
|
|
2018-10-14 01:55:33 +01:00
|
|
|
TRACEBACK_BORDER = """
|
2017-05-01 17:56:33 +01:00
|
|
|
<div class="tb-border">
|
|
|
|
<b><i>
|
|
|
|
The above exception was the direct cause of the
|
|
|
|
following exception:
|
|
|
|
</i></b>
|
|
|
|
</div>
|
2018-10-14 01:55:33 +01:00
|
|
|
"""
|
2017-05-01 17:56:33 +01:00
|
|
|
|
2018-10-14 01:55:33 +01:00
|
|
|
TRACEBACK_LINE_HTML = """
|
2017-01-13 00:54:34 +00:00
|
|
|
<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>
|
2018-10-14 01:55:33 +01:00
|
|
|
"""
|
2017-01-13 00:54:34 +00:00
|
|
|
|
2018-10-14 01:55:33 +01:00
|
|
|
INTERNAL_SERVER_ERROR_HTML = """
|
2017-01-13 00:54:34 +00:00
|
|
|
<h1>Internal Server Error</h1>
|
|
|
|
<p>
|
|
|
|
The server encountered an internal error and cannot complete
|
|
|
|
your request.
|
|
|
|
</p>
|
2018-10-14 01:55:33 +01:00
|
|
|
"""
|
2016-10-03 04:47:15 +01:00
|
|
|
|
2016-10-15 03:53:49 +01:00
|
|
|
|
2017-05-20 18:44:09 +01:00
|
|
|
_sanic_exceptions = {}
|
2017-05-20 09:21:09 +01:00
|
|
|
|
|
|
|
|
|
|
|
def add_status_code(code):
|
|
|
|
"""
|
2017-05-20 18:29:00 +01:00
|
|
|
Decorator used for adding exceptions to _sanic_exceptions.
|
2017-05-20 09:21:09 +01:00
|
|
|
"""
|
2018-10-14 01:55:33 +01:00
|
|
|
|
2017-05-20 09:21:09 +01:00
|
|
|
def class_decorator(cls):
|
|
|
|
cls.status_code = code
|
2017-05-20 18:29:00 +01:00
|
|
|
_sanic_exceptions[code] = cls
|
2017-05-20 09:21:09 +01:00
|
|
|
return cls
|
2018-10-14 01:55:33 +01:00
|
|
|
|
2017-05-20 09:21:09 +01:00
|
|
|
return class_decorator
|
|
|
|
|
|
|
|
|
2016-10-03 05:11:38 +01:00
|
|
|
class SanicException(Exception):
|
2017-05-19 22:40:01 +01:00
|
|
|
def __init__(self, message, status_code=None):
|
|
|
|
super().__init__(message)
|
|
|
|
|
2016-10-15 03:53:49 +01:00
|
|
|
if status_code is not None:
|
|
|
|
self.status_code = status_code
|
|
|
|
|
2016-10-03 05:11:38 +01:00
|
|
|
|
2017-05-20 09:21:09 +01:00
|
|
|
@add_status_code(404)
|
2016-10-03 05:11:38 +01:00
|
|
|
class NotFound(SanicException):
|
2017-05-21 03:30:08 +01:00
|
|
|
pass
|
2016-10-15 03:53:49 +01:00
|
|
|
|
|
|
|
|
2017-05-20 09:21:09 +01:00
|
|
|
@add_status_code(400)
|
2016-10-03 05:11:38 +01:00
|
|
|
class InvalidUsage(SanicException):
|
2017-05-21 03:30:08 +01:00
|
|
|
pass
|
2016-10-15 03:53:49 +01:00
|
|
|
|
|
|
|
|
2017-12-12 04:11:20 +00:00
|
|
|
@add_status_code(405)
|
|
|
|
class MethodNotSupported(SanicException):
|
|
|
|
def __init__(self, message, method, allowed_methods):
|
|
|
|
super().__init__(message)
|
|
|
|
self.headers = dict()
|
|
|
|
self.headers["Allow"] = ", ".join(allowed_methods)
|
2018-10-14 01:55:33 +01:00
|
|
|
if method in ["HEAD", "PATCH", "PUT", "DELETE"]:
|
|
|
|
self.headers["Content-Length"] = 0
|
2017-12-12 04:11:20 +00:00
|
|
|
|
|
|
|
|
2017-05-20 09:21:09 +01:00
|
|
|
@add_status_code(500)
|
2016-10-03 05:11:38 +01:00
|
|
|
class ServerError(SanicException):
|
2017-05-21 03:30:08 +01:00
|
|
|
pass
|
2016-10-15 03:53:49 +01:00
|
|
|
|
2016-10-03 04:47:15 +01:00
|
|
|
|
2017-09-11 08:17:33 +01:00
|
|
|
@add_status_code(503)
|
|
|
|
class ServiceUnavailable(SanicException):
|
|
|
|
"""The server is currently unavailable (because it is overloaded or
|
|
|
|
down for maintenance). Generally, this is a temporary state."""
|
2018-10-14 01:55:33 +01:00
|
|
|
|
2017-09-11 08:17:33 +01:00
|
|
|
pass
|
|
|
|
|
|
|
|
|
2017-05-21 03:30:08 +01:00
|
|
|
class URLBuildError(ServerError):
|
|
|
|
pass
|
2017-02-02 17:21:14 +00:00
|
|
|
|
|
|
|
|
2016-10-24 09:21:06 +01:00
|
|
|
class FileNotFound(NotFound):
|
|
|
|
def __init__(self, message, path, relative_url):
|
|
|
|
super().__init__(message)
|
|
|
|
self.path = path
|
|
|
|
self.relative_url = relative_url
|
|
|
|
|
|
|
|
|
2017-05-20 09:21:09 +01:00
|
|
|
@add_status_code(408)
|
2016-11-26 04:55:45 +00:00
|
|
|
class RequestTimeout(SanicException):
|
2017-09-11 08:17:33 +01:00
|
|
|
"""The Web server (running the Web site) thinks that there has been too
|
|
|
|
long an interval of time between 1) the establishment of an IP
|
|
|
|
connection (socket) between the client and the server and
|
|
|
|
2) the receipt of any data on that socket, so the server has dropped
|
|
|
|
the connection. The socket connection has actually been lost - the Web
|
|
|
|
server has 'timed out' on that particular socket connection.
|
|
|
|
"""
|
2018-10-14 01:55:33 +01:00
|
|
|
|
2017-05-21 03:30:08 +01:00
|
|
|
pass
|
2016-11-26 04:55:45 +00:00
|
|
|
|
|
|
|
|
2017-05-20 09:21:09 +01:00
|
|
|
@add_status_code(413)
|
2016-12-04 01:50:32 +00:00
|
|
|
class PayloadTooLarge(SanicException):
|
2017-05-21 03:30:08 +01:00
|
|
|
pass
|
2016-12-04 01:50:32 +00:00
|
|
|
|
2017-02-09 01:59:34 +00:00
|
|
|
|
2017-05-21 03:30:08 +01:00
|
|
|
class HeaderNotFound(InvalidUsage):
|
|
|
|
pass
|
2017-01-31 01:04:51 +00:00
|
|
|
|
|
|
|
|
2017-05-20 09:21:09 +01:00
|
|
|
@add_status_code(416)
|
2017-01-31 01:04:51 +00:00
|
|
|
class ContentRangeError(SanicException):
|
|
|
|
def __init__(self, message, content_range):
|
|
|
|
super().__init__(message)
|
|
|
|
self.headers = {
|
2018-10-14 01:55:33 +01:00
|
|
|
"Content-Type": "text/plain",
|
|
|
|
"Content-Range": "bytes */%s" % (content_range.total,),
|
2017-01-31 01:04:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-06-23 15:44:57 +01:00
|
|
|
@add_status_code(403)
|
|
|
|
class Forbidden(SanicException):
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
2017-01-31 01:04:51 +00:00
|
|
|
class InvalidRangeType(ContentRangeError):
|
|
|
|
pass
|
2017-05-19 22:40:01 +01:00
|
|
|
|
|
|
|
|
2018-10-03 13:27:59 +01:00
|
|
|
class PyFileError(Exception):
|
|
|
|
def __init__(self, file):
|
2018-10-14 01:55:33 +01:00
|
|
|
super().__init__("could not execute config file %s", file)
|
2018-10-03 13:27:59 +01:00
|
|
|
|
|
|
|
|
2017-06-23 15:12:15 +01:00
|
|
|
@add_status_code(401)
|
|
|
|
class Unauthorized(SanicException):
|
|
|
|
"""
|
|
|
|
Unauthorized exception (401 HTTP status code).
|
|
|
|
|
2017-07-27 22:00:27 +01:00
|
|
|
:param message: Message describing the exception.
|
2017-08-24 15:46:39 +01:00
|
|
|
:param status_code: HTTP Status code.
|
2017-06-23 15:12:15 +01:00
|
|
|
:param scheme: Name of the authentication scheme to be used.
|
2017-07-27 22:00:27 +01:00
|
|
|
|
2017-07-28 21:29:45 +01:00
|
|
|
When present, kwargs is used to complete the WWW-Authentication header.
|
2017-06-29 11:34:52 +01:00
|
|
|
|
|
|
|
Examples::
|
|
|
|
|
|
|
|
# With a Basic auth-scheme, realm MUST be present:
|
2017-08-24 15:46:39 +01:00
|
|
|
raise Unauthorized("Auth required.",
|
|
|
|
scheme="Basic",
|
|
|
|
realm="Restricted Area")
|
2017-06-29 11:34:52 +01:00
|
|
|
|
|
|
|
# With a Digest auth-scheme, things are a bit more complicated:
|
2017-07-27 22:00:27 +01:00
|
|
|
raise Unauthorized("Auth required.",
|
2017-08-24 15:46:39 +01:00
|
|
|
scheme="Digest",
|
2017-07-27 22:00:27 +01:00
|
|
|
realm="Restricted Area",
|
|
|
|
qop="auth, auth-int",
|
|
|
|
algorithm="MD5",
|
|
|
|
nonce="abcdef",
|
|
|
|
opaque="zyxwvu")
|
|
|
|
|
|
|
|
# With a Bearer auth-scheme, realm is optional so you can write:
|
2017-08-24 15:46:39 +01:00
|
|
|
raise Unauthorized("Auth required.", scheme="Bearer")
|
2017-07-27 22:00:27 +01:00
|
|
|
|
|
|
|
# or, if you want to specify the realm:
|
2017-08-24 15:46:39 +01:00
|
|
|
raise Unauthorized("Auth required.",
|
|
|
|
scheme="Bearer",
|
|
|
|
realm="Restricted Area")
|
2017-06-23 15:12:15 +01:00
|
|
|
"""
|
2018-10-14 01:55:33 +01:00
|
|
|
|
2017-08-24 15:46:39 +01:00
|
|
|
def __init__(self, message, status_code=None, scheme=None, **kwargs):
|
|
|
|
super().__init__(message, status_code)
|
2017-06-23 15:12:15 +01:00
|
|
|
|
2017-08-24 15:46:39 +01:00
|
|
|
# if auth-scheme is specified, set "WWW-Authenticate" header
|
|
|
|
if scheme is not None:
|
2017-12-22 01:54:57 +00:00
|
|
|
values = ['{!s}="{!s}"'.format(k, v) for k, v in kwargs.items()]
|
2018-10-14 01:55:33 +01:00
|
|
|
challenge = ", ".join(values)
|
2017-06-23 15:12:15 +01:00
|
|
|
|
2017-08-24 15:46:39 +01:00
|
|
|
self.headers = {
|
|
|
|
"WWW-Authenticate": "{} {}".format(scheme, challenge).rstrip()
|
|
|
|
}
|
2017-06-23 15:12:15 +01:00
|
|
|
|
|
|
|
|
2017-05-19 22:40:01 +01:00
|
|
|
def abort(status_code, message=None):
|
|
|
|
"""
|
|
|
|
Raise an exception based on SanicException. Returns the HTTP response
|
|
|
|
message appropriate for the given status code, unless provided.
|
2017-07-02 18:02:04 +01:00
|
|
|
|
2017-05-19 22:40:01 +01:00
|
|
|
:param status_code: The HTTP status code to return.
|
|
|
|
:param message: The HTTP response body. Defaults to the messages
|
2017-07-02 18:02:04 +01:00
|
|
|
in response.py for the given status code.
|
2017-05-19 22:40:01 +01:00
|
|
|
"""
|
|
|
|
if message is None:
|
2017-10-20 00:43:07 +01:00
|
|
|
message = STATUS_CODES.get(status_code)
|
2017-05-21 03:30:08 +01:00
|
|
|
# These are stored as bytes in the STATUS_CODES dict
|
2018-10-14 01:55:33 +01:00
|
|
|
message = message.decode("utf8")
|
2017-05-20 18:29:00 +01:00
|
|
|
sanic_exception = _sanic_exceptions.get(status_code, SanicException)
|
2017-05-19 22:40:01 +01:00
|
|
|
raise sanic_exception(message=message, status_code=status_code)
|