DB error handlers in app/main.py raise NameError: psycopg2 is never imported
In app/main.py there are two exception handlers that reference psycopg2:
except psycopg2.errors.UndefinedObject as e: # line ~673
...
except psycopg2.Error as e: # line ~676
but psycopg2 is never imported in that module. Python evaluates the
exception class when the except clause is reached, so the moment a database
error actually occurs and control hits these handlers, they raise
NameError: name 'psycopg2' is not defined — masking the real database error
and turning a handled failure into an unhandled 500 with a misleading
traceback. Static analysis (ruff F821) flags both lines; caught while
auditing the repo, not at runtime.
Two possible fixes:
import psycopg2inapp/main.py(smallest change), or- catch SQLAlchemy's wrapped exceptions instead (
sqlalchemy.exc.ProgrammingError,sqlalchemy.exc.DBAPIError) if all DB access goes through SQLAlchemy — then the driver never needs importing here.
Happy to send either as a PR.
Disclosure: found by an AI agent (Claude) operated by @Llicklair, who reviewed this issue before posting.
In
app/main.pythere are two exception handlers that referencepsycopg2:except psycopg2.errors.UndefinedObject as e: # line ~673 ... except psycopg2.Error as e: # line ~676 but
psycopg2is never imported in that module. Python evaluates the exception class when the except clause is reached, so the moment a database error actually occurs and control hits these handlers, they raiseNameError: name 'psycopg2' is not defined— masking the real database error and turning a handled failure into an unhandled 500 with a misleading traceback. Static analysis (ruffF821) flags both lines; caught while auditing the repo, not at runtime.Two possible fixes:
import psycopg2inapp/main.py(smallest change), or- catch SQLAlchemy's wrapped exceptions instead (
sqlalchemy.exc.ProgrammingError,sqlalchemy.exc.DBAPIError) if all DB access goes through SQLAlchemy — then the driver never needs importing here.Happy to send either as a PR.
Disclosure: found by an AI agent (Claude) operated by @Llicklair, who reviewed this issue before posting.
Send PR.