Name | landregistry-exception-handlers JSON |
Version |
0.9.9
JSON |
| download |
home_page | None |
Summary | Standardised exception handlers for HMLR Flask applications |
upload_time | 2024-10-31 13:54:05 |
maintainer | None |
docs_url | None |
author | Ian Harvey |
requires_python | >=3.9 |
license | MIT |
keywords |
|
VCS |
|
bugtrack_url |
|
requirements |
No requirements were recorded.
|
Travis-CI |
No Travis.
|
coveralls test coverage |
No coveralls.
|
# Flask Exception Handlers
Defines an extension for standardising exception handling in HMLR's Flask applications.
## Basic Usage
Initialise as any Flask extension:
```python
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:
```python
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`:
```python
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:
```python
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:
```python
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)
```
Raw data
{
"_id": null,
"home_page": null,
"name": "landregistry-exception-handlers",
"maintainer": null,
"docs_url": null,
"requires_python": ">=3.9",
"maintainer_email": null,
"keywords": null,
"author": "Ian Harvey",
"author_email": "ian.harvey@landregistry.gov.uk",
"download_url": "https://files.pythonhosted.org/packages/3b/bc/cea47c53d84bcae768937521f4e4132b41f48f2e7545c9c01a6658ef474b/landregistry_exception_handlers-0.9.9.tar.gz",
"platform": null,
"description": "# Flask Exception Handlers\n\nDefines an extension for standardising exception handling in HMLR's Flask applications.\n\n## Basic Usage\n\nInitialise as any Flask extension:\n\n```python\nexception_handlers = ExceptionHandlers()\n\ndef register_extensions(app):\n exception_handlers.init_app(app)\n```\n\n## Custom Renderers\n\nIf 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:\n\n```python\ndef http_error_renderer(description, code, http_code, e=None):\n return render_template('app/errors/unhandled.html', http_code=http_code), http_code\n\n\ndef unhandled_error_renderer(description, code, http_code, e=None):\n return render_template('app/errors/unhandled.html', http_code=http_code), http_code\n\n\ndef application_error_renderer(description, code, http_code, e=None):\n try:\n return render_template('app/errors/application/{}.html'.format(e.code), description=e.message, code=e.code,\n http_code=http_code, e=e), http_code\n except TemplateNotFound:\n return render_template('app/errors/application.html', description=e.message, code=e.code, http_code=http_code),\n http_code\n\n\ndef register_exception_handlers(app):\n handlers = ExceptionHandlers()\n handlers.init_app(app)\n handlers.on_http_error_render = http_error_renderer\n handlers.on_application_error_render = application_error_renderer\n handlers.on_unhandled_error_render = unhandled_error_renderer\n```\n\n## Suppressing HTTP Response Codes\n\nWant your app to only send certain HTTP response codes? Supply the list of **allowed** codes to `init_app`:\n\n```python\nhandlers = ExceptionHandlers()\nhandlers.init_app(app, [403, 404])\n# Or...\nhandlers = ExceptionHandlers(app=app, allow_codes=[418])\n```\n\n(Anything not in the list gets replaced with a 500).\n\nThe helper list `ALLOWED_UI_RESPONSES` (403, 404, 429 and 500) is available:\n\n```python\nfrom landregistry.exceptions import ExceptionHandlers, ALLOWED_UI_RESPONSES\n\nhandlers = ExceptionHandlers()\nhandlers.init_app(app, ALLOWED_UI_RESPONSES)\n```\n\n## ApplicationError\n\nUse this class when the application identifies there's been a problem and the client should be informed.\n\n*exception* **ApplicationError**(*message: str, error_code: str, http_code: int, force_logging: bool*)\n\n`message` The text of the exception message.\n\n`error_code` Unique identifier for the error. Can be anything, but useful to reference in support documentation or\nknowledge items.\n\n`http_code` Defaults to 500. Specifies the HTTP response code to send.\n\n`force_logging` Defaults to False. Forces logging to Info if True (Debug otherwise). If the http code is 500, this\nparameter is ignored (and logging is to Error).\n\nExamples:\n\n```python\nfrom landregistry.exceptions import ApplicationError\n\nraise ApplicationError(\"Critical error\", \"DB\")\n# or\nraise ApplicationError(\"Title number invalid\", \"E102\", http_code=400)\n# or\nraise ApplicationError(\"Title number invalid\", \"E102\", http_code=400, force_logging=True)\n```\n",
"bugtrack_url": null,
"license": "MIT",
"summary": "Standardised exception handlers for HMLR Flask applications",
"version": "0.9.9",
"project_urls": null,
"split_keywords": [],
"urls": [
{
"comment_text": "",
"digests": {
"blake2b_256": "f13230e54685dde1b2ce64495c1ad705af3c32f4aed989ff1881902fecb9c7c5",
"md5": "c9627c14570b7441b8dea072dbc8e066",
"sha256": "ae15d9c0612c7f44813d429f3b126776bd637a87b188a4683ae3588b2f7ca71e"
},
"downloads": -1,
"filename": "landregistry_exception_handlers-0.9.9-py3-none-any.whl",
"has_sig": false,
"md5_digest": "c9627c14570b7441b8dea072dbc8e066",
"packagetype": "bdist_wheel",
"python_version": "py3",
"requires_python": ">=3.9",
"size": 5380,
"upload_time": "2024-10-31T13:54:03",
"upload_time_iso_8601": "2024-10-31T13:54:03.024745Z",
"url": "https://files.pythonhosted.org/packages/f1/32/30e54685dde1b2ce64495c1ad705af3c32f4aed989ff1881902fecb9c7c5/landregistry_exception_handlers-0.9.9-py3-none-any.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": "",
"digests": {
"blake2b_256": "3bbccea47c53d84bcae768937521f4e4132b41f48f2e7545c9c01a6658ef474b",
"md5": "71d9fef8126a1980dafeb675d2722457",
"sha256": "fc2b47b254cab421af14d5972da0b9cf8073748afd0650af3909af646c8f1508"
},
"downloads": -1,
"filename": "landregistry_exception_handlers-0.9.9.tar.gz",
"has_sig": false,
"md5_digest": "71d9fef8126a1980dafeb675d2722457",
"packagetype": "sdist",
"python_version": "source",
"requires_python": ">=3.9",
"size": 4318,
"upload_time": "2024-10-31T13:54:05",
"upload_time_iso_8601": "2024-10-31T13:54:05.449087Z",
"url": "https://files.pythonhosted.org/packages/3b/bc/cea47c53d84bcae768937521f4e4132b41f48f2e7545c9c01a6658ef474b/landregistry_exception_handlers-0.9.9.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2024-10-31 13:54:05",
"github": false,
"gitlab": false,
"bitbucket": false,
"codeberg": false,
"lcname": "landregistry-exception-handlers"
}