aboutsummaryrefslogtreecommitdiff
path: root/gn2/wqflask/app_errors.py
blob: c6081e0808623ba30d1bcda58878d53f68cfb54d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
"""Handle errors at the application's top-level"""
from flask import flash, redirect, current_app, render_template
from authlib.integrations.base_client.errors import InvalidTokenError

from gn2.wqflask.oauth2 import session
from gn2.wqflask.decorators import AuthorisationError

def handle_authorisation_error(exc: AuthorisationError):
    """Handle AuthorisationError if not handled anywhere else."""
    current_app.logger.error(exc)
    return render_template(
        "authorisation_error.html", error_type=type(exc).__name__, error=exc)

def handle_invalid_token_error(exc: InvalidTokenError):
    flash("An invalid session token was detected. "
          "You have been logged out of the system.",
          "alert-danger")
    session.clear_session_info()
    return redirect("/")

__handlers__ = {
    AuthorisationError: handle_authorisation_error,
    InvalidTokenError: handle_invalid_token_error
}

def register_error_handlers(app):
    """Register all error handlers."""
    for klass, handler in __handlers__.items():
        app.register_error_handler(klass, handler)

    return app