Skip to content

_🛠️ Refactor suggestion_ | _🟠 Major_ #30

@chitcommit

Description

@chitcommit

In server/worker.ts around lines 15 to 20, the code blindly assigns required
secrets from env to process.env without validating them; add a validation step
that checks each required variable (DATABASE_URL, OPENAI_API_KEY,
CHITTYCONNECT_API_BASE, CHITTY_AUTH_SERVICE_TOKEN) exists on env, collect any
missing names, and if any are missing log a clear error (or throw) and exit with
non‑zero status so the worker fails fast with an explicit message rather than
producing cryptic errors later. Ensure the validation runs before assigning to
process.env and include the variable names in the error output. 🛠️ Refactor suggestion | 🟠 Major

Validate required environment variables before proceeding.

The code assumes all required secrets (DATABASE_URL, OPENAI_API_KEY, etc.) are present in the Worker environment but doesn't validate them. If any are undefined, the application will fail later with cryptic errors rather than failing fast with a clear message.

Apply this diff to add validation:

     try {
       // Set environment variables from Worker env
+      const requiredEnvVars = ['DATABASE_URL', 'OPENAI_API_KEY', 'CHITTYCONNECT_API_BASE', 'CHITTY_AUTH_SERVICE_TOKEN'];
+      const missing = requiredEnvVars.filter(key => !env[key]);
+      if (missing.length > 0) {
+        return new Response(
+          JSON.stringify({ error: 'Configuration Error', missing: missing }),
+          { status: 500, headers: { 'content-type': 'application/json' } }
+        );
+      }
+
       process.env.DATABASE_URL = env.DATABASE_URL;
       process.env.OPENAI_API_KEY = env.OPENAI_API_KEY;
🤖 Prompt for AI Agents
In server/worker.ts around lines 15 to 20, the code blindly assigns required
secrets from env to process.env without validating them; add a validation step
that checks each required variable (DATABASE_URL, OPENAI_API_KEY,
CHITTYCONNECT_API_BASE, CHITTY_AUTH_SERVICE_TOKEN) exists on env, collect any
missing names, and if any are missing log a clear error (or throw) and exit with
non‑zero status so the worker fails fast with an explicit message rather than
producing cryptic errors later. Ensure the validation runs before assigning to
process.env and include the variable names in the error output.

Originally posted by @coderabbitai[bot] in #19 (comment)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions