pdfLLM icon indicating copy to clipboard operation
pdfLLM copied to clipboard

DB error handlers in app/main.py raise NameError: psycopg2 is never imported

Open Llicklair opened this issue 2 months ago • 1 comments

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:

  1. import psycopg2 in app/main.py (smallest change), or
  2. 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.

Llicklair avatar Sep 13 '26 03:09 Llicklair

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:

  1. import psycopg2 in app/main.py (smallest change), or
  2. 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.

ikantkode avatar Oct 31 '26 15:10 ikantkode