Handle errors. Returns HTTP response with specific status code. Logs additional information. It always closes current connection.
(self, status=500, message=None,
payload=None, exc=None, headers=None, reason=None)
| 316 | return |
| 317 | |
| 318 | def handle_error(self, status=500, message=None, |
| 319 | payload=None, exc=None, headers=None, reason=None): |
| 320 | """Handle errors. |
| 321 | |
| 322 | Returns HTTP response with specific status code. Logs additional |
| 323 | information. It always closes current connection.""" |
| 324 | now = self._loop.time() |
| 325 | try: |
| 326 | if self._request_handler is None: |
| 327 | # client has been disconnected during writing. |
| 328 | return () |
| 329 | |
| 330 | if status == 500: |
| 331 | self.log_exception("Error handling request") |
| 332 | |
| 333 | try: |
| 334 | if reason is None or reason == '': |
| 335 | reason, msg = RESPONSES[status] |
| 336 | else: |
| 337 | msg = reason |
| 338 | except KeyError: |
| 339 | status = 500 |
| 340 | reason, msg = '???', '' |
| 341 | |
| 342 | if self.debug and exc is not None: |
| 343 | try: |
| 344 | tb = traceback.format_exc() |
| 345 | tb = html_escape(tb) |
| 346 | msg += '<br><h2>Traceback:</h2>\n<pre>{}</pre>'.format(tb) |
| 347 | except: |
| 348 | pass |
| 349 | |
| 350 | html = DEFAULT_ERROR_MESSAGE.format( |
| 351 | status=status, reason=reason, message=msg).encode('utf-8') |
| 352 | |
| 353 | response = aiohttp.Response(self.writer, status, close=True) |
| 354 | response.add_header(hdrs.CONTENT_TYPE, 'text/html; charset=utf-8') |
| 355 | response.add_header(hdrs.CONTENT_LENGTH, str(len(html))) |
| 356 | if headers is not None: |
| 357 | for name, value in headers: |
| 358 | response.add_header(name, value) |
| 359 | response.send_headers() |
| 360 | |
| 361 | response.write(html) |
| 362 | # disable CORK, enable NODELAY if needed |
| 363 | self.writer.set_tcp_nodelay(True) |
| 364 | drain = response.write_eof() |
| 365 | |
| 366 | self.log_access(message, None, response, self._loop.time() - now) |
| 367 | return drain |
| 368 | finally: |
| 369 | self.keep_alive(False) |
| 370 | |
| 371 | def handle_request(self, message, payload): |
| 372 | """Handle a single HTTP request. |