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 Inf 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
- minimal()ο
Provide a formatted message that is meant to not show any sensitive data or details.
- Return type
- render()ο
Outputs the exception as a
HTTPResponse
.- Returns
The formatted exception
- Return type
str
- class sanic.errorpages.HTMLRenderer(request, exception, debug)ο
Bases:
sanic.errorpages.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
- minimal()ο
Provide a formatted message that is meant to not show any sensitive data or details.
- Return type
- class sanic.errorpages.JSONRenderer(request, exception, debug)ο
Bases:
sanic.errorpages.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
- minimal()ο
Provide a formatted message that is meant to not show any sensitive data or details.
- Return type
- class sanic.errorpages.TextRenderer(request, exception, debug)ο
Bases:
sanic.errorpages.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
- minimal()ο
Provide a formatted message that is meant to not show any sensitive data or details.
- Return type
- 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 (sanic.request.Request) β
exception (Exception) β
debug (bool) β
fallback (str) β
base (Type[sanic.errorpages.BaseRenderer]) β
renderer (Optional[Type[Optional[sanic.errorpages.BaseRenderer]]]) β
- Return type
sanic.exceptionsο
- exception sanic.exceptions.ContentRangeError(message, content_range)ο
Bases:
sanic.exceptions.SanicException
Status: 416 Range Not Satisfiable
- Parameters
message (str) β
- exception sanic.exceptions.FileNotFound(message, path, relative_url)ο
Bases:
sanic.exceptions.NotFound
Status: 404 Not Found
- Parameters
message (str) β
- exception sanic.exceptions.Forbidden(message=None, status_code=None, quiet=None, context=None, extra=None)ο
Bases:
sanic.exceptions.SanicException
Status: 403 Forbidden
- Parameters
message (str) β
status_code (Optional[int]) β
quiet (Optional[bool]) β
context (Optional[Dict[str, Any]]) β
extra (Optional[Dict[str, Any]]) β
- Return type
None
- exception sanic.exceptions.HeaderExpectationFailed(message=None, status_code=None, quiet=None, context=None, extra=None)ο
Bases:
sanic.exceptions.SanicException
Status: 417 Expectation Failed
- Parameters
message (str) β
status_code (Optional[int]) β
quiet (Optional[bool]) β
context (Optional[Dict[str, Any]]) β
extra (Optional[Dict[str, Any]]) β
- Return type
None
- exception sanic.exceptions.HeaderNotFound(message=None, status_code=None, quiet=None, context=None, extra=None)ο
Bases:
sanic.exceptions.InvalidUsage
Status: 400 Bad Request
- Parameters
message (str) β
status_code (Optional[int]) β
quiet (Optional[bool]) β
context (Optional[Dict[str, Any]]) β
extra (Optional[Dict[str, Any]]) β
- Return type
None
- exception sanic.exceptions.InvalidHeader(message=None, status_code=None, quiet=None, context=None, extra=None)ο
Bases:
sanic.exceptions.InvalidUsage
Status: 400 Bad Request
- Parameters
message (str) β
status_code (Optional[int]) β
quiet (Optional[bool]) β
context (Optional[Dict[str, Any]]) β
extra (Optional[Dict[str, Any]]) β
- Return type
None
- exception sanic.exceptions.InvalidRangeType(message, content_range)ο
Bases:
sanic.exceptions.ContentRangeError
Status: 416 Range Not Satisfiable
- Parameters
message (str) β
- exception sanic.exceptions.InvalidSignal(message=None, status_code=None, quiet=None, context=None, extra=None)ο
Bases:
sanic.exceptions.SanicException
- Parameters
message (str) β
status_code (Optional[int]) β
quiet (Optional[bool]) β
context (Optional[Dict[str, Any]]) β
extra (Optional[Dict[str, Any]]) β
- Return type
None
- exception sanic.exceptions.InvalidUsage(message=None, status_code=None, quiet=None, context=None, extra=None)ο
Bases:
sanic.exceptions.SanicException
Status: 400 Bad Request
- Parameters
message (str) β
status_code (Optional[int]) β
quiet (Optional[bool]) β
context (Optional[Dict[str, Any]]) β
extra (Optional[Dict[str, Any]]) β
- Return type
None
- exception sanic.exceptions.LoadFileException(message=None, status_code=None, quiet=None, context=None, extra=None)ο
Bases:
sanic.exceptions.SanicException
- Parameters
message (str) β
status_code (Optional[int]) β
quiet (Optional[bool]) β
context (Optional[Dict[str, Any]]) β
extra (Optional[Dict[str, Any]]) β
- Return type
None
- exception sanic.exceptions.MethodNotSupported(message, method, allowed_methods)ο
Bases:
sanic.exceptions.SanicException
Status: 405 Method Not Allowed
- Parameters
message (str) β
- exception sanic.exceptions.NotFound(message=None, status_code=None, quiet=None, context=None, extra=None)ο
Bases:
sanic.exceptions.SanicException
Status: 404 Not Found
- Parameters
message (str) β
status_code (Optional[int]) β
quiet (Optional[bool]) β
context (Optional[Dict[str, Any]]) β
extra (Optional[Dict[str, Any]]) β
- Return type
None
- exception sanic.exceptions.PayloadTooLarge(message=None, status_code=None, quiet=None, context=None, extra=None)ο
Bases:
sanic.exceptions.SanicException
Status: 413 Payload Too Large
- Parameters
message (str) β
status_code (Optional[int]) β
quiet (Optional[bool]) β
context (Optional[Dict[str, Any]]) β
extra (Optional[Dict[str, Any]]) β
- Return type
None
- exception sanic.exceptions.PyFileError(file)ο
Bases:
Exception
- exception sanic.exceptions.RequestTimeout(message=None, status_code=None, quiet=None, context=None, extra=None)ο
Bases:
sanic.exceptions.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 (Optional[int]) β
quiet (Optional[bool]) β
context (Optional[Dict[str, Any]]) β
extra (Optional[Dict[str, Any]]) β
- 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 (Optional[int]) β
quiet (Optional[bool]) β
context (Optional[Dict[str, Any]]) β
extra (Optional[Dict[str, Any]]) β
- Return type
None
- exception sanic.exceptions.ServerError(message=None, status_code=None, quiet=None, context=None, extra=None)ο
Bases:
sanic.exceptions.SanicException
Status: 500 Internal Server Error
- Parameters
message (str) β
status_code (Optional[int]) β
quiet (Optional[bool]) β
context (Optional[Dict[str, Any]]) β
extra (Optional[Dict[str, Any]]) β
- Return type
None
Bases:
sanic.exceptions.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 (Optional[int]) β
quiet (Optional[bool]) β
context (Optional[Dict[str, Any]]) β
extra (Optional[Dict[str, Any]]) β
- Return type
None
- exception sanic.exceptions.URLBuildError(message=None, status_code=None, quiet=None, context=None, extra=None)ο
Bases:
sanic.exceptions.ServerError
Status: 500 Internal Server Error
- Parameters
message (str) β
status_code (Optional[int]) β
quiet (Optional[bool]) β
context (Optional[Dict[str, Any]]) β
extra (Optional[Dict[str, Any]]) β
- Return type
None
- exception sanic.exceptions.Unauthorized(message, status_code=None, scheme=None, **kwargs)ο
Bases:
sanic.exceptions.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:
sanic.exceptions.SanicException
- Parameters
message (str) β
status_code (Optional[int]) β
quiet (Optional[bool]) β
context (Optional[Dict[str, Any]]) β
extra (Optional[Dict[str, Any]]) β
- Return type
None