Skip to content

Commit

Permalink
Handle errors
Browse files Browse the repository at this point in the history
TODO: TESTING config setting isn't applying as expected
  • Loading branch information
mfisher87 committed Sep 3, 2023
1 parent 80a6d79 commit 1c681c9
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 3 deletions.
4 changes: 2 additions & 2 deletions docker-compose.dev.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,10 @@ services:
- "USAON_VTA_LOGIN_DISABLED=true"
# Use a local file DB instead of a remote one:
- "USAON_VTA_DB_SQLITE=true"
# Disable error handlers and allow exceptions to propagate:
- "USAON_VTA_TESTING=True"
# Enable the in-browser debugger:
- "FLASK_DEBUG=True"
# Disable error handlers and allow exceptions to propagate:
- "FLASK_TESTING=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

0 comments on commit 1c681c9

Please sign in to comment.