An exception that will turn into an HTTP error response. Raising an `HTTPError` is a convenient alternative to calling `RequestHandler.send_error` since it automatically ends the current function. To customize the response sent with an `HTTPError`, override `RequestHandler.writ
| 2366 | |
| 2367 | |
| 2368 | class HTTPError(Exception): |
| 2369 | """An exception that will turn into an HTTP error response. |
| 2370 | |
| 2371 | Raising an `HTTPError` is a convenient alternative to calling |
| 2372 | `RequestHandler.send_error` since it automatically ends the |
| 2373 | current function. |
| 2374 | |
| 2375 | To customize the response sent with an `HTTPError`, override |
| 2376 | `RequestHandler.write_error`. |
| 2377 | |
| 2378 | :arg int status_code: HTTP status code. Must be listed in |
| 2379 | `httplib.responses <http.client.responses>` unless the ``reason`` |
| 2380 | keyword argument is given. |
| 2381 | :arg str log_message: Message to be written to the log for this error |
| 2382 | (will not be shown to the user unless the `Application` is in debug |
| 2383 | mode). May contain ``%s``-style placeholders, which will be filled |
| 2384 | in with remaining positional parameters. |
| 2385 | :arg str reason: Keyword-only argument. The HTTP "reason" phrase |
| 2386 | to pass in the status line along with ``status_code``. Normally |
| 2387 | determined automatically from ``status_code``, but can be used |
| 2388 | to use a non-standard numeric code. |
| 2389 | """ |
| 2390 | |
| 2391 | def __init__( |
| 2392 | self, |
| 2393 | status_code: int = 500, |
| 2394 | log_message: Optional[str] = None, |
| 2395 | *args: Any, |
| 2396 | **kwargs: Any |
| 2397 | ) -> None: |
| 2398 | self.status_code = status_code |
| 2399 | self.log_message = log_message |
| 2400 | self.args = args |
| 2401 | self.reason = kwargs.get("reason", None) |
| 2402 | if log_message and not args: |
| 2403 | self.log_message = log_message.replace("%", "%%") |
| 2404 | |
| 2405 | def __str__(self) -> str: |
| 2406 | message = "HTTP %d: %s" % ( |
| 2407 | self.status_code, |
| 2408 | self.reason or httputil.responses.get(self.status_code, "Unknown"), |
| 2409 | ) |
| 2410 | if self.log_message: |
| 2411 | return message + " (" + (self.log_message % self.args) + ")" |
| 2412 | else: |
| 2413 | return message |
| 2414 | |
| 2415 | |
| 2416 | class Finish(Exception): |
no outgoing calls