Skip to main content

Standardised exception handlers for HMLR Flask applications

Project description

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)

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

landregistry_exception_handlers-0.9.9.tar.gz (4.3 kB view details)

Uploaded Source

Built Distribution

File details

Details for the file landregistry_exception_handlers-0.9.9.tar.gz.

File metadata

File hashes

Hashes for landregistry_exception_handlers-0.9.9.tar.gz
Algorithm Hash digest
SHA256 fc2b47b254cab421af14d5972da0b9cf8073748afd0650af3909af646c8f1508
MD5 71d9fef8126a1980dafeb675d2722457
BLAKE2b-256 3bbccea47c53d84bcae768937521f4e4132b41f48f2e7545c9c01a6658ef474b

See more details on using hashes here.

File details

Details for the file landregistry_exception_handlers-0.9.9-py3-none-any.whl.

File metadata

File hashes

Hashes for landregistry_exception_handlers-0.9.9-py3-none-any.whl
Algorithm Hash digest
SHA256 ae15d9c0612c7f44813d429f3b126776bd637a87b188a4683ae3588b2f7ca71e
MD5 c9627c14570b7441b8dea072dbc8e066
BLAKE2b-256 f13230e54685dde1b2ce64495c1ad705af3c32f4aed989ff1881902fecb9c7c5

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page