-
Notifications
You must be signed in to change notification settings - Fork 2
Fix API start script exit error #6
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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", | ||
|
Comment on lines
273
to
285
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Error Handling and RecoveryThe current logic exits the process immediately if environment variable validation fails. While this is robust, it may be beneficial to provide more granular error reporting or fallback mechanisms, especially in development or CI environments. For example, you could allow the process to continue with warnings in non-production environments, or provide a summary of missing variables for easier debugging. This would improve developer experience and maintainability. |
||
|
|
@@ -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", | ||
| ); | ||
|
Comment on lines
+293
to
+311
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. To improve readability and performance, you can consolidate the multiple console.error(
[
"\n╔════════════════════════════════════════════════════════════╗",
"║ ENVIRONMENT VARIABLE VALIDATION FAILED ║",
"╠════════════════════════════════════════════════════════════╣",
"║ The following environment variables have issues: ║",
"╚════════════════════════════════════════════════════════════╝\n",
errorMessages.join("\n"),
"\n[Help] Check .env file and Railway environment variables.\n",
].join("\n")
); |
||
|
|
||
| process.exit(1); | ||
| } | ||
|
|
||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For improved conciseness and robustness, you can use a regular expression to validate the
DATABASE_URLprotocol. This is a common pattern for this type of string validation and makes the intent clearer with less code, while also removing the need for a multi-line function body and comments.