Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP] Error handling #132

Draft
wants to merge 5 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions docker-compose.dev.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ services:
- "USAON_VTA_LOGIN_DISABLED=true"
# Use a local file DB instead of a remote one:
- "USAON_VTA_DB_SQLITE=true"
# Enable the in-browser debugger:
- "FLASK_DEBUG=True"
# Enable the in-browser debugger:
- "FLASK_DEBUG=true"


# Development DANGER ZONE: Do not use the below settings except for
Expand Down
17 changes: 16 additions & 1 deletion usaon_vta_survey/__init__.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import os
from typing import Final

from flask import Flask
from flask import Flask, render_template
from flask_bootstrap import Bootstrap5
from flask_sqlalchemy import SQLAlchemy
from sqlalchemy import MetaData
Expand All @@ -28,13 +28,28 @@
app = Flask(__name__)
app.config['SECRET_KEY'] = os.environ.get('FLASK_SECRET_KEY', 'youcanneverguess')
app.config['LOGIN_DISABLED'] = envvar_is_true("USAON_VTA_LOGIN_DISABLED")
# TODO: This should disable error handlers but it doesn't:
app.config['TESTING'] = envvar_is_true("USAON_VTA_TESTING")
app.config['SQLALCHEMY_DATABASE_URI'] = db_connstr(app)

db.init_app(app)
bootstrap = Bootstrap5(app)

app.jinja_env.globals.update(sqla_inspect=sqla_inspect, __version__=__version__)


@app.errorhandler(Exception)
def handle_exception(e):
"""Naively handle all exceptions the same way.

TODO: Not all exceptions should necessarily be handled the same way. Think harder.
Read docs: https://flask.palletsprojects.com/en/2.3.x/errorhandling/
TODO: Some way to link error messages users see with full tracebacks in logs. A
random request ID?
"""
return render_template("error.html", message=e)


# NOTE: This is a circular import, but it's specified by the Flask docs:
# https://flask.palletsprojects.com/en/3.1.x/patterns/packages/
import usaon_vta_survey.routes # noqa: E402, F401