Warning

These documents are OUTDATED as of 2023-12-31.

Please refer to the latest version of the documentation at sanic.dev.

Exceptions

sanic.errorpages

Sanic provides a pattern for providing a response when an exception occurs. However, if you do no handle an exception, it will provide a fallback. There are three fallback types:

  • HTML - default

  • Text

  • JSON

Setting app.config.FALLBACK_ERROR_FORMAT = "auto" will enable a switch that will attempt to provide an appropriate response format based upon the request type.

class sanic.errorpages.BaseRenderer(request, exception, debug)

Bases: object

Base class that all renderers must inherit from.

This class defines the structure for rendering objects, handling the core functionality that specific renderers may extend.

Attributes:

request (Request): The incoming request object that needs rendering. exception (Exception): Any exception that occurred and needs to be rendered. debug (bool): Flag indicating whether to render with debugging information.

Methods:

dumps: A static method that must be overridden by subclasses to define the specific rendering.

Args:

request (Request): The incoming request object that needs rendering. exception (Exception): Any exception that occurred and needs to be rendered. debug (bool): Flag indicating whether to render with debugging information.

Parameters:
  • request (Request) –

  • exception (Exception) –

  • debug (bool) –

static dumps()

Converts arbitrary object recursively into JSON. Use ensure_ascii=false to output UTF-8. Set encode_html_chars=True to encode < > & as unicode escape sequences. Set escape_forward_slashes=False to prevent escaping / characters.Set allow_nan=False to raise an exception when NaN or Infinity would be serialized.Set reject_bytes=True to raise TypeError on bytes.

full()

Provide a formatted message that has all details and is mean to be used primarily for debugging and non-production environments.

Returns:

HTTPResponse: The response object.

Return type:

HTTPResponse

minimal()

Provide a formatted message that is meant to not show any sensitive data or details.

This is the default fallback for production environments.

Returns:

HTTPResponse: The response object.

Return type:

HTTPResponse

render()

Outputs the exception as a response.

Returns:

HTTPResponse: The response object.

Return type:

HTTPResponse

property headers: Dict[str, str]

The headers to be used for the response.

property status

The status code to be used for the response.

property text

The text to be used for the response.

property title

The title to be used for the response.

class sanic.errorpages.HTMLRenderer(request, exception, debug)

Bases: BaseRenderer

Render an exception as HTML.

The default fallback type.

Parameters:
  • request (Request) –

  • exception (Exception) –

  • debug (bool) –

full()

Provide a formatted message that has all details and is mean to be used primarily for debugging and non-production environments.

Returns:

HTTPResponse: The response object.

Return type:

HTTPResponse

minimal()

Provide a formatted message that is meant to not show any sensitive data or details.

This is the default fallback for production environments.

Returns:

HTTPResponse: The response object.

Return type:

HTTPResponse

class sanic.errorpages.JSONRenderer(request, exception, debug)

Bases: BaseRenderer

Render an exception as JSON.

Parameters:
  • request (Request) –

  • exception (Exception) –

  • debug (bool) –

full()

Provide a formatted message that has all details and is mean to be used primarily for debugging and non-production environments.

Returns:

HTTPResponse: The response object.

Return type:

HTTPResponse

minimal()

Provide a formatted message that is meant to not show any sensitive data or details.

This is the default fallback for production environments.

Returns:

HTTPResponse: The response object.

Return type:

HTTPResponse

property title

The title to be used for the response.

class sanic.errorpages.TextRenderer(request, exception, debug)

Bases: BaseRenderer

Render an exception as plain text.

Parameters:
  • request (Request) –

  • exception (Exception) –

  • debug (bool) –

full()

Provide a formatted message that has all details and is mean to be used primarily for debugging and non-production environments.

Returns:

HTTPResponse: The response object.

Return type:

HTTPResponse

minimal()

Provide a formatted message that is meant to not show any sensitive data or details.

This is the default fallback for production environments.

Returns:

HTTPResponse: The response object.

Return type:

HTTPResponse

property title

The title to be used for the response.

sanic.errorpages.check_error_format(format)

Check that the format is known.

sanic.errorpages.escape(text)

Minimal HTML escaping, not for attribute values (unlike html.escape).

sanic.errorpages.exception_response(request, exception, debug, fallback, base, renderer=None)

Render a response for the default FALLBACK exception handler.

Parameters:
  • request (Request) –

  • exception (Exception) –

  • debug (bool) –

  • fallback (str) –

  • base (t.Type[BaseRenderer]) –

  • renderer (t.Optional[t.Type[BaseRenderer]]) –

Return type:

HTTPResponse

sanic.errorpages.guess_mime(req, fallback)

Guess the MIME type for the response based upon the request.

Parameters:
  • req (Request) –

  • fallback (str) –

Return type:

str

sanic.exceptions

exception sanic.exceptions.BadRequest(message=None, *, quiet=None, context=None, extra=None, headers=None)

Bases: HTTPException

400 Bad Request

Args:
message (Optional[Union[str, bytes]], optional): 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], optional): When True, the error traceback will be suppressed

from the logs. Defaults to None.

context (Optional[Dict[str, Any]], optional): Additional mapping of key/value data that will be

sent to the client upon exception. Defaults to None.

extra (Optional[Dict[str, Any]], optional): 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]], optional): Additional headers that should be sent with the HTTP

response. Defaults to None.

Parameters:
  • message (str) –

  • quiet (bool | None) –

  • context (Dict[str, Any] | None) –

  • extra (Dict[str, Any] | None) –

  • headers (Dict[str, str]) –

Return type:

None

sanic.exceptions.BadURL

alias of BadRequest

sanic.exceptions.ContentRangeError

alias of RangeNotSatisfiable

exception sanic.exceptions.ExpectationFailed(message=None, *, quiet=None, context=None, extra=None, headers=None)

Bases: HTTPException

417 Expectation Failed

Args:
message (Optional[Union[str, bytes]], optional): 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], optional): When True, the error traceback will be suppressed

from the logs. Defaults to None.

context (Optional[Dict[str, Any]], optional): Additional mapping of key/value data that will be

sent to the client upon exception. Defaults to None.

extra (Optional[Dict[str, Any]], optional): 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]], optional): Additional headers that should be sent with the HTTP

response. Defaults to None.

Parameters:
  • message (str) –

  • quiet (bool | None) –

  • context (Dict[str, Any] | None) –

  • extra (Dict[str, Any] | None) –

  • headers (Dict[str, str]) –

Return type:

None

exception sanic.exceptions.FileNotFound(message=None, path=None, relative_url=None, *, quiet=None, context=None, extra=None, headers=None)

Bases: NotFound

404 Not Found

A specific form of NotFound that is specifically when looking for a file on the file system at a known path.

Args:
message (Optional[Union[str, bytes]], optional): 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], optional): The path, if any, to the file that could not

be found. Defaults to None.

relative_url (Optional[str], optional): A relative URL of the file. Defaults to None. quiet (Optional[bool], optional): When True, the error traceback will be suppressed

from the logs. Defaults to None.

context (Optional[Dict[str, Any]], optional): Additional mapping of key/value data that will be

sent to the client upon exception. Defaults to None.

extra (Optional[Dict[str, Any]], optional): 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]], optional): Additional headers that should be sent with the HTTP

response. Defaults to None.

Parameters:
  • message (str) –

  • path (PathLike | None) –

  • relative_url (str | None) –

  • quiet (bool | None) –

  • context (Dict[str, Any] | None) –

  • extra (Dict[str, Any] | None) –

  • headers (Dict[str, str]) –

exception sanic.exceptions.Forbidden(message=None, *, quiet=None, context=None, extra=None, headers=None)

Bases: HTTPException

403 Forbidden

Args:
message (Optional[Union[str, bytes]], optional): 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], optional): When True, the error traceback will be suppressed

from the logs. Defaults to None.

context (Optional[Dict[str, Any]], optional): Additional mapping of key/value data that will be

sent to the client upon exception. Defaults to None.

extra (Optional[Dict[str, Any]], optional): 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]], optional): Additional headers that should be sent with the HTTP

response. Defaults to None.

Parameters:
  • message (str) –

  • quiet (bool | None) –

  • context (Dict[str, Any] | None) –

  • extra (Dict[str, Any] | None) –

  • headers (Dict[str, str]) –

Return type:

None

exception sanic.exceptions.HTTPException(message=None, *, quiet=None, context=None, extra=None, headers=None)

Bases: SanicException

A base class for other exceptions and should not be called directly.

Parameters:
  • message (str) –

  • quiet (bool | None) –

  • context (Dict[str, Any] | None) –

  • extra (Dict[str, Any] | None) –

  • headers (Dict[str, str]) –

Return type:

None

sanic.exceptions.HeaderExpectationFailed

alias of ExpectationFailed

exception sanic.exceptions.HeaderNotFound(message=None, *, quiet=None, context=None, extra=None, headers=None)

Bases: BadRequest

400 Bad Request

Args:
message (Optional[Union[str, bytes]], optional): 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], optional): When True, the error traceback will be suppressed

from the logs. Defaults to None.

context (Optional[Dict[str, Any]], optional): Additional mapping of key/value data that will be

sent to the client upon exception. Defaults to None.

extra (Optional[Dict[str, Any]], optional): 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]], optional): Additional headers that should be sent with the HTTP

response. Defaults to None.

Parameters:
  • message (str) –

  • quiet (bool | None) –

  • context (Dict[str, Any] | None) –

  • extra (Dict[str, Any] | None) –

  • headers (Dict[str, str]) –

Return type:

None

sanic.exceptions.InternalServerError

alias of ServerError

exception sanic.exceptions.InvalidHeader(message=None, *, quiet=None, context=None, extra=None, headers=None)

Bases: BadRequest

400 Bad Request

Args:
message (Optional[Union[str, bytes]], optional): 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], optional): When True, the error traceback will be suppressed

from the logs. Defaults to None.

context (Optional[Dict[str, Any]], optional): Additional mapping of key/value data that will be

sent to the client upon exception. Defaults to None.

extra (Optional[Dict[str, Any]], optional): 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]], optional): Additional headers that should be sent with the HTTP

response. Defaults to None.

Parameters:
  • message (str) –

  • quiet (bool | None) –

  • context (Dict[str, Any] | None) –

  • extra (Dict[str, Any] | None) –

  • headers (Dict[str, str]) –

Return type:

None

exception sanic.exceptions.InvalidRangeType(message=None, content_range=None, *, quiet=None, context=None, extra=None, headers=None)

Bases: RangeNotSatisfiable

416 Range Not Satisfiable

Args:
message (Optional[Union[str, bytes]], optional): 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], optional): When True, the error traceback will be suppressed

from the logs. Defaults to None.

context (Optional[Dict[str, Any]], optional): Additional mapping of key/value data that will be

sent to the client upon exception. Defaults to None.

extra (Optional[Dict[str, Any]], optional): 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]], optional): Additional headers that should be sent with the HTTP

response. Defaults to None.

Parameters:
  • message (str) –

  • content_range (Range | None) –

  • quiet (bool | None) –

  • context (Dict[str, Any] | None) –

  • extra (Dict[str, Any] | None) –

  • headers (Dict[str, str]) –

exception sanic.exceptions.InvalidSignal(message=None, status_code=None, *, quiet=None, context=None, extra=None, headers=None)

Bases: SanicException

Exception raised when an invalid signal is sent.

Parameters:
  • message (str) –

  • status_code (int) –

  • quiet (bool | None) –

  • context (Dict[str, Any] | None) –

  • extra (Dict[str, Any] | None) –

  • headers (Dict[str, str]) –

Return type:

None

sanic.exceptions.InvalidUsage

alias of BadRequest

exception sanic.exceptions.LoadFileException(message=None, status_code=None, *, quiet=None, context=None, extra=None, headers=None)

Bases: SanicException

Exception raised when a file cannot be loaded.

Parameters:
  • message (str) –

  • status_code (int) –

  • quiet (bool | None) –

  • context (Dict[str, Any] | None) –

  • extra (Dict[str, Any] | None) –

  • headers (Dict[str, str]) –

Return type:

None

exception sanic.exceptions.MethodNotAllowed(message=None, method='', allowed_methods=None, *, quiet=None, context=None, extra=None, headers=None)

Bases: HTTPException

405 Method Not Allowed

Args:
message (Optional[Union[str, bytes]], optional): 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], optional): The HTTP method that was used. Defaults to an empty string. allowed_methods (Optional[Sequence[str]], optional): The HTTP methods that can be used instead of the

one that was attempted.

quiet (Optional[bool], optional): When True, the error traceback will be suppressed

from the logs. Defaults to None.

context (Optional[Dict[str, Any]], optional): Additional mapping of key/value data that will be

sent to the client upon exception. Defaults to None.

extra (Optional[Dict[str, Any]], optional): 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]], optional): Additional headers that should be sent with the HTTP

response. Defaults to None.

Parameters:
  • message (str) –

  • method (str) –

  • allowed_methods (Sequence[str] | None) –

  • quiet (bool | None) –

  • context (Dict[str, Any] | None) –

  • extra (Dict[str, Any] | None) –

  • headers (Dict[str, str]) –

sanic.exceptions.MethodNotSupported

alias of MethodNotAllowed

exception sanic.exceptions.NotFound(message=None, *, quiet=None, context=None, extra=None, headers=None)

Bases: HTTPException

A base class for other exceptions and should not be called directly.

Args:
message (Optional[Union[str, bytes]], optional): 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], optional): When True, the error traceback will be suppressed from the logs.

Defaults to None.

context (Optional[Dict[str, Any]], optional): Additional mapping of key/value data that will be

sent to the client upon exception. Defaults to None.

extra (Optional[Dict[str, Any]], optional): 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]], optional): Additional headers that should be sent with the HTTP

response. Defaults to None.

Parameters:
  • message (str) –

  • quiet (bool | None) –

  • context (Dict[str, Any] | None) –

  • extra (Dict[str, Any] | None) –

  • headers (Dict[str, str]) –

Return type:

None

exception sanic.exceptions.PayloadTooLarge(message=None, *, quiet=None, context=None, extra=None, headers=None)

Bases: HTTPException

413 Payload Too Large

This is an internal exception thrown by Sanic and should not be used directly.

Args:
message (Optional[Union[str, bytes]], optional): 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], optional): When True, the error traceback will be suppressed

from the logs. Defaults to None.

context (Optional[Dict[str, Any]], optional): Additional mapping of key/value data that will be

sent to the client upon exception. Defaults to None.

extra (Optional[Dict[str, Any]], optional): 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]], optional): Additional headers that should be sent with the HTTP

response. Defaults to None.

Parameters:
  • message (str) –

  • quiet (bool | None) –

  • context (Dict[str, Any] | None) –

  • extra (Dict[str, Any] | None) –

  • headers (Dict[str, str]) –

Return type:

None

exception sanic.exceptions.PyFileError(file, status_code=None, *, quiet=None, context=None, extra=None, headers=None)

Bases: SanicException

Parameters:
  • status_code (int) –

  • quiet (bool | None) –

  • context (Dict[str, Any] | None) –

  • extra (Dict[str, Any] | None) –

  • headers (Dict[str, str]) –

exception sanic.exceptions.RangeNotSatisfiable(message=None, content_range=None, *, quiet=None, context=None, extra=None, headers=None)

Bases: HTTPException

416 Range Not Satisfiable

Args:
message (Optional[Union[str, bytes]], optional): 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], optional): An object meeting the ContentRange protocol

that has a total property. Defaults to None.

quiet (Optional[bool], optional): When True, the error traceback will be suppressed

from the logs. Defaults to None.

context (Optional[Dict[str, Any]], optional): Additional mapping of key/value data that will be

sent to the client upon exception. Defaults to None.

extra (Optional[Dict[str, Any]], optional): 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]], optional): Additional headers that should be sent with the HTTP

response. Defaults to None.

Parameters:
  • message (str) –

  • content_range (Range | None) –

  • quiet (bool | None) –

  • context (Dict[str, Any] | None) –

  • extra (Dict[str, Any] | None) –

  • headers (Dict[str, str]) –

exception sanic.exceptions.RequestCancelled

Bases: CancelledError

exception sanic.exceptions.RequestTimeout(message=None, *, quiet=None, context=None, extra=None, headers=None)

Bases: HTTPException

408 Request Timeout

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.

Args:
message (Optional[Union[str, bytes]], optional): 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], optional): When True, the error traceback will be suppressed

from the logs. Defaults to None.

context (Optional[Dict[str, Any]], optional): Additional mapping of key/value data that will be

sent to the client upon exception. Defaults to None.

extra (Optional[Dict[str, Any]], optional): 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]], optional): Additional headers that should be sent with the HTTP

response. Defaults to None.

Parameters:
  • message (str) –

  • quiet (bool | None) –

  • context (Dict[str, Any] | None) –

  • extra (Dict[str, Any] | None) –

  • headers (Dict[str, str]) –

Return type:

None

exception sanic.exceptions.SanicException(message=None, status_code=None, *, quiet=None, context=None, extra=None, headers=None)

Bases: Exception

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.

Args:
message (Optional[Union[str, bytes]], optional): 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], optional): The HTTP response code to send, if applicable. If None,

then it will be 500. Defaults to None.

quiet (Optional[bool], optional): When True, the error traceback will be suppressed from the logs.

Defaults to None.

context (Optional[Dict[str, Any]], optional): Additional mapping of key/value data that will be

sent to the client upon exception. Defaults to None.

extra (Optional[Dict[str, Any]], optional): 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]], optional): Additional headers that should be sent with the HTTP

response. Defaults to None.

Examples:

```python raise SanicException(

“Something went wrong”, status_code=999, context={

“info”: “Some additional details to send to the client”,

}, headers={

“X-Foo”: “bar”

}

Parameters:
  • message (str) –

  • status_code (int) –

  • quiet (bool | None) –

  • context (Dict[str, Any] | None) –

  • extra (Dict[str, Any] | None) –

  • headers (Dict[str, str]) –

Return type:

None

exception sanic.exceptions.ServerError(message=None, *, quiet=None, context=None, extra=None, headers=None)

Bases: HTTPException

500 Internal Server Error

A general server-side error has occurred. If no other HTTP exception is appropriate, then this should be used

Args:
message (Optional[Union[str, bytes]], optional): 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], optional): When True, the error traceback will be suppressed

from the logs. Defaults to None.

context (Optional[Dict[str, Any]], optional): Additional mapping of key/value data that will be

sent to the client upon exception. Defaults to None.

extra (Optional[Dict[str, Any]], optional): 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]], optional): Additional headers that should be sent with the HTTP

response. Defaults to None.

Parameters:
  • message (str) –

  • quiet (bool | None) –

  • context (Dict[str, Any] | None) –

  • extra (Dict[str, Any] | None) –

  • headers (Dict[str, str]) –

Return type:

None

exception sanic.exceptions.ServerKilled

Bases: Exception

Exception Sanic server uses when killing a server process for something unexpected happening.

exception sanic.exceptions.ServiceUnavailable(message=None, *, quiet=None, context=None, extra=None, headers=None)

Bases: HTTPException

503 Service Unavailable

The server is currently unavailable (because it is overloaded or down for maintenance). Generally, this is a temporary state.

Args:
message (Optional[Union[str, bytes]], optional): 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], optional): When True, the error traceback will be suppressed

from the logs. Defaults to None.

context (Optional[Dict[str, Any]], optional): Additional mapping of key/value data that will be

sent to the client upon exception. Defaults to None.

extra (Optional[Dict[str, Any]], optional): 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]], optional): Additional headers that should be sent with the HTTP

response. Defaults to None.

Parameters:
  • message (str) –

  • quiet (bool | None) –

  • context (Dict[str, Any] | None) –

  • extra (Dict[str, Any] | None) –

  • headers (Dict[str, str]) –

Return type:

None

exception sanic.exceptions.URLBuildError(message=None, *, quiet=None, context=None, extra=None, headers=None)

Bases: HTTPException

500 Internal Server Error

An exception used by Sanic internals when unable to build a URL.

Args:
message (Optional[Union[str, bytes]], optional): 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], optional): When True, the error traceback will be suppressed

from the logs. Defaults to None.

context (Optional[Dict[str, Any]], optional): Additional mapping of key/value data that will be

sent to the client upon exception. Defaults to None.

extra (Optional[Dict[str, Any]], optional): 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]], optional): Additional headers that should be sent with the HTTP

response. Defaults to None.

Parameters:
  • message (str) –

  • quiet (bool | None) –

  • context (Dict[str, Any] | None) –

  • extra (Dict[str, Any] | None) –

  • headers (Dict[str, str]) –

Return type:

None

exception sanic.exceptions.Unauthorized(message=None, scheme=None, *, quiet=None, context=None, extra=None, headers=None, **challenges)

Bases: HTTPException

Status: 401 Unauthorized

When present, additional keyword arguments may be used to complete the WWW-Authentication header.

Args:
message (Optional[Union[str, bytes]], optional): 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], optional): Name of the authentication scheme to be used. Defaults to None. quiet (Optional[bool], optional): When True, the error traceback will be suppressed

from the logs. Defaults to None.

context (Optional[Dict[str, Any]], optional): Additional mapping of key/value data that will be

sent to the client upon exception. Defaults to None.

extra (Optional[Dict[str, Any]], optional): 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]], optional): 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: ```python raise Unauthorized(

“Auth required.”, scheme=”Basic”, realm=”Restricted Area”

With a Digest auth-scheme, things are a bit more complicated: ```python 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: `python raise Unauthorized("Auth required.", scheme="Bearer") `

or, if you want to specify the realm: ```python raise Unauthorized(

“Auth required.”, scheme=”Bearer”, realm=”Restricted Area”

Parameters:
  • message (str) –

  • scheme (str | None) –

  • quiet (bool | None) –

  • context (Dict[str, Any] | None) –

  • extra (Dict[str, Any] | None) –

  • headers (Dict[str, str]) –

exception sanic.exceptions.WebsocketClosed(message=None, status_code=None, *, quiet=None, context=None, extra=None, headers=None)

Bases: SanicException

Exception raised when a websocket is closed.

Parameters:
  • message (str) –

  • status_code (int) –

  • quiet (bool | None) –

  • context (Dict[str, Any] | None) –

  • extra (Dict[str, Any] | None) –

  • headers (Dict[str, str]) –

Return type:

None