Skip to content

Commit 4eee650

Browse files
committed
fix: catch missing DB tables error with clear message in anonymous auth
Detect Postgres error code 42P01 (undefined_table) in ensureAnonymousUserExists() and throw an actionable error message instead of a generic internal server error. This helps developers quickly identify that database migrations need to be run.
1 parent 2b302ff commit 4eee650

1 file changed

Lines changed: 18 additions & 0 deletions

File tree

‎apps/sim/lib/auth/anonymous.ts‎

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,19 @@ const logger = createLogger('AnonymousAuth')
99

1010
let anonymousUserEnsured = false
1111

12+
function isMissingTableError(error: unknown): boolean {
13+
if (error && typeof error === 'object') {
14+
const code = (error as Record<string, unknown>).code
15+
if (code === '42P01') return true
16+
const cause = (error as Record<string, unknown>).cause
17+
if (cause && typeof cause === 'object') {
18+
const innerCode = (cause as Record<string, unknown>).code
19+
if (innerCode === '42P01') return true
20+
}
21+
}
22+
return false
23+
}
24+
1225
/**
1326
* Ensures the anonymous user and their stats record exist in the database.
1427
* Called when DISABLE_AUTH is enabled to ensure DB operations work.
@@ -47,6 +60,11 @@ export async function ensureAnonymousUserExists(): Promise<void> {
4760

4861
anonymousUserEnsured = true
4962
} catch (error) {
63+
if (isMissingTableError(error)) {
64+
throw new Error(
65+
'Database tables not found. Run database migrations before starting the app: bun run db:migrate'
66+
)
67+
}
5068
if (
5169
error instanceof Error &&
5270
(error.message.includes('unique') || error.message.includes('duplicate'))

0 commit comments

Comments
 (0)