Template error handler
    Renders a template for the error code if exception is know by werkzeug.
    Will attempt to load a template from userland app templates and then
    fall back to boiler templates if not found.
    :param exception: Exception
    :return:

def template_error_handler(exception):
    """
    Template error handler
    Renders a template for the error code if exception is know by werkzeug.
    Will attempt to load a template from userland app templates and then
    fall back to boiler templates if not found.
    :param exception: Exception
    :return:
    """
    http_exception = isinstance(exception, exceptions.HTTPException)
    code = exception.code if http_exception else 500

    # log exceptions only (app debug should be off)
    if code == 500:
        current_app.logger.error(exception)

    template = 'errors/{}.j2'.format(code)
    return render_template(template, error=exception), code