Skip to content

Viewer shows generic 'Unexpected error' when database is unreachable #92

@GeiserX

Description

@GeiserX

Problem

When PostgreSQL is down or unreachable, the viewer login endpoint returns a generic 500 "Internal server error" which the frontend displays as "Unexpected error, please try again". This gives no indication that the database is the problem.

The root cause is that db.get_viewer_by_username() at main.py:769 (and similar DB calls throughout the login flow) have no connection error handling. When the DB connection fails, the unhandled asyncpg/aiosqlite exception propagates up as a 500.

Expected Behavior

  • Login should return 503 Service Unavailable with a clear message like "Database temporarily unavailable"
  • The master account (env var credentials) should still work even when the DB is down (fall through to env var check)
  • The frontend should display a user-friendly message distinguishing DB issues from auth failures

Affected Endpoints

The login() function is the most visible, but all endpoints that hit the DB without catching connection errors are affected:

  • POST /api/login — viewer account lookup fails
  • POST /auth/token — token lookup fails
  • GET /api/auth/check — session restore fails (already has a warning log but no user-facing message)
  • All admin CRUD endpoints

Context

Discovered when PostgreSQL containers on Unraid were in "Created" state after a kernel panic. The viewers were running but couldn't reach postgres, causing all login attempts to fail with unhelpful errors.

Suggested Fix

  1. Catch DB connection errors in login() and fall through to master credential check
  2. Return 503 with specific message for DB-dependent operations when connection fails
  3. Add a health check endpoint (/api/health) that reports DB connectivity status

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions