Generic exception that will generate an HTTP response when raised in the context of a request lifecycle. Usually, it is best practice to use one of the more specific exceptions than this generic one. Even when trying to raise a 500, it is generally preferable to use `ServerError`.
| 18 | |
| 19 | |
| 20 | class SanicException(Exception): |
| 21 | """Generic exception that will generate an HTTP response when raised in the context of a request lifecycle. |
| 22 | |
| 23 | Usually, it is best practice to use one of the more specific exceptions |
| 24 | than this generic one. Even when trying to raise a 500, it is generally |
| 25 | preferable to use `ServerError`. |
| 26 | |
| 27 | Args: |
| 28 | message (Optional[Union[str, bytes]], optional): The message to be sent to the client. If `None`, |
| 29 | then the appropriate HTTP response status message will be used instead. Defaults to `None`. |
| 30 | status_code (Optional[int], optional): The HTTP response code to send, if applicable. If `None`, |
| 31 | then it will be 500. Defaults to `None`. |
| 32 | quiet (Optional[bool], optional): When `True`, the error traceback will be suppressed from the logs. |
| 33 | Defaults to `None`. |
| 34 | context (Optional[Dict[str, Any]], optional): Additional mapping of key/value data that will be |
| 35 | sent to the client upon exception. Defaults to `None`. |
| 36 | extra (Optional[Dict[str, Any]], optional): Additional mapping of key/value data that will NOT be |
| 37 | sent to the client when in PRODUCTION mode. Defaults to `None`. |
| 38 | headers (Optional[Dict[str, Any]], optional): Additional headers that should be sent with the HTTP |
| 39 | response. Defaults to `None`. |
| 40 | |
| 41 | Examples: |
| 42 | ```python |
| 43 | raise SanicException( |
| 44 | "Something went wrong", |
| 45 | status_code=999, |
| 46 | context={ |
| 47 | "info": "Some additional details to send to the client", |
| 48 | }, |
| 49 | headers={ |
| 50 | "X-Foo": "bar" |
| 51 | } |
| 52 | ) |
| 53 | ``` |
| 54 | """ # noqa: E501 |
| 55 | |
| 56 | status_code: int = 500 |
| 57 | quiet: bool | None = False |
| 58 | headers: dict[str, str] = {} |
| 59 | message: str = "" |
| 60 | |
| 61 | def __init__( |
| 62 | self, |
| 63 | message: str | bytes | None = None, |
| 64 | status_code: int | None = None, |
| 65 | *, |
| 66 | quiet: bool | None = None, |
| 67 | context: dict[str, Any] | None = None, |
| 68 | extra: dict[str, Any] | None = None, |
| 69 | headers: dict[str, str] | None = None, |
| 70 | ) -> None: |
| 71 | self.context = context |
| 72 | self.extra = extra |
| 73 | status_code = status_code or getattr( |
| 74 | self.__class__, "status_code", None |
| 75 | ) |
| 76 | quiet = ( |
| 77 | quiet |
no outgoing calls
searching dependent graphs…