Flask Exception Handlers
Defines an extension for standardising exception handling in HMLR's Flask applications.
Basic Usage
Initialise as any Flask extension:
exception_handlers = ExceptionHandlers()
def register_extensions(app):
exception_handlers.init_app(app)
Custom Renderers
If you need to change how the errors are returned to the client, use the on_http_error_render, on_application_error_render and on_unhandled_error_render event handlers:
def http_error_renderer(description, code, http_code, e=None):
return render_template('app/errors/unhandled.html', http_code=http_code), http_code
def unhandled_error_renderer(description, code, http_code, e=None):
return render_template('app/errors/unhandled.html', http_code=http_code), http_code
def application_error_renderer(description, code, http_code, e=None):
try:
return render_template('app/errors/application/{}.html'.format(e.code), description=e.message, code=e.code,
http_code=http_code, e=e), http_code
except TemplateNotFound:
return render_template('app/errors/application.html', description=e.message, code=e.code, http_code=http_code),
http_code
def register_exception_handlers(app):
handlers = ExceptionHandlers()
handlers.init_app(app)
handlers.on_http_error_render = http_error_renderer
handlers.on_application_error_render = application_error_renderer
handlers.on_unhandled_error_render = unhandled_error_renderer
Suppressing HTTP Response Codes
Want your app to only send certain HTTP response codes? Supply the list of allowed codes to init_app:
handlers = ExceptionHandlers()
handlers.init_app(app, [403, 404])
# Or...
handlers = ExceptionHandlers(app=app, allow_codes=[418])
(Anything not in the list gets replaced with a 500).
The helper list ALLOWED_UI_RESPONSES (403, 404, 429 and 500) is available:
from landregistry.exceptions import ExceptionHandlers, ALLOWED_UI_RESPONSES
handlers = ExceptionHandlers()
handlers.init_app(app, ALLOWED_UI_RESPONSES)
ApplicationError
Use this class when the application identifies there's been a problem and the client should be informed.
exception ApplicationError(message: str, error_code: str, http_code: int, force_logging: bool)
message The text of the exception message.
error_code Unique identifier for the error. Can be anything, but useful to reference in support documentation or
knowledge items.
http_code Defaults to 500. Specifies the HTTP response code to send.
force_logging Defaults to False. Forces logging to Info if True (Debug otherwise). If the http code is 500, this
parameter is ignored (and logging is to Error).
Examples:
from landregistry.exceptions import ApplicationError
raise ApplicationError("Critical error", "DB")
# or
raise ApplicationError("Title number invalid", "E102", http_code=400)
# or
raise ApplicationError("Title number invalid", "E102", http_code=400, force_logging=True)
Metadata
Release files for landregistry-exception-handlers 0.9.9
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| landregistry_exception_handlers-0.9.9.tar.gz | 4.3 kB | Without provenance | Details for landregistry_exception_handlers-0.9.9.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| landregistry_exception_handlers-0.9.9-py3-none-any.whl | Python 3 | none | any | Details for landregistry_exception_handlers-0.9.9-py3-none-any.whl |
Total release size: 9.7 kB
Release files / landregistry_exception_handlers-0.9.9.tar.gz
| Download URL | landregistry_exception_handlers-0.9.9.tar.gz |
|---|---|
| Size | 4.3 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
fc2b47b254cab421af14d5972da0b9cf8073748afd0650af3909af646c8f1508
|
|
BLAKE2b-256 checksum How to use checksums |
3bbccea47c53d84bcae768937521f4e4132b41f48f2e7545c9c01a6658ef474b
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/5.1.1 CPython/3.10.12
|
Release files / landregistry_exception_handlers-0.9.9-py3-none-any.whl
| Download URL | landregistry_exception_handlers-0.9.9-py3-none-any.whl |
|---|---|
| Size | 5.4 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
ae15d9c0612c7f44813d429f3b126776bd637a87b188a4683ae3588b2f7ca71e
|
|
BLAKE2b-256 checksum How to use checksums |
f13230e54685dde1b2ce64495c1ad705af3c32f4aed989ff1881902fecb9c7c5
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/5.1.1 CPython/3.10.12
|
Security for landregistry-exception-handlers 0.9.9
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.