Table of Contents
BadRequestsanic.exceptionsBadURLsanic.exceptionsContentRangeErrorsanic.exceptionsExpectationFailedsanic.exceptionsFileNotFoundsanic.exceptionsForbiddensanic.exceptionsHTTPExceptionsanic.exceptionsHeaderExpectationFailedsanic.exceptionsHeaderNotFoundsanic.exceptionsInternalServerErrorsanic.exceptionsInvalidHeadersanic.exceptionsInvalidRangeTypesanic.exceptionsInvalidSignalsanic.exceptionsInvalidUsagesanic.exceptionsLoadFileExceptionsanic.exceptionsMethodNotAllowedsanic.exceptionsMethodNotSupportedsanic.exceptionsNotFoundsanic.exceptionsPayloadTooLargesanic.exceptionsPyFileErrorsanic.exceptionsRangeNotSatisfiablesanic.exceptionsRequestCancelledsanic.exceptionsRequestTimeoutsanic.exceptionsSanicExceptionsanic.exceptionsServerErrorsanic.exceptionsServerKilledsanic.exceptionsServiceUnavailablesanic.exceptionsURLBuildErrorsanic.exceptionsUnauthorizedsanic.exceptionsWebsocketClosedsanic.exceptionssanic.exceptions.BadRequest#
400 Bad Request
class BadRequest(message: typing.Union[str, bytes, NoneType] = None, quiet: typing.Optional[bool] = None, context: typing.Optional[typing.Dict[str, typing.Any]] = None, extra: typing.Optional[typing.Dict[str, typing.Any]] = None, headers: typing.Optional[typing.Dict[str, typing.Any]] = None): -> None
Parameters
- message
Optional[Union[str, bytes]] The message to be sent to the client. If
None
then the HTTP status 'Bad Request' will be sent. Defaults toNone
.
- quiet
Optional[bool] When
True
, the error traceback will be suppressed from the logs. Defaults toNone
.
- context
Optional[Dict[str, Any]] Additional mapping of key/value data that will be sent to the client upon exception. Defaults to
None
.
- extra
Optional[Dict[str, Any]] Additional mapping of key/value data that will NOT be sent to the client when in PRODUCTION mode. Defaults to
None
.
- headers
Optional[Dict[str, Any]] Additional headers that should be sent with the HTTP response. Defaults to
None
.
sanic.exceptions.BadURL#
400 Bad Request
class BadURL(message: typing.Union[str, bytes, NoneType] = None, quiet: typing.Optional[bool] = None, context: typing.Optional[typing.Dict[str, typing.Any]] = None, extra: typing.Optional[typing.Dict[str, typing.Any]] = None, headers: typing.Optional[typing.Dict[str, typing.Any]] = None): -> None
Parameters
- message
Optional[Union[str, bytes]] The message to be sent to the client. If
None
then the HTTP status 'Bad Request' will be sent. Defaults toNone
.
- quiet
Optional[bool] When
True
, the error traceback will be suppressed from the logs. Defaults toNone
.
- context
Optional[Dict[str, Any]] Additional mapping of key/value data that will be sent to the client upon exception. Defaults to
None
.
- extra
Optional[Dict[str, Any]] Additional mapping of key/value data that will NOT be sent to the client when in PRODUCTION mode. Defaults to
None
.
- headers
Optional[Dict[str, Any]] Additional headers that should be sent with the HTTP response. Defaults to
None
.
sanic.exceptions.ContentRangeError#
416 Range Not Satisfiable
class ContentRangeError(message: typing.Union[str, bytes, NoneType] = None, content_range: typing.Optional[sanic.models.protocol_types.Range] = None, quiet: typing.Optional[bool] = None, context: typing.Optional[typing.Dict[str, typing.Any]] = None, extra: typing.Optional[typing.Dict[str, typing.Any]] = None, headers: typing.Optional[typing.Dict[str, typing.Any]] = None)
Parameters
- message
Optional[Union[str, bytes]] The message to be sent to the client. If
None
then the HTTP status 'Range Not Satisfiable' will be sent. Defaults toNone
.
- content_range
Optional[ContentRange] An object meeting the :class:
.ContentRange
protocol that has atotal
property. Defaults toNone
.
- quiet
Optional[bool] When
True
, the error traceback will be suppressed from the logs. Defaults toNone
.
- context
Optional[Dict[str, Any]] Additional mapping of key/value data that will be sent to the client upon exception. Defaults to
None
.
- extra
Optional[Dict[str, Any]] Additional mapping of key/value data that will NOT be sent to the client when in PRODUCTION mode. Defaults to
None
.
- headers
Optional[Dict[str, Any]] Additional headers that should be sent with the HTTP response. Defaults to
None
.
sanic.exceptions.ExpectationFailed#
417 Expectation Failed
class ExpectationFailed(message: typing.Union[str, bytes, NoneType] = None, quiet: typing.Optional[bool] = None, context: typing.Optional[typing.Dict[str, typing.Any]] = None, extra: typing.Optional[typing.Dict[str, typing.Any]] = None, headers: typing.Optional[typing.Dict[str, typing.Any]] = None): -> None
Parameters
- message
Optional[Union[str, bytes]] The message to be sent to the client. If
None
then the HTTP status 'Bad Request' will be sent. Defaults toNone
.
- quiet
Optional[bool] When
True
, the error traceback will be suppressed from the logs. Defaults toNone
.
- context
Optional[Dict[str, Any]] Additional mapping of key/value data that will be sent to the client upon exception. Defaults to
None
.
- extra
Optional[Dict[str, Any]] Additional mapping of key/value data that will NOT be sent to the client when in PRODUCTION mode. Defaults to
None
.
- headers
Optional[Dict[str, Any]] Additional headers that should be sent with the HTTP response. Defaults to
None
.
sanic.exceptions.FileNotFound#
404 Not Found
class FileNotFound(message: typing.Union[str, bytes, NoneType] = None, path: typing.Optional[os.PathLike] = None, relative_url: typing.Optional[str] = None, quiet: typing.Optional[bool] = None, context: typing.Optional[typing.Dict[str, typing.Any]] = None, extra: typing.Optional[typing.Dict[str, typing.Any]] = None, headers: typing.Optional[typing.Dict[str, typing.Any]] = None)
A specific form of :class:.NotFound
that is specifically when looking
for a file on the file system at a known path.
Parameters
- message
Optional[Union[str, bytes]] The message to be sent to the client. If
None
then the HTTP status 'Not Found' will be sent. Defaults toNone
.
- path
Optional[PathLike] The path, if any, to the file that could not be found. Defaults to
None
.
- relative_url
Optional[str] A relative URL of the file. Defaults to
None
.
- quiet
Optional[bool] When
True
, the error traceback will be suppressed from the logs. Defaults toNone
.
- context
Optional[Dict[str, Any]] Additional mapping of key/value data that will be sent to the client upon exception. Defaults to
None
.
- extra
Optional[Dict[str, Any]] Additional mapping of key/value data that will NOT be sent to the client when in PRODUCTION mode. Defaults to
None
.
- headers
Optional[Dict[str, Any]] Additional headers that should be sent with the HTTP response. Defaults to
None
.
sanic.exceptions.Forbidden#
403 Forbidden
class Forbidden(message: typing.Union[str, bytes, NoneType] = None, quiet: typing.Optional[bool] = None, context: typing.Optional[typing.Dict[str, typing.Any]] = None, extra: typing.Optional[typing.Dict[str, typing.Any]] = None, headers: typing.Optional[typing.Dict[str, typing.Any]] = None): -> None
Parameters
- message
Optional[Union[str, bytes]] The message to be sent to the client. If
None
then the HTTP status 'Bad Request' will be sent. Defaults toNone
.
- quiet
Optional[bool] When
True
, the error traceback will be suppressed from the logs. Defaults toNone
.
- context
Optional[Dict[str, Any]] Additional mapping of key/value data that will be sent to the client upon exception. Defaults to
None
.
- extra
Optional[Dict[str, Any]] Additional mapping of key/value data that will NOT be sent to the client when in PRODUCTION mode. Defaults to
None
.
- headers
Optional[Dict[str, Any]] Additional headers that should be sent with the HTTP response. Defaults to
None
.
sanic.exceptions.HTTPException#
A base class for other exceptions and should not be called directly.
class HTTPException(message: typing.Union[str, bytes, NoneType] = None, quiet: typing.Optional[bool] = None, context: typing.Optional[typing.Dict[str, typing.Any]] = None, extra: typing.Optional[typing.Dict[str, typing.Any]] = None, headers: typing.Optional[typing.Dict[str, typing.Any]] = None): -> None
sanic.exceptions.HeaderExpectationFailed#
417 Expectation Failed
class HeaderExpectationFailed(message: typing.Union[str, bytes, NoneType] = None, quiet: typing.Optional[bool] = None, context: typing.Optional[typing.Dict[str, typing.Any]] = None, extra: typing.Optional[typing.Dict[str, typing.Any]] = None, headers: typing.Optional[typing.Dict[str, typing.Any]] = None): -> None
Parameters
- message
Optional[Union[str, bytes]] The message to be sent to the client. If
None
then the HTTP status 'Bad Request' will be sent. Defaults toNone
.
- quiet
Optional[bool] When
True
, the error traceback will be suppressed from the logs. Defaults toNone
.
- context
Optional[Dict[str, Any]] Additional mapping of key/value data that will be sent to the client upon exception. Defaults to
None
.
- extra
Optional[Dict[str, Any]] Additional mapping of key/value data that will NOT be sent to the client when in PRODUCTION mode. Defaults to
None
.
- headers
Optional[Dict[str, Any]] Additional headers that should be sent with the HTTP response. Defaults to
None
.
sanic.exceptions.HeaderNotFound#
400 Bad Request
class HeaderNotFound(message: typing.Union[str, bytes, NoneType] = None, quiet: typing.Optional[bool] = None, context: typing.Optional[typing.Dict[str, typing.Any]] = None, extra: typing.Optional[typing.Dict[str, typing.Any]] = None, headers: typing.Optional[typing.Dict[str, typing.Any]] = None): -> None
Parameters
- message
Optional[Union[str, bytes]] The message to be sent to the client. If
None
then the HTTP status 'Bad Request' will be sent. Defaults toNone
.
- quiet
Optional[bool] When
True
, the error traceback will be suppressed from the logs. Defaults toNone
.
- context
Optional[Dict[str, Any]] Additional mapping of key/value data that will be sent to the client upon exception. Defaults to
None
.
- extra
Optional[Dict[str, Any]] Additional mapping of key/value data that will NOT be sent to the client when in PRODUCTION mode. Defaults to
None
.
- headers
Optional[Dict[str, Any]] Additional headers that should be sent with the HTTP response. Defaults to
None
.
sanic.exceptions.InternalServerError#
500 Internal Server Error
class InternalServerError(message: typing.Union[str, bytes, NoneType] = None, quiet: typing.Optional[bool] = None, context: typing.Optional[typing.Dict[str, typing.Any]] = None, extra: typing.Optional[typing.Dict[str, typing.Any]] = None, headers: typing.Optional[typing.Dict[str, typing.Any]] = None): -> None
A general server-side error has occurred. If no other HTTP exception is appropriate, then this should be used
Parameters
- message
Optional[Union[str, bytes]] The message to be sent to the client. If
None
then the HTTP status 'Bad Request' will be sent. Defaults toNone
.
- quiet
Optional[bool] When
True
, the error traceback will be suppressed from the logs. Defaults toNone
.
- context
Optional[Dict[str, Any]] Additional mapping of key/value data that will be sent to the client upon exception. Defaults to
None
.
- extra
Optional[Dict[str, Any]] Additional mapping of key/value data that will NOT be sent to the client when in PRODUCTION mode. Defaults to
None
.
- headers
Optional[Dict[str, Any]] Additional headers that should be sent with the HTTP response. Defaults to
None
.
sanic.exceptions.InvalidHeader#
400 Bad Request
class InvalidHeader(message: typing.Union[str, bytes, NoneType] = None, quiet: typing.Optional[bool] = None, context: typing.Optional[typing.Dict[str, typing.Any]] = None, extra: typing.Optional[typing.Dict[str, typing.Any]] = None, headers: typing.Optional[typing.Dict[str, typing.Any]] = None): -> None
Parameters
- message
Optional[Union[str, bytes]] The message to be sent to the client. If
None
then the HTTP status 'Bad Request' will be sent. Defaults toNone
.
- quiet
Optional[bool] When
True
, the error traceback will be suppressed from the logs. Defaults toNone
.
- context
Optional[Dict[str, Any]] Additional mapping of key/value data that will be sent to the client upon exception. Defaults to
None
.
- extra
Optional[Dict[str, Any]] Additional mapping of key/value data that will NOT be sent to the client when in PRODUCTION mode. Defaults to
None
.
- headers
Optional[Dict[str, Any]] Additional headers that should be sent with the HTTP response. Defaults to
None
.
sanic.exceptions.InvalidRangeType#
416 Range Not Satisfiable
class InvalidRangeType(message: typing.Union[str, bytes, NoneType] = None, content_range: typing.Optional[sanic.models.protocol_types.Range] = None, quiet: typing.Optional[bool] = None, context: typing.Optional[typing.Dict[str, typing.Any]] = None, extra: typing.Optional[typing.Dict[str, typing.Any]] = None, headers: typing.Optional[typing.Dict[str, typing.Any]] = None)
Parameters
- message
Optional[Union[str, bytes]] The message to be sent to the client. If
None
then the HTTP status 'Bad Request' will be sent. Defaults toNone
.
- quiet
Optional[bool] When
True
, the error traceback will be suppressed from the logs. Defaults toNone
.
- context
Optional[Dict[str, Any]] Additional mapping of key/value data that will be sent to the client upon exception. Defaults to
None
.
- extra
Optional[Dict[str, Any]] Additional mapping of key/value data that will NOT be sent to the client when in PRODUCTION mode. Defaults to
None
.
- headers
Optional[Dict[str, Any]] Additional headers that should be sent with the HTTP response. Defaults to
None
.
sanic.exceptions.InvalidSignal#
Exception raised when an invalid signal is sent.
class InvalidSignal(message: typing.Union[str, bytes, NoneType] = None, status_code: typing.Optional[int] = None, quiet: typing.Optional[bool] = None, context: typing.Optional[typing.Dict[str, typing.Any]] = None, extra: typing.Optional[typing.Dict[str, typing.Any]] = None, headers: typing.Optional[typing.Dict[str, typing.Any]] = None): -> None
sanic.exceptions.InvalidUsage#
400 Bad Request
class InvalidUsage(message: typing.Union[str, bytes, NoneType] = None, quiet: typing.Optional[bool] = None, context: typing.Optional[typing.Dict[str, typing.Any]] = None, extra: typing.Optional[typing.Dict[str, typing.Any]] = None, headers: typing.Optional[typing.Dict[str, typing.Any]] = None): -> None
Parameters
- message
Optional[Union[str, bytes]] The message to be sent to the client. If
None
then the HTTP status 'Bad Request' will be sent. Defaults toNone
.
- quiet
Optional[bool] When
True
, the error traceback will be suppressed from the logs. Defaults toNone
.
- context
Optional[Dict[str, Any]] Additional mapping of key/value data that will be sent to the client upon exception. Defaults to
None
.
- extra
Optional[Dict[str, Any]] Additional mapping of key/value data that will NOT be sent to the client when in PRODUCTION mode. Defaults to
None
.
- headers
Optional[Dict[str, Any]] Additional headers that should be sent with the HTTP response. Defaults to
None
.
sanic.exceptions.LoadFileException#
Exception raised when a file cannot be loaded.
class LoadFileException(message: typing.Union[str, bytes, NoneType] = None, status_code: typing.Optional[int] = None, quiet: typing.Optional[bool] = None, context: typing.Optional[typing.Dict[str, typing.Any]] = None, extra: typing.Optional[typing.Dict[str, typing.Any]] = None, headers: typing.Optional[typing.Dict[str, typing.Any]] = None): -> None
sanic.exceptions.MethodNotAllowed#
405 Method Not Allowed
class MethodNotAllowed(message: typing.Union[str, bytes, NoneType] = None, method: <class 'str'> = , allowed_methods: typing.Optional[typing.Sequence[str]] = None, quiet: typing.Optional[bool] = None, context: typing.Optional[typing.Dict[str, typing.Any]] = None, extra: typing.Optional[typing.Dict[str, typing.Any]] = None, headers: typing.Optional[typing.Dict[str, typing.Any]] = None)
Parameters
- message
Optional[Union[str, bytes]] The message to be sent to the client. If
None
then the HTTP status 'Method Not Allowed' will be sent. Defaults toNone
.
- method
Optional[str] The HTTP method that was used. Defaults to an empty string.
- allowed_methods
Optional[Sequence[str]] The HTTP methods that can be used instead of the one that was attempted.
- quiet
Optional[bool] When
True
, the error traceback will be suppressed from the logs. Defaults toNone
.
- context
Optional[Dict[str, Any]] Additional mapping of key/value data that will be sent to the client upon exception. Defaults to
None
.
- extra
Optional[Dict[str, Any]] Additional mapping of key/value data that will NOT be sent to the client when in PRODUCTION mode. Defaults to
None
.
- headers
Optional[Dict[str, Any]] Additional headers that should be sent with the HTTP response. Defaults to
None
.
sanic.exceptions.MethodNotSupported#
405 Method Not Allowed
class MethodNotSupported(message: typing.Union[str, bytes, NoneType] = None, method: <class 'str'> = , allowed_methods: typing.Optional[typing.Sequence[str]] = None, quiet: typing.Optional[bool] = None, context: typing.Optional[typing.Dict[str, typing.Any]] = None, extra: typing.Optional[typing.Dict[str, typing.Any]] = None, headers: typing.Optional[typing.Dict[str, typing.Any]] = None)
Parameters
- message
Optional[Union[str, bytes]] The message to be sent to the client. If
None
then the HTTP status 'Method Not Allowed' will be sent. Defaults toNone
.
- method
Optional[str] The HTTP method that was used. Defaults to an empty string.
- allowed_methods
Optional[Sequence[str]] The HTTP methods that can be used instead of the one that was attempted.
- quiet
Optional[bool] When
True
, the error traceback will be suppressed from the logs. Defaults toNone
.
- context
Optional[Dict[str, Any]] Additional mapping of key/value data that will be sent to the client upon exception. Defaults to
None
.
- extra
Optional[Dict[str, Any]] Additional mapping of key/value data that will NOT be sent to the client when in PRODUCTION mode. Defaults to
None
.
- headers
Optional[Dict[str, Any]] Additional headers that should be sent with the HTTP response. Defaults to
None
.
sanic.exceptions.NotFound#
A base class for other exceptions and should not be called directly.
class NotFound(message: typing.Union[str, bytes, NoneType] = None, quiet: typing.Optional[bool] = None, context: typing.Optional[typing.Dict[str, typing.Any]] = None, extra: typing.Optional[typing.Dict[str, typing.Any]] = None, headers: typing.Optional[typing.Dict[str, typing.Any]] = None): -> None
Parameters
- message
Optional[Union[str, bytes]] The message to be sent to the client. If
None
, then the appropriate HTTP response status message will be used instead. Defaults toNone
.
- quiet
Optional[bool] When
True
, the error traceback will be suppressed from the logs. Defaults toNone
.
- context
Optional[Dict[str, Any]] Additional mapping of key/value data that will be sent to the client upon exception. Defaults to
None
.
- extra
Optional[Dict[str, Any]] Additional mapping of key/value data that will NOT be sent to the client when in PRODUCTION mode. Defaults to
None
.
- headers
Optional[Dict[str, Any]] Additional headers that should be sent with the HTTP response. Defaults to
None
.
sanic.exceptions.PayloadTooLarge#
413 Payload Too Large
class PayloadTooLarge(message: typing.Union[str, bytes, NoneType] = None, quiet: typing.Optional[bool] = None, context: typing.Optional[typing.Dict[str, typing.Any]] = None, extra: typing.Optional[typing.Dict[str, typing.Any]] = None, headers: typing.Optional[typing.Dict[str, typing.Any]] = None): -> None
This is an internal exception thrown by Sanic and should not be used directly.
Parameters
- message
Optional[Union[str, bytes]] The message to be sent to the client. If
None
then the HTTP status 'Bad Request' will be sent. Defaults toNone
.
- quiet
Optional[bool] When
True
, the error traceback will be suppressed from the logs. Defaults toNone
.
- context
Optional[Dict[str, Any]] Additional mapping of key/value data that will be sent to the client upon exception. Defaults to
None
.
- extra
Optional[Dict[str, Any]] Additional mapping of key/value data that will NOT be sent to the client when in PRODUCTION mode. Defaults to
None
.
- headers
Optional[Dict[str, Any]] Additional headers that should be sent with the HTTP response. Defaults to
None
.
sanic.exceptions.PyFileError#
Generic exception that will generate an HTTP response when raised in the context of a request lifecycle.
class PyFileError(file, status_code: typing.Optional[int] = None, quiet: typing.Optional[bool] = None, context: typing.Optional[typing.Dict[str, typing.Any]] = None, extra: typing.Optional[typing.Dict[str, typing.Any]] = None, headers: typing.Optional[typing.Dict[str, typing.Any]] = None)
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
.
Parameters
- message
Optional[Union[str, bytes]] The message to be sent to the client. If
None
, then the appropriate HTTP response status message will be used instead. Defaults toNone
.
- status_code
Optional[int] The HTTP response code to send, if applicable. If
None
, then it will be 500. Defaults toNone
.
- quiet
Optional[bool] When
True
, the error traceback will be suppressed from the logs. Defaults toNone
.
- context
Optional[Dict[str, Any]] Additional mapping of key/value data that will be sent to the client upon exception. Defaults to
None
.
- extra
Optional[Dict[str, Any]] Additional mapping of key/value data that will NOT be sent to the client when in PRODUCTION mode. Defaults to
None
.
- headers
Optional[Dict[str, Any]] Additional headers that should be sent with the HTTP response. Defaults to
None
.
Examples
raise SanicException(
"Something went wrong",
status_code=999,
context={
"info": "Some additional details to send to the client",
},
headers={
"X-Foo": "bar"
}
)
sanic.exceptions.RangeNotSatisfiable#
416 Range Not Satisfiable
class RangeNotSatisfiable(message: typing.Union[str, bytes, NoneType] = None, content_range: typing.Optional[sanic.models.protocol_types.Range] = None, quiet: typing.Optional[bool] = None, context: typing.Optional[typing.Dict[str, typing.Any]] = None, extra: typing.Optional[typing.Dict[str, typing.Any]] = None, headers: typing.Optional[typing.Dict[str, typing.Any]] = None)
Parameters
- message
Optional[Union[str, bytes]] The message to be sent to the client. If
None
then the HTTP status 'Range Not Satisfiable' will be sent. Defaults toNone
.
- content_range
Optional[ContentRange] An object meeting the :class:
.ContentRange
protocol that has atotal
property. Defaults toNone
.
- quiet
Optional[bool] When
True
, the error traceback will be suppressed from the logs. Defaults toNone
.
- context
Optional[Dict[str, Any]] Additional mapping of key/value data that will be sent to the client upon exception. Defaults to
None
.
- extra
Optional[Dict[str, Any]] Additional mapping of key/value data that will NOT be sent to the client when in PRODUCTION mode. Defaults to
None
.
- headers
Optional[Dict[str, Any]] Additional headers that should be sent with the HTTP response. Defaults to
None
.
sanic.exceptions.RequestCancelled#
The Future or Task was cancelled.
class RequestCancelled(self)
sanic.exceptions.RequestTimeout#
408 Request Timeout
class RequestTimeout(message: typing.Union[str, bytes, NoneType] = None, quiet: typing.Optional[bool] = None, context: typing.Optional[typing.Dict[str, typing.Any]] = None, extra: typing.Optional[typing.Dict[str, typing.Any]] = None, headers: typing.Optional[typing.Dict[str, typing.Any]] = None): -> None
The Web server (running the Web site) thinks that there has been too long an interval of time between 1) the establishment of an IP connection (socket) between the client and the server and 2) the receipt of any data on that socket, so the server has dropped the connection. The socket connection has actually been lost - the Web server has 'timed out' on that particular socket connection.
This is an internal exception thrown by Sanic and should not be used directly.
Parameters
- message
Optional[Union[str, bytes]] The message to be sent to the client. If
None
then the HTTP status 'Bad Request' will be sent. Defaults toNone
.
- quiet
Optional[bool] When
True
, the error traceback will be suppressed from the logs. Defaults toNone
.
- context
Optional[Dict[str, Any]] Additional mapping of key/value data that will be sent to the client upon exception. Defaults to
None
.
- extra
Optional[Dict[str, Any]] Additional mapping of key/value data that will NOT be sent to the client when in PRODUCTION mode. Defaults to
None
.
- headers
Optional[Dict[str, Any]] Additional headers that should be sent with the HTTP response. Defaults to
None
.
sanic.exceptions.SanicException#
Generic exception that will generate an HTTP response when raised in the context of a request lifecycle.
class SanicException(message: typing.Union[str, bytes, NoneType] = None, status_code: typing.Optional[int] = None, quiet: typing.Optional[bool] = None, context: typing.Optional[typing.Dict[str, typing.Any]] = None, extra: typing.Optional[typing.Dict[str, typing.Any]] = None, headers: typing.Optional[typing.Dict[str, typing.Any]] = None): -> None
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
.
Parameters
- message
Optional[Union[str, bytes]] The message to be sent to the client. If
None
, then the appropriate HTTP response status message will be used instead. Defaults toNone
.
- status_code
Optional[int] The HTTP response code to send, if applicable. If
None
, then it will be 500. Defaults toNone
.
- quiet
Optional[bool] When
True
, the error traceback will be suppressed from the logs. Defaults toNone
.
- context
Optional[Dict[str, Any]] Additional mapping of key/value data that will be sent to the client upon exception. Defaults to
None
.
- extra
Optional[Dict[str, Any]] Additional mapping of key/value data that will NOT be sent to the client when in PRODUCTION mode. Defaults to
None
.
- headers
Optional[Dict[str, Any]] Additional headers that should be sent with the HTTP response. Defaults to
None
.
Examples
raise SanicException(
"Something went wrong",
status_code=999,
context={
"info": "Some additional details to send to the client",
},
headers={
"X-Foo": "bar"
}
)
sanic.exceptions.ServerError#
500 Internal Server Error
class ServerError(message: typing.Union[str, bytes, NoneType] = None, quiet: typing.Optional[bool] = None, context: typing.Optional[typing.Dict[str, typing.Any]] = None, extra: typing.Optional[typing.Dict[str, typing.Any]] = None, headers: typing.Optional[typing.Dict[str, typing.Any]] = None): -> None
A general server-side error has occurred. If no other HTTP exception is appropriate, then this should be used
Parameters
- message
Optional[Union[str, bytes]] The message to be sent to the client. If
None
then the HTTP status 'Bad Request' will be sent. Defaults toNone
.
- quiet
Optional[bool] When
True
, the error traceback will be suppressed from the logs. Defaults toNone
.
- context
Optional[Dict[str, Any]] Additional mapping of key/value data that will be sent to the client upon exception. Defaults to
None
.
- extra
Optional[Dict[str, Any]] Additional mapping of key/value data that will NOT be sent to the client when in PRODUCTION mode. Defaults to
None
.
- headers
Optional[Dict[str, Any]] Additional headers that should be sent with the HTTP response. Defaults to
None
.
sanic.exceptions.ServerKilled#
Exception Sanic server uses when killing a server process for something unexpected happening.
class ServerKilled(self)
sanic.exceptions.ServiceUnavailable#
503 Service Unavailable
class ServiceUnavailable(message: typing.Union[str, bytes, NoneType] = None, quiet: typing.Optional[bool] = None, context: typing.Optional[typing.Dict[str, typing.Any]] = None, extra: typing.Optional[typing.Dict[str, typing.Any]] = None, headers: typing.Optional[typing.Dict[str, typing.Any]] = None): -> None
The server is currently unavailable (because it is overloaded or down for maintenance). Generally, this is a temporary state.
Parameters
- message
Optional[Union[str, bytes]] The message to be sent to the client. If
None
then the HTTP status 'Bad Request' will be sent. Defaults toNone
.
- quiet
Optional[bool] When
True
, the error traceback will be suppressed from the logs. Defaults toNone
.
- context
Optional[Dict[str, Any]] Additional mapping of key/value data that will be sent to the client upon exception. Defaults to
None
.
- extra
Optional[Dict[str, Any]] Additional mapping of key/value data that will NOT be sent to the client when in PRODUCTION mode. Defaults to
None
.
- headers
Optional[Dict[str, Any]] Additional headers that should be sent with the HTTP response. Defaults to
None
.
sanic.exceptions.URLBuildError#
500 Internal Server Error
class URLBuildError(message: typing.Union[str, bytes, NoneType] = None, quiet: typing.Optional[bool] = None, context: typing.Optional[typing.Dict[str, typing.Any]] = None, extra: typing.Optional[typing.Dict[str, typing.Any]] = None, headers: typing.Optional[typing.Dict[str, typing.Any]] = None): -> None
An exception used by Sanic internals when unable to build a URL.
Parameters
- message
Optional[Union[str, bytes]] The message to be sent to the client. If
None
then the HTTP status 'Bad Request' will be sent. Defaults toNone
.
- quiet
Optional[bool] When
True
, the error traceback will be suppressed from the logs. Defaults toNone
.
- context
Optional[Dict[str, Any]] Additional mapping of key/value data that will be sent to the client upon exception. Defaults to
None
.
- extra
Optional[Dict[str, Any]] Additional mapping of key/value data that will NOT be sent to the client when in PRODUCTION mode. Defaults to
None
.
- headers
Optional[Dict[str, Any]] Additional headers that should be sent with the HTTP response. Defaults to
None
.
sanic.exceptions.Unauthorized#
Status: 401 Unauthorized
class Unauthorized(message: typing.Union[str, bytes, NoneType] = None, scheme: typing.Optional[str] = None, quiet: typing.Optional[bool] = None, context: typing.Optional[typing.Dict[str, typing.Any]] = None, extra: typing.Optional[typing.Dict[str, typing.Any]] = None, headers: typing.Optional[typing.Dict[str, typing.Any]] = None, challenges)
When present, additional keyword arguments may be used to complete the WWW-Authentication header.
Parameters
- message
Optional[Union[str, bytes]] The message to be sent to the client. If
None
then the HTTP status 'Bad Request' will be sent. Defaults toNone
.
- scheme
Optional[str] Name of the authentication scheme to be used. Defaults to
None
.
- quiet
Optional[bool] When
True
, the error traceback will be suppressed from the logs. Defaults toNone
.
- context
Optional[Dict[str, Any]] Additional mapping of key/value data that will be sent to the client upon exception. Defaults to
None
.
- extra
Optional[Dict[str, Any]] Additional mapping of key/value data that will NOT be sent to the client when in PRODUCTION mode. Defaults to
None
.
- headers
Optional[Dict[str, Any]] Additional headers that should be sent with the HTTP response. Defaults to
None
.
- **challenges
Dict[str, Any] Additional keyword arguments that will be used to complete the WWW-Authentication header. Defaults to
None
.
Examples
With a Basic auth-scheme, realm MUST be present:
raise Unauthorized(
"Auth required.",
scheme="Basic",
realm="Restricted Area"
)
With a Digest auth-scheme, things are a bit more complicated:
raise Unauthorized(
"Auth required.",
scheme="Digest",
realm="Restricted Area",
qop="auth, auth-int",
algorithm="MD5",
nonce="abcdef",
opaque="zyxwvu"
)
With a Bearer auth-scheme, realm is optional so you can write:
raise Unauthorized("Auth required.", scheme="Bearer")
or, if you want to specify the realm:
raise Unauthorized(
"Auth required.",
scheme="Bearer",
realm="Restricted Area"
)
sanic.exceptions.WebsocketClosed#
Exception raised when a websocket is closed.
class WebsocketClosed(message: typing.Union[str, bytes, NoneType] = None, status_code: typing.Optional[int] = None, quiet: typing.Optional[bool] = None, context: typing.Optional[typing.Dict[str, typing.Any]] = None, extra: typing.Optional[typing.Dict[str, typing.Any]] = None, headers: typing.Optional[typing.Dict[str, typing.Any]] = None): -> None