flexmeasures.utils.error_utils

Utils for handling of errors

Functions

flexmeasures.utils.error_utils.add_basic_error_handlers(app: Flask)

Register a generic error handler for all exceptions. See also the auth package for auth-specific error handling (Unauthorized, Forbidden)

flexmeasures.utils.error_utils.error_handling_router(error: Exception)

Generic handler for errors. We respond in JSON if the request content-type is JSON, if the request was made under /api (whether or not it matched a route, so that an unknown API URL gets a JSON 404, too), or if the error is a SecurityError. Otherwise, the ui package can define how it wants to render HTML errors, by setting a function.

Any exception that is not an HTTPException (e.g. a database error) is logged in full, and then answered as an InternalServerError. Its code attribute is not an HTTP status (SQLAlchemy errors carry codes like “f405”), which would otherwise end up in the response’s status line, and its message may reveal internals (e.g. SQL), which belong in the logs only.

flexmeasures.utils.error_utils.get_err_source_info(original_traceback=None) → dict

Use this when an error is handled to get info on where it occurred.

flexmeasures.utils.error_utils.log_error(exc: Exception, error_msg: str, verbose: bool = True)

Collect metadata about the exception and log it. error_msg comes in as an extra attribute because Exception implementations differ here.

flexmeasures.utils.error_utils.log_http_error(error: HTTPException, http_error_code: int)

Log an HTTPException, leaving out the traceback where it is not interesting.

flexmeasures.utils.error_utils.print_query(query: Query) → str

Print full SQLAlchemy query with compiled parameters.

Recommended use as developer tool only.

Adapted from https://stackoverflow.com/a/63900851/13775459