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.

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.

Return type:

HTTPResponse

minimal()ΒΆ

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

Return type:

HTTPResponse

render()ΒΆ

Outputs the exception as a HTTPResponse.

Returns:

The formatted exception

Return type:

str

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

Bases: BaseRenderer

Render an exception as HTML.

The default fallback type.

full()ΒΆ

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

Return type:

HTTPResponse

minimal()ΒΆ

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

Return type:

HTTPResponse

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

Bases: BaseRenderer

Render an exception as JSON.

full()ΒΆ

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

Return type:

HTTPResponse

minimal()ΒΆ

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

Return type:

HTTPResponse

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

Bases: BaseRenderer

Render an exception as plain text.

full()ΒΆ

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

Return type:

HTTPResponse

minimal()ΒΆ

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

Return type:

HTTPResponse

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.Type[t.Optional[BaseRenderer]]) –

Return type:

HTTPResponse

sanic.exceptionsΒΆ

exception sanic.exceptions.BadRequest(message=None, status_code=None, quiet=None, context=None, extra=None)ΒΆ

Bases: SanicException

Status: 400 Bad Request

Parameters:
  • message (str) –

  • status_code (int | None) –

  • quiet (bool | None) –

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

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

Return type:

None

exception sanic.exceptions.BadURL(message=None, status_code=None, quiet=None, context=None, extra=None)ΒΆ

Bases: BadRequest

Parameters:
  • message (str) –

  • status_code (int | None) –

  • quiet (bool | None) –

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

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

Return type:

None

sanic.exceptions.ContentRangeErrorΒΆ

alias of RangeNotSatisfiable

exception sanic.exceptions.ExpectationFailed(message=None, status_code=None, quiet=None, context=None, extra=None)ΒΆ

Bases: SanicException

Status: 417 Expectation Failed

Parameters:
  • message (str) –

  • status_code (int | None) –

  • quiet (bool | None) –

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

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

Return type:

None

exception sanic.exceptions.FileNotFound(message, path, relative_url)ΒΆ

Bases: NotFound

Status: 404 Not Found

Parameters:

message (str) –

exception sanic.exceptions.Forbidden(message=None, status_code=None, quiet=None, context=None, extra=None)ΒΆ

Bases: SanicException

Status: 403 Forbidden

Parameters:
  • message (str) –

  • status_code (int | None) –

  • quiet (bool | None) –

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

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

Return type:

None

sanic.exceptions.HeaderExpectationFailedΒΆ

alias of ExpectationFailed

exception sanic.exceptions.HeaderNotFound(message=None, status_code=None, quiet=None, context=None, extra=None)ΒΆ

Bases: BadRequest

Status: 400 Bad Request

Parameters:
  • message (str) –

  • status_code (int | None) –

  • quiet (bool | None) –

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

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

Return type:

None

exception sanic.exceptions.InvalidHeader(message=None, status_code=None, quiet=None, context=None, extra=None)ΒΆ

Bases: BadRequest

Status: 400 Bad Request

Parameters:
  • message (str) –

  • status_code (int | None) –

  • quiet (bool | None) –

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

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

Return type:

None

exception sanic.exceptions.InvalidRangeType(message, content_range)ΒΆ

Bases: RangeNotSatisfiable

Status: 416 Range Not Satisfiable

Parameters:

message (str) –

exception sanic.exceptions.InvalidSignal(message=None, status_code=None, quiet=None, context=None, extra=None)ΒΆ

Bases: SanicException

Parameters:
  • message (str) –

  • status_code (int | None) –

  • quiet (bool | None) –

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

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

Return type:

None

sanic.exceptions.InvalidUsageΒΆ

alias of BadRequest

exception sanic.exceptions.LoadFileException(message=None, status_code=None, quiet=None, context=None, extra=None)ΒΆ

Bases: SanicException

Parameters:
  • message (str) –

  • status_code (int | None) –

  • quiet (bool | None) –

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

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

Return type:

None

exception sanic.exceptions.MethodNotAllowed(message, method, allowed_methods)ΒΆ

Bases: SanicException

Status: 405 Method Not Allowed

Parameters:

message (str) –

sanic.exceptions.MethodNotSupportedΒΆ

alias of MethodNotAllowed

exception sanic.exceptions.NotFound(message=None, status_code=None, quiet=None, context=None, extra=None)ΒΆ

Bases: SanicException

Status: 404 Not Found

Parameters:
  • message (str) –

  • status_code (int | None) –

  • quiet (bool | None) –

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

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

Return type:

None

exception sanic.exceptions.PayloadTooLarge(message=None, status_code=None, quiet=None, context=None, extra=None)ΒΆ

Bases: SanicException

Status: 413 Payload Too Large

Parameters:
  • message (str) –

  • status_code (int | None) –

  • quiet (bool | None) –

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

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

Return type:

None

exception sanic.exceptions.PyFileError(file)ΒΆ

Bases: Exception

exception sanic.exceptions.RangeNotSatisfiable(message, content_range)ΒΆ

Bases: SanicException

Status: 416 Range Not Satisfiable

Parameters:

message (str) –

exception sanic.exceptions.RequestCancelledΒΆ

Bases: CancelledError

exception sanic.exceptions.RequestTimeout(message=None, status_code=None, quiet=None, context=None, extra=None)ΒΆ

Bases: SanicException

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.

Parameters:
  • message (str) –

  • status_code (int | None) –

  • quiet (bool | None) –

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

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

Return type:

None

exception sanic.exceptions.SanicException(message=None, status_code=None, quiet=None, context=None, extra=None)ΒΆ

Bases: Exception

Parameters:
  • message (str) –

  • status_code (int | None) –

  • quiet (bool | None) –

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

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

Return type:

None

exception sanic.exceptions.ServerError(message=None, status_code=None, quiet=None, context=None, extra=None)ΒΆ

Bases: SanicException

Status: 500 Internal Server Error

Parameters:
  • message (str) –

  • status_code (int | None) –

  • quiet (bool | None) –

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

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

Return type:

None

exception sanic.exceptions.ServerKilledΒΆ

Bases: Exception

exception sanic.exceptions.ServiceUnavailable(message=None, status_code=None, quiet=None, context=None, extra=None)ΒΆ

Bases: SanicException

Status: 503 Service Unavailable

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

Parameters:
  • message (str) –

  • status_code (int | None) –

  • quiet (bool | None) –

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

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

Return type:

None

exception sanic.exceptions.URLBuildError(message=None, status_code=None, quiet=None, context=None, extra=None)ΒΆ

Bases: ServerError

Status: 500 Internal Server Error

Parameters:
  • message (str) –

  • status_code (int | None) –

  • quiet (bool | None) –

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

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

Return type:

None

exception sanic.exceptions.Unauthorized(message, status_code=None, scheme=None, **kwargs)ΒΆ

Bases: SanicException

Status: 401 Unauthorized

Parameters:
  • message (str) – Message describing the exception.

  • status_code – HTTP Status code.

  • scheme – Name of the authentication scheme to be used.

When present, kwargs is used to complete the WWW-Authentication header.

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")
exception sanic.exceptions.WebsocketClosed(message=None, status_code=None, quiet=None, context=None, extra=None)ΒΆ

Bases: SanicException

Parameters:
  • message (str) –

  • status_code (int | None) –

  • quiet (bool | None) –

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

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

Return type:

None