From 8965aeff25c8b69644afcd11a21425f102f3ca2a Mon Sep 17 00:00:00 2001 From: Claude Date: Sun, 23 Nov 2025 22:40:40 +0000 Subject: [PATCH] fix: Improve DATABASE_URL validation and error logging - Fix DATABASE_URL validation to accept postgres:// and postgresql:// protocols (Zod's .url() validator rejects non-standard protocols) - Add clear console error output showing which env vars failed validation - Make it easier to debug Railway deployment env var issues --- apps/core/server/config/env.ts | 49 ++++++++++++++++++++++++++++++++-- 1 file changed, 47 insertions(+), 2 deletions(-) diff --git a/apps/core/server/config/env.ts b/apps/core/server/config/env.ts index 0cb3aa2..8d2ab2c 100644 --- a/apps/core/server/config/env.ts +++ b/apps/core/server/config/env.ts @@ -24,7 +24,20 @@ const envSchema = z.object({ // ========================================= // Database // ========================================= - DATABASE_URL: z.string().url("DATABASE_URL must be a valid PostgreSQL URL"), + DATABASE_URL: z + .string() + .min(1, "DATABASE_URL is required") + .refine( + (val) => { + // Accept postgres:// or postgresql:// connection strings + // These are valid connection strings but may not pass standard URL validation + return val.startsWith("postgres://") || val.startsWith("postgresql://"); + }, + { + message: + "DATABASE_URL must be a valid PostgreSQL connection string (postgres:// or postgresql://)", + }, + ), // ========================================= // Authentication (Required for API server, optional for workers) @@ -147,7 +160,10 @@ const envSchema = z.object({ .optional() .transform((val) => { if (!val) return []; - return val.split(",").map((origin) => origin.trim()).filter(Boolean); + return val + .split(",") + .map((origin) => origin.trim()) + .filter(Boolean); }), FRONTEND_URL: z .string() @@ -257,6 +273,13 @@ const envSchema = z.object({ const parsed = envSchema.safeParse(process.env); if (!parsed.success) { + // Format errors in a human-readable way for console output + const errorMessages = parsed.error.issues.map((issue) => { + const path = issue.path.join("."); + return ` - ${path}: ${issue.message}`; + }); + + // Log structured data for log aggregation systems logger.error( { validation: "failed", @@ -265,6 +288,28 @@ if (!parsed.success) { }, "Environment variable validation failed", ); + + // Also print to console for Railway logs visibility + console.error( + "\n╔════════════════════════════════════════════════════════════╗", + ); + console.error( + "║ ENVIRONMENT VARIABLE VALIDATION FAILED ║", + ); + console.error( + "╠════════════════════════════════════════════════════════════╣", + ); + console.error( + "║ The following environment variables have issues: ║", + ); + console.error( + "╚════════════════════════════════════════════════════════════╝\n", + ); + console.error(errorMessages.join("\n")); + console.error( + "\n[Help] Check .env file and Railway environment variables.\n", + ); + process.exit(1); }