Sends the given HTTP error code to the browser. If `flush()` has already been called, it is not possible to send an error, so this method will simply terminate the response. If output has been written but not yet flushed, it will be discarded and replaced with the er
(self, status_code: int = 500, **kwargs: Any)
| 1345 | self.ui = None # type: ignore |
| 1346 | |
| 1347 | def send_error(self, status_code: int = 500, **kwargs: Any) -> None: |
| 1348 | """Sends the given HTTP error code to the browser. |
| 1349 | |
| 1350 | If `flush()` has already been called, it is not possible to send |
| 1351 | an error, so this method will simply terminate the response. |
| 1352 | If output has been written but not yet flushed, it will be discarded |
| 1353 | and replaced with the error page. |
| 1354 | |
| 1355 | Override `write_error()` to customize the error page that is returned. |
| 1356 | Additional keyword arguments are passed through to `write_error`. |
| 1357 | """ |
| 1358 | if self._headers_written: |
| 1359 | gen_log.error("Cannot send error response after headers written") |
| 1360 | if not self._finished: |
| 1361 | # If we get an error between writing headers and finishing, |
| 1362 | # we are unlikely to be able to finish due to a |
| 1363 | # Content-Length mismatch. Try anyway to release the |
| 1364 | # socket. |
| 1365 | try: |
| 1366 | self.finish() |
| 1367 | except Exception: |
| 1368 | gen_log.error("Failed to flush partial response", exc_info=True) |
| 1369 | return |
| 1370 | self.clear() |
| 1371 | |
| 1372 | reason = kwargs.get("reason") |
| 1373 | if "exc_info" in kwargs: |
| 1374 | exception = kwargs["exc_info"][1] |
| 1375 | if isinstance(exception, HTTPError) and exception.reason: |
| 1376 | reason = exception.reason |
| 1377 | self.set_status(status_code, reason=reason) |
| 1378 | try: |
| 1379 | if status_code != 304: |
| 1380 | self.write_error(status_code, **kwargs) |
| 1381 | except Exception: |
| 1382 | app_log.error("Uncaught exception in write_error", exc_info=True) |
| 1383 | if not self._finished: |
| 1384 | self.finish() |
| 1385 | |
| 1386 | def write_error(self, status_code: int, **kwargs: Any) -> None: |
| 1387 | """Override to implement custom error pages. |