We are currently solving the following issue within our repository. Here is the issue text:
--- BEGIN ISSUE ---
Sanic exceptions
How does sanic exceptions are supposed to work? The docs states that 

> Exceptions can be thrown from within request handlers and will automatically be handled by Sanic. Exceptions take a message as their first argument, and can also take a status code to be passed back in the HTTP response.

This is my route
```python
@app.route("/")
async def test(request):
    abort(401)
```

When I make a request on the path I get a response of : 

>Internal Server Error
The server encountered an internal error and cannot complete your request.

`2017-08-24 10:18:43 - (sanic)[ERROR]: Traceback (most recent call last):
  File "/home/nikos/.virtualenvs/3.6/lib/python3.6/site-packages/sanic/app.py", line 503, in handle_request
    response = await response
  File "/home/nikos/Desktop/Side Projects/micro/test2.py", line 15, in test
    abort(401)
  File "/home/nikos/.virtualenvs/3.6/lib/python3.6/site-packages/sanic/exceptions.py", line 262, in abort
    raise sanic_exception(message=message, status_code=status_code)
TypeError: __init__() missing 1 required positional argument: 'scheme'`

Also after a bit the connection times out and the log trace is 

`2017-08-24 10:18:43 - (network)[INFO][127.0.0.1:34734]: GET http://0.0.0.0:8001/  500 144
2017-08-24 10:19:43 - (sanic)[ERROR]: Traceback (most recent call last):
  File "/home/nikos/.virtualenvs/3.6/lib/python3.6/site-packages/sanic/server.py", line 143, in connection_timeout
    raise RequestTimeout('Request Timeout')
sanic.exceptions.RequestTimeout: Request Timeout`

--- END ISSUE ---

Below are some code segments, each from a relevant file. One or more of these files may contain bugs.
--- BEGIN FILES ---
Path: `sanic/exceptions.py`
Content:
```
1 from sanic.response import ALL_STATUS_CODES, COMMON_STATUS_CODES
2 
3 TRACEBACK_STYLE = '''
4     <style>
5         body {
6             padding: 20px;
7             font-family: Arial, sans-serif;
8         }
9 
10         p {
11             margin: 0;
12         }
13 
14         .summary {
15             padding: 10px;
16         }
17 
18         h1 {
19             margin-bottom: 0;
20         }
21 
22         h3 {
23             margin-top: 10px;
24         }
25 
26         h3 code {
27             font-size: 24px;
28         }
29 
30         .frame-line > * {
31             padding: 5px 10px;
32         }
33 
34         .frame-line {
35             margin-bottom: 5px;
36         }
37 
38         .frame-code {
39             font-size: 16px;
40             padding-left: 30px;
41         }
42 
43         .tb-wrapper {
44             border: 1px solid #f3f3f3;
45         }
46 
47         .tb-header {
48             background-color: #f3f3f3;
49             padding: 5px 10px;
50         }
51 
52         .tb-border {
53             padding-top: 20px;
54         }
55 
56         .frame-descriptor {
57             background-color: #e2eafb;
58         }
59 
60         .frame-descriptor {
61             font-size: 14px;
62         }
63     </style>
64 '''
65 
66 TRACEBACK_WRAPPER_HTML = '''
67     <html>
68         <head>
69             {style}
70         </head>
71         <body>
72             {inner_html}
73             <div class="summary">
74                 <p>
75                 <b>{exc_name}: {exc_value}</b>
76                     while handling path <code>{path}</code>
77                 </p>
78             </div>
79         </body>
80     </html>
81 '''
82 
83 TRACEBACK_WRAPPER_INNER_HTML = '''
84     <h1>{exc_name}</h1>
85     <h3><code>{exc_value}</code></h3>
86     <div class="tb-wrapper">
87         <p class="tb-header">Traceback (most recent call last):</p>
88         {frame_html}
89     </div>
90 '''
91 
92 TRACEBACK_BORDER = '''
93     <div class="tb-border">
94         <b><i>
95             The above exception was the direct cause of the
96             following exception:
97         </i></b>
98     </div>
99 '''
100 
101 TRACEBACK_LINE_HTML = '''
102     <div class="frame-line">
103         <p class="frame-descriptor">
104             File {0.filename}, line <i>{0.lineno}</i>,
105             in <code><b>{0.name}</b></code>
106         </p>
107         <p class="frame-code"><code>{0.line}</code></p>
108     </div>
109 '''
110 
111 INTERNAL_SERVER_ERROR_HTML = '''
112     <h1>Internal Server Error</h1>
113     <p>
114         The server encountered an internal error and cannot complete
115         your request.
116     </p>
117 '''
118 
119 
120 _sanic_exceptions = {}
121 
122 
123 def add_status_code(code):
124     """
125     Decorator used for adding exceptions to _sanic_exceptions.
126     """
127     def class_decorator(cls):
128         cls.status_code = code
129         _sanic_exceptions[code] = cls
130         return cls
131     return class_decorator
132 
133 
134 class SanicException(Exception):
135 
136     def __init__(self, message, status_code=None):
137         super().__init__(message)
138 
139         if status_code is not None:
140             self.status_code = status_code
141 
142 
143 @add_status_code(404)
144 class NotFound(SanicException):
145     pass
146 
147 
148 @add_status_code(400)
149 class InvalidUsage(SanicException):
150     pass
151 
152 
153 @add_status_code(500)
154 class ServerError(SanicException):
155     pass
156 
157 
158 class URLBuildError(ServerError):
159     pass
160 
161 
162 class FileNotFound(NotFound):
163     pass
164 
165     def __init__(self, message, path, relative_url):
166         super().__init__(message)
167         self.path = path
168         self.relative_url = relative_url
169 
170 
171 @add_status_code(408)
172 class RequestTimeout(SanicException):
173     pass
174 
175 
176 @add_status_code(413)
177 class PayloadTooLarge(SanicException):
178     pass
179 
180 
181 class HeaderNotFound(InvalidUsage):
182     pass
183 
184 
185 @add_status_code(416)
186 class ContentRangeError(SanicException):
187     pass
188 
189     def __init__(self, message, content_range):
190         super().__init__(message)
191         self.headers = {
192             'Content-Type': 'text/plain',
193             "Content-Range": "bytes */%s" % (content_range.total,)
194         }
195 
196 
197 @add_status_code(403)
198 class Forbidden(SanicException):
199     pass
200 
201 
202 class InvalidRangeType(ContentRangeError):
203     pass
204 
205 
206 @add_status_code(401)
207 class Unauthorized(SanicException):
208     """
209     Unauthorized exception (401 HTTP status code).
210 
211     :param message: Message describing the exception.
212     :param scheme: Name of the authentication scheme to be used.
213 
214     When present, kwargs is used to complete the WWW-Authentication header.
215 
216     Examples::
217 
218         # With a Basic auth-scheme, realm MUST be present:
219         raise Unauthorized("Auth required.", "Basic", realm="Restricted Area")
220 
221         # With a Digest auth-scheme, things are a bit more complicated:
222         raise Unauthorized("Auth required.",
223                            "Digest",
224                            realm="Restricted Area",
225                            qop="auth, auth-int",
226                            algorithm="MD5",
227                            nonce="abcdef",
228                            opaque="zyxwvu")
229 
230         # With a Bearer auth-scheme, realm is optional so you can write:
231         raise Unauthorized("Auth required.", "Bearer")
232 
233         # or, if you want to specify the realm:
234         raise Unauthorized("Auth required.", "Bearer", realm="Restricted Area")
235     """
236     def __init__(self, message, scheme, **kwargs):
237         super().__init__(message)
238 
239         values = ["{!s}={!r}".format(k, v) for k, v in kwargs.items()]
240         challenge = ', '.join(values)
241 
242         self.headers = {
243             "WWW-Authenticate": "{} {}".format(scheme, challenge).rstrip()
244         }
245 
246 
247 def abort(status_code, message=None):
248     """
249     Raise an exception based on SanicException. Returns the HTTP response
250     message appropriate for the given status code, unless provided.
251 
252     :param status_code: The HTTP status code to return.
253     :param message: The HTTP response body. Defaults to the messages
254                     in response.py for the given status code.
255     """
256     if message is None:
257         message = COMMON_STATUS_CODES.get(status_code,
258                                           ALL_STATUS_CODES.get(status_code))
259         # These are stored as bytes in the STATUS_CODES dict
260         message = message.decode('utf8')
261     sanic_exception = _sanic_exceptions.get(status_code, SanicException)
262     raise sanic_exception(message=message, status_code=status_code)
263
```
--- END FILES ---

Please first localize the bug based on the issue statement, and then generate a patch according to the `git diff` format fenced by three backticks.

Here is an example:

```diff
diff --git a/examples/server_async.py b/examples/server_async.py
--- a/examples/server_async.py
+++ b/examples/server_async.py
@@ -313,4 +313,4 @@
 
 
 if __name__ == "__main__":
-    asyncio.run(run_async_server("."), debug=True)
+    asyncio.run(run_async_server(), debug=True)
diff --git a/examples/server_sync.py b/examples/server_sync.py
--- a/examples/server_sync.py
+++ b/examples/server_sync.py
@@ -313,5 +313,5 @@
 
 
 if __name__ == "__main__":
-    server = run_sync_server(".")
+    server = run_sync_server()
     server.shutdown()

```

diff --git a/sanic/exceptions.py b/sanic/exceptions.py
--- a/sanic/exceptions.py
+++ b/sanic/exceptions.py
@@ -209,6 +209,7 @@
     Unauthorized exception (401 HTTP status code).
 
     :param message: Message describing the exception.
+    :param status_code: HTTP Status code.
     :param scheme: Name of the authentication scheme to be used.
 
     When present, kwargs is used to complete the WWW-Authentication header.
@@ -216,11 +217,13 @@
     Examples::
 
         # With a Basic auth-scheme, realm MUST be present:
-        raise Unauthorized("Auth required.", "Basic", realm="Restricted Area")
+        raise Unauthorized("Auth required.",
+                           scheme="Basic",
+                           realm="Restricted Area")
 
         # With a Digest auth-scheme, things are a bit more complicated:
         raise Unauthorized("Auth required.",
-                           "Digest",
+                           scheme="Digest",
                            realm="Restricted Area",
                            qop="auth, auth-int",
                            algorithm="MD5",
@@ -228,20 +231,24 @@
                            opaque="zyxwvu")
 
         # With a Bearer auth-scheme, realm is optional so you can write:
-        raise Unauthorized("Auth required.", "Bearer")
+        raise Unauthorized("Auth required.", scheme="Bearer")
 
         # or, if you want to specify the realm:
-        raise Unauthorized("Auth required.", "Bearer", realm="Restricted Area")
+        raise Unauthorized("Auth required.",
+                           scheme="Bearer",
+                           realm="Restricted Area")
     """
-    def __init__(self, message, scheme, **kwargs):
-        super().__init__(message)
+    def __init__(self, message, status_code=None, scheme=None, **kwargs):
+        super().__init__(message, status_code)
 
-        values = ["{!s}={!r}".format(k, v) for k, v in kwargs.items()]
-        challenge = ', '.join(values)
+        # if auth-scheme is specified, set "WWW-Authenticate" header
+        if scheme is not None:
+            values = ["{!s}={!r}".format(k, v) for k, v in kwargs.items()]
+            challenge = ', '.join(values)
 
-        self.headers = {
-            "WWW-Authenticate": "{} {}".format(scheme, challenge).rstrip()
-        }
+            self.headers = {
+                "WWW-Authenticate": "{} {}".format(scheme, challenge).rstrip()
+            }
 
 
 def abort(status_code, message=None):

{"golden_diff": "diff --git a/sanic/exceptions.py b/sanic/exceptions.py\n--- a/sanic/exceptions.py\n+++ b/sanic/exceptions.py\n@@ -209,6 +209,7 @@\n     Unauthorized exception (401 HTTP status code).\n \n     :param message: Message describing the exception.\n+    :param status_code: HTTP Status code.\n     :param scheme: Name of the authentication scheme to be used.\n \n     When present, kwargs is used to complete the WWW-Authentication header.\n@@ -216,11 +217,13 @@\n     Examples::\n \n         # With a Basic auth-scheme, realm MUST be present:\n-        raise Unauthorized(\"Auth required.\", \"Basic\", realm=\"Restricted Area\")\n+        raise Unauthorized(\"Auth required.\",\n+                           scheme=\"Basic\",\n+                           realm=\"Restricted Area\")\n \n         # With a Digest auth-scheme, things are a bit more complicated:\n         raise Unauthorized(\"Auth required.\",\n-                           \"Digest\",\n+                           scheme=\"Digest\",\n                            realm=\"Restricted Area\",\n                            qop=\"auth, auth-int\",\n                            algorithm=\"MD5\",\n@@ -228,20 +231,24 @@\n                            opaque=\"zyxwvu\")\n \n         # With a Bearer auth-scheme, realm is optional so you can write:\n-        raise Unauthorized(\"Auth required.\", \"Bearer\")\n+        raise Unauthorized(\"Auth required.\", scheme=\"Bearer\")\n \n         # or, if you want to specify the realm:\n-        raise Unauthorized(\"Auth required.\", \"Bearer\", realm=\"Restricted Area\")\n+        raise Unauthorized(\"Auth required.\",\n+                           scheme=\"Bearer\",\n+                           realm=\"Restricted Area\")\n     \"\"\"\n-    def __init__(self, message, scheme, **kwargs):\n-        super().__init__(message)\n+    def __init__(self, message, status_code=None, scheme=None, **kwargs):\n+        super().__init__(message, status_code)\n \n-        values = [\"{!s}={!r}\".format(k, v) for k, v in kwargs.items()]\n-        challenge = ', '.join(values)\n+        # if auth-scheme is specified, set \"WWW-Authenticate\" header\n+        if scheme is not None:\n+            values = [\"{!s}={!r}\".format(k, v) for k, v in kwargs.items()]\n+            challenge = ', '.join(values)\n \n-        self.headers = {\n-            \"WWW-Authenticate\": \"{} {}\".format(scheme, challenge).rstrip()\n-        }\n+            self.headers = {\n+                \"WWW-Authenticate\": \"{} {}\".format(scheme, challenge).rstrip()\n+            }\n \n \n def abort(status_code, message=None):\n", "issue": "Sanic exceptions\nHow does sanic exceptions are supposed to work? The docs states that \r\n\r\n> Exceptions can be thrown from within request handlers and will automatically be handled by Sanic. Exceptions take a message as their first argument, and can also take a status code to be passed back in the HTTP response.\r\n\r\nThis is my route\r\n```python\r\n@app.route(\"/\")\r\nasync def test(request):\r\n    abort(401)\r\n```\r\n\r\nWhen I make a request on the path I get a response of : \r\n\r\n>Internal Server Error\r\nThe server encountered an internal error and cannot complete your request.\r\n\r\n`2017-08-24 10:18:43 - (sanic)[ERROR]: Traceback (most recent call last):\r\n  File \"/home/nikos/.virtualenvs/3.6/lib/python3.6/site-packages/sanic/app.py\", line 503, in handle_request\r\n    response = await response\r\n  File \"/home/nikos/Desktop/Side Projects/micro/test2.py\", line 15, in test\r\n    abort(401)\r\n  File \"/home/nikos/.virtualenvs/3.6/lib/python3.6/site-packages/sanic/exceptions.py\", line 262, in abort\r\n    raise sanic_exception(message=message, status_code=status_code)\r\nTypeError: __init__() missing 1 required positional argument: 'scheme'`\r\n\r\nAlso after a bit the connection times out and the log trace is \r\n\r\n`2017-08-24 10:18:43 - (network)[INFO][127.0.0.1:34734]: GET http://0.0.0.0:8001/  500 144\r\n2017-08-24 10:19:43 - (sanic)[ERROR]: Traceback (most recent call last):\r\n  File \"/home/nikos/.virtualenvs/3.6/lib/python3.6/site-packages/sanic/server.py\", line 143, in connection_timeout\r\n    raise RequestTimeout('Request Timeout')\r\nsanic.exceptions.RequestTimeout: Request Timeout`\n", "before_files": [{"content": "from sanic.response import ALL_STATUS_CODES, COMMON_STATUS_CODES\n\nTRACEBACK_STYLE = '''\n    <style>\n        body {\n            padding: 20px;\n            font-family: Arial, sans-serif;\n        }\n\n        p {\n            margin: 0;\n        }\n\n        .summary {\n            padding: 10px;\n        }\n\n        h1 {\n            margin-bottom: 0;\n        }\n\n        h3 {\n            margin-top: 10px;\n        }\n\n        h3 code {\n            font-size: 24px;\n        }\n\n        .frame-line > * {\n            padding: 5px 10px;\n        }\n\n        .frame-line {\n            margin-bottom: 5px;\n        }\n\n        .frame-code {\n            font-size: 16px;\n            padding-left: 30px;\n        }\n\n        .tb-wrapper {\n            border: 1px solid #f3f3f3;\n        }\n\n        .tb-header {\n            background-color: #f3f3f3;\n            padding: 5px 10px;\n        }\n\n        .tb-border {\n            padding-top: 20px;\n        }\n\n        .frame-descriptor {\n            background-color: #e2eafb;\n        }\n\n        .frame-descriptor {\n            font-size: 14px;\n        }\n    </style>\n'''\n\nTRACEBACK_WRAPPER_HTML = '''\n    <html>\n        <head>\n            {style}\n        </head>\n        <body>\n            {inner_html}\n            <div class=\"summary\">\n                <p>\n                <b>{exc_name}: {exc_value}</b>\n                    while handling path <code>{path}</code>\n                </p>\n            </div>\n        </body>\n    </html>\n'''\n\nTRACEBACK_WRAPPER_INNER_HTML = '''\n    <h1>{exc_name}</h1>\n    <h3><code>{exc_value}</code></h3>\n    <div class=\"tb-wrapper\">\n        <p class=\"tb-header\">Traceback (most recent call last):</p>\n        {frame_html}\n    </div>\n'''\n\nTRACEBACK_BORDER = '''\n    <div class=\"tb-border\">\n        <b><i>\n            The above exception was the direct cause of the\n            following exception:\n        </i></b>\n    </div>\n'''\n\nTRACEBACK_LINE_HTML = '''\n    <div class=\"frame-line\">\n        <p class=\"frame-descriptor\">\n            File {0.filename}, line <i>{0.lineno}</i>,\n            in <code><b>{0.name}</b></code>\n        </p>\n        <p class=\"frame-code\"><code>{0.line}</code></p>\n    </div>\n'''\n\nINTERNAL_SERVER_ERROR_HTML = '''\n    <h1>Internal Server Error</h1>\n    <p>\n        The server encountered an internal error and cannot complete\n        your request.\n    </p>\n'''\n\n\n_sanic_exceptions = {}\n\n\ndef add_status_code(code):\n    \"\"\"\n    Decorator used for adding exceptions to _sanic_exceptions.\n    \"\"\"\n    def class_decorator(cls):\n        cls.status_code = code\n        _sanic_exceptions[code] = cls\n        return cls\n    return class_decorator\n\n\nclass SanicException(Exception):\n\n    def __init__(self, message, status_code=None):\n        super().__init__(message)\n\n        if status_code is not None:\n            self.status_code = status_code\n\n\n@add_status_code(404)\nclass NotFound(SanicException):\n    pass\n\n\n@add_status_code(400)\nclass InvalidUsage(SanicException):\n    pass\n\n\n@add_status_code(500)\nclass ServerError(SanicException):\n    pass\n\n\nclass URLBuildError(ServerError):\n    pass\n\n\nclass FileNotFound(NotFound):\n    pass\n\n    def __init__(self, message, path, relative_url):\n        super().__init__(message)\n        self.path = path\n        self.relative_url = relative_url\n\n\n@add_status_code(408)\nclass RequestTimeout(SanicException):\n    pass\n\n\n@add_status_code(413)\nclass PayloadTooLarge(SanicException):\n    pass\n\n\nclass HeaderNotFound(InvalidUsage):\n    pass\n\n\n@add_status_code(416)\nclass ContentRangeError(SanicException):\n    pass\n\n    def __init__(self, message, content_range):\n        super().__init__(message)\n        self.headers = {\n            'Content-Type': 'text/plain',\n            \"Content-Range\": \"bytes */%s\" % (content_range.total,)\n        }\n\n\n@add_status_code(403)\nclass Forbidden(SanicException):\n    pass\n\n\nclass InvalidRangeType(ContentRangeError):\n    pass\n\n\n@add_status_code(401)\nclass Unauthorized(SanicException):\n    \"\"\"\n    Unauthorized exception (401 HTTP status code).\n\n    :param message: Message describing the exception.\n    :param scheme: Name of the authentication scheme to be used.\n\n    When present, kwargs is used to complete the WWW-Authentication header.\n\n    Examples::\n\n        # With a Basic auth-scheme, realm MUST be present:\n        raise Unauthorized(\"Auth required.\", \"Basic\", realm=\"Restricted Area\")\n\n        # With a Digest auth-scheme, things are a bit more complicated:\n        raise Unauthorized(\"Auth required.\",\n                           \"Digest\",\n                           realm=\"Restricted Area\",\n                           qop=\"auth, auth-int\",\n                           algorithm=\"MD5\",\n                           nonce=\"abcdef\",\n                           opaque=\"zyxwvu\")\n\n        # With a Bearer auth-scheme, realm is optional so you can write:\n        raise Unauthorized(\"Auth required.\", \"Bearer\")\n\n        # or, if you want to specify the realm:\n        raise Unauthorized(\"Auth required.\", \"Bearer\", realm=\"Restricted Area\")\n    \"\"\"\n    def __init__(self, message, scheme, **kwargs):\n        super().__init__(message)\n\n        values = [\"{!s}={!r}\".format(k, v) for k, v in kwargs.items()]\n        challenge = ', '.join(values)\n\n        self.headers = {\n            \"WWW-Authenticate\": \"{} {}\".format(scheme, challenge).rstrip()\n        }\n\n\ndef abort(status_code, message=None):\n    \"\"\"\n    Raise an exception based on SanicException. Returns the HTTP response\n    message appropriate for the given status code, unless provided.\n\n    :param status_code: The HTTP status code to return.\n    :param message: The HTTP response body. Defaults to the messages\n                    in response.py for the given status code.\n    \"\"\"\n    if message is None:\n        message = COMMON_STATUS_CODES.get(status_code,\n                                          ALL_STATUS_CODES.get(status_code))\n        # These are stored as bytes in the STATUS_CODES dict\n        message = message.decode('utf8')\n    sanic_exception = _sanic_exceptions.get(status_code, SanicException)\n    raise sanic_exception(message=message, status_code=status_code)\n", "path": "sanic/exceptions.py"}], "after_files": [{"content": "from sanic.response import ALL_STATUS_CODES, COMMON_STATUS_CODES\n\nTRACEBACK_STYLE = '''\n    <style>\n        body {\n            padding: 20px;\n            font-family: Arial, sans-serif;\n        }\n\n        p {\n            margin: 0;\n        }\n\n        .summary {\n            padding: 10px;\n        }\n\n        h1 {\n            margin-bottom: 0;\n        }\n\n        h3 {\n            margin-top: 10px;\n        }\n\n        h3 code {\n            font-size: 24px;\n        }\n\n        .frame-line > * {\n            padding: 5px 10px;\n        }\n\n        .frame-line {\n            margin-bottom: 5px;\n        }\n\n        .frame-code {\n            font-size: 16px;\n            padding-left: 30px;\n        }\n\n        .tb-wrapper {\n            border: 1px solid #f3f3f3;\n        }\n\n        .tb-header {\n            background-color: #f3f3f3;\n            padding: 5px 10px;\n        }\n\n        .tb-border {\n            padding-top: 20px;\n        }\n\n        .frame-descriptor {\n            background-color: #e2eafb;\n        }\n\n        .frame-descriptor {\n            font-size: 14px;\n        }\n    </style>\n'''\n\nTRACEBACK_WRAPPER_HTML = '''\n    <html>\n        <head>\n            {style}\n        </head>\n        <body>\n            {inner_html}\n            <div class=\"summary\">\n                <p>\n                <b>{exc_name}: {exc_value}</b>\n                    while handling path <code>{path}</code>\n                </p>\n            </div>\n        </body>\n    </html>\n'''\n\nTRACEBACK_WRAPPER_INNER_HTML = '''\n    <h1>{exc_name}</h1>\n    <h3><code>{exc_value}</code></h3>\n    <div class=\"tb-wrapper\">\n        <p class=\"tb-header\">Traceback (most recent call last):</p>\n        {frame_html}\n    </div>\n'''\n\nTRACEBACK_BORDER = '''\n    <div class=\"tb-border\">\n        <b><i>\n            The above exception was the direct cause of the\n            following exception:\n        </i></b>\n    </div>\n'''\n\nTRACEBACK_LINE_HTML = '''\n    <div class=\"frame-line\">\n        <p class=\"frame-descriptor\">\n            File {0.filename}, line <i>{0.lineno}</i>,\n            in <code><b>{0.name}</b></code>\n        </p>\n        <p class=\"frame-code\"><code>{0.line}</code></p>\n    </div>\n'''\n\nINTERNAL_SERVER_ERROR_HTML = '''\n    <h1>Internal Server Error</h1>\n    <p>\n        The server encountered an internal error and cannot complete\n        your request.\n    </p>\n'''\n\n\n_sanic_exceptions = {}\n\n\ndef add_status_code(code):\n    \"\"\"\n    Decorator used for adding exceptions to _sanic_exceptions.\n    \"\"\"\n    def class_decorator(cls):\n        cls.status_code = code\n        _sanic_exceptions[code] = cls\n        return cls\n    return class_decorator\n\n\nclass SanicException(Exception):\n\n    def __init__(self, message, status_code=None):\n        super().__init__(message)\n\n        if status_code is not None:\n            self.status_code = status_code\n\n\n@add_status_code(404)\nclass NotFound(SanicException):\n    pass\n\n\n@add_status_code(400)\nclass InvalidUsage(SanicException):\n    pass\n\n\n@add_status_code(500)\nclass ServerError(SanicException):\n    pass\n\n\nclass URLBuildError(ServerError):\n    pass\n\n\nclass FileNotFound(NotFound):\n    pass\n\n    def __init__(self, message, path, relative_url):\n        super().__init__(message)\n        self.path = path\n        self.relative_url = relative_url\n\n\n@add_status_code(408)\nclass RequestTimeout(SanicException):\n    pass\n\n\n@add_status_code(413)\nclass PayloadTooLarge(SanicException):\n    pass\n\n\nclass HeaderNotFound(InvalidUsage):\n    pass\n\n\n@add_status_code(416)\nclass ContentRangeError(SanicException):\n    pass\n\n    def __init__(self, message, content_range):\n        super().__init__(message)\n        self.headers = {\n            'Content-Type': 'text/plain',\n            \"Content-Range\": \"bytes */%s\" % (content_range.total,)\n        }\n\n\n@add_status_code(403)\nclass Forbidden(SanicException):\n    pass\n\n\nclass InvalidRangeType(ContentRangeError):\n    pass\n\n\n@add_status_code(401)\nclass Unauthorized(SanicException):\n    \"\"\"\n    Unauthorized exception (401 HTTP status code).\n\n    :param message: Message describing the exception.\n    :param status_code: HTTP Status code.\n    :param scheme: Name of the authentication scheme to be used.\n\n    When present, kwargs is used to complete the WWW-Authentication header.\n\n    Examples::\n\n        # With a Basic auth-scheme, realm MUST be present:\n        raise Unauthorized(\"Auth required.\",\n                           scheme=\"Basic\",\n                           realm=\"Restricted Area\")\n\n        # With a Digest auth-scheme, things are a bit more complicated:\n        raise Unauthorized(\"Auth required.\",\n                           scheme=\"Digest\",\n                           realm=\"Restricted Area\",\n                           qop=\"auth, auth-int\",\n                           algorithm=\"MD5\",\n                           nonce=\"abcdef\",\n                           opaque=\"zyxwvu\")\n\n        # With a Bearer auth-scheme, realm is optional so you can write:\n        raise Unauthorized(\"Auth required.\", scheme=\"Bearer\")\n\n        # or, if you want to specify the realm:\n        raise Unauthorized(\"Auth required.\",\n                           scheme=\"Bearer\",\n                           realm=\"Restricted Area\")\n    \"\"\"\n    def __init__(self, message, status_code=None, scheme=None, **kwargs):\n        super().__init__(message, status_code)\n\n        # if auth-scheme is specified, set \"WWW-Authenticate\" header\n        if scheme is not None:\n            values = [\"{!s}={!r}\".format(k, v) for k, v in kwargs.items()]\n            challenge = ', '.join(values)\n\n            self.headers = {\n                \"WWW-Authenticate\": \"{} {}\".format(scheme, challenge).rstrip()\n            }\n\n\ndef abort(status_code, message=None):\n    \"\"\"\n    Raise an exception based on SanicException. Returns the HTTP response\n    message appropriate for the given status code, unless provided.\n\n    :param status_code: The HTTP status code to return.\n    :param message: The HTTP response body. Defaults to the messages\n                    in response.py for the given status code.\n    \"\"\"\n    if message is None:\n        message = COMMON_STATUS_CODES.get(status_code,\n                                          ALL_STATUS_CODES.get(status_code))\n        # These are stored as bytes in the STATUS_CODES dict\n        message = message.decode('utf8')\n    sanic_exception = _sanic_exceptions.get(status_code, SanicException)\n    raise sanic_exception(message=message, status_code=status_code)\n", "path": "sanic/exceptions.py"}]}