sanic.exceptions.BadRequest#

400 Bad Request

Inherits from: HTTPException, SanicException, Exception, BaseException

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 to None.

quiet
Optional[bool]

When True, the error traceback will be suppressed from the logs. Defaults to None.

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

Inherits from: HTTPException, SanicException, Exception, BaseException

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 to None.

quiet
Optional[bool]

When True, the error traceback will be suppressed from the logs. Defaults to None.

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

Inherits from: HTTPException, SanicException, Exception, BaseException

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 to None.

content_range
Optional[ContentRange]

An object meeting the :class:.ContentRange protocol that has a total property. Defaults to None.

quiet
Optional[bool]

When True, the error traceback will be suppressed from the logs. Defaults to None.

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

Inherits from: HTTPException, SanicException, Exception, BaseException

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 to None.

quiet
Optional[bool]

When True, the error traceback will be suppressed from the logs. Defaults to None.

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

Inherits from: NotFound, HTTPException, SanicException, Exception, BaseException

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 to None.

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 to None.

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

Inherits from: HTTPException, SanicException, Exception, BaseException

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 to None.

quiet
Optional[bool]

When True, the error traceback will be suppressed from the logs. Defaults to None.

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.

Inherits from: SanicException, Exception, BaseException

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

Inherits from: HTTPException, SanicException, Exception, BaseException

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 to None.

quiet
Optional[bool]

When True, the error traceback will be suppressed from the logs. Defaults to None.

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

Inherits from: BadRequest, HTTPException, SanicException, Exception, BaseException

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 to None.

quiet
Optional[bool]

When True, the error traceback will be suppressed from the logs. Defaults to None.

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

Inherits from: HTTPException, SanicException, Exception, BaseException

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 to None.

quiet
Optional[bool]

When True, the error traceback will be suppressed from the logs. Defaults to None.

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

Inherits from: BadRequest, HTTPException, SanicException, Exception, BaseException

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 to None.

quiet
Optional[bool]

When True, the error traceback will be suppressed from the logs. Defaults to None.

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

Inherits from: RangeNotSatisfiable, HTTPException, SanicException, Exception, BaseException

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 to None.

quiet
Optional[bool]

When True, the error traceback will be suppressed from the logs. Defaults to None.

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.

Inherits from: SanicException, Exception, BaseException

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

Inherits from: HTTPException, SanicException, Exception, BaseException

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 to None.

quiet
Optional[bool]

When True, the error traceback will be suppressed from the logs. Defaults to None.

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.

Inherits from: SanicException, Exception, BaseException

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

Inherits from: HTTPException, SanicException, Exception, BaseException

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 to None.

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 to None.

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

Inherits from: HTTPException, SanicException, Exception, BaseException

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 to None.

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 to None.

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.

Inherits from: HTTPException, SanicException, Exception, BaseException

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 to None.

quiet
Optional[bool]

When True, the error traceback will be suppressed from the logs. Defaults to None.

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

Inherits from: HTTPException, SanicException, Exception, BaseException

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 to None.

quiet
Optional[bool]

When True, the error traceback will be suppressed from the logs. Defaults to None.

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.

Inherits from: SanicException, Exception, BaseException

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 to None.

status_code
Optional[int]

The HTTP response code to send, if applicable. If None, then it will be 500. Defaults to None.

quiet
Optional[bool]

When True, the error traceback will be suppressed from the logs. Defaults to None.

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

Inherits from: HTTPException, SanicException, Exception, BaseException

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 to None.

content_range
Optional[ContentRange]

An object meeting the :class:.ContentRange protocol that has a total property. Defaults to None.

quiet
Optional[bool]

When True, the error traceback will be suppressed from the logs. Defaults to None.

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.

Inherits from: CancelledError, BaseException

class RequestCancelled(self)

sanic.exceptions.RequestTimeout#

408 Request Timeout

Inherits from: HTTPException, SanicException, Exception, BaseException

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 to None.

quiet
Optional[bool]

When True, the error traceback will be suppressed from the logs. Defaults to None.

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.

Inherits from: Exception, BaseException

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 to None.

status_code
Optional[int]

The HTTP response code to send, if applicable. If None, then it will be 500. Defaults to None.

quiet
Optional[bool]

When True, the error traceback will be suppressed from the logs. Defaults to None.

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

Inherits from: HTTPException, SanicException, Exception, BaseException

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 to None.

quiet
Optional[bool]

When True, the error traceback will be suppressed from the logs. Defaults to None.

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.

Inherits from: Exception, BaseException

class ServerKilled(self)

sanic.exceptions.ServiceUnavailable#

503 Service Unavailable

Inherits from: HTTPException, SanicException, Exception, BaseException

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 to None.

quiet
Optional[bool]

When True, the error traceback will be suppressed from the logs. Defaults to None.

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

Inherits from: HTTPException, SanicException, Exception, BaseException

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 to None.

quiet
Optional[bool]

When True, the error traceback will be suppressed from the logs. Defaults to None.

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

Inherits from: HTTPException, SanicException, Exception, BaseException

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 to None.

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 to None.

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.

Inherits from: SanicException, Exception, BaseException

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