Skip to content

fix(config): throw typed EnvValidationError from validateEnv instead of process.exit - #228

Open
Jatinprajapati7869 wants to merge 1 commit into
vicharanashala:mainfrom
Jatinprajapati7869:fix/env-validator-typed-error
Open

fix(config): throw typed EnvValidationError from validateEnv instead of process.exit#228
Jatinprajapati7869 wants to merge 1 commit into
vicharanashala:mainfrom
Jatinprajapati7869:fix/env-validator-typed-error

Conversation

@Jatinprajapati7869

Copy link
Copy Markdown

What changed

validateEnv() no longer calls process.exit(1) inline on validation failure (finding M7 in docs/ISSUES.md). It now throws a typed EnvValidationError carrying every individual failure message in an errors: string[] payload. The boot boundary in server.ts catches this error, logs each failure in the exact same format as before, and exits with code 1 so the decision to terminate the process is owned by the entrypoint, not by a validation helper. This makes the validator unit-testable (previously any failing test killed the test runner) and safe for future non-boot callers. Boot behaviour is unchanged and proven by a new smoke test that runs the real entrypoint with an invalid environment and asserts exit code 1.

Related issue

Closes #227

Type of change

  • Bug fix
  • Feature
  • Refactor (no behaviour change)
  • Docs / comments only
  • CI / tooling

Area affected

  • Backend (Express / Mongoose)
  • Frontend (React / Vite)
  • Admin / Train tab (/admin/*)
  • Community (/community posts, comments, auto-answer)
  • Search (hybrid text retrieval, training stats)
  • Auth / middleware / samagama.in bridge
  • Crons / schedulers / embedding-warm
  • Observability (Sentry / logging / Discord alerts)
  • Docs

CI verification

  • cd apps/backend && npx tsc --noEmit exits 0
  • cd apps/backend && npx vitest run all tests pass (6 pre-existing failures on main, unrelated details below)
  • cd apps/frontend && npx tsc --noEmit exits 0
  • cd apps/frontend && npx vitest run all tests pass
  • pnpm run lint 0 errors (1 pre-existing error on main, unrelated details below)
  • GitHub Actions green on the merge commit (CI, CodeQL, Build & Deploy)
  • Tested with a real API hit or browser interaction if behaviour changed (booted the real server with missing env vars: exits 1 with identical error output)
  • Tests added or updated for the change
  • Single logical change unrelated fixes noted in description, not fixed here
  • Docs updated if route / API / env var / pipeline behaviour changed (no route/API/env var behaviour changed)
  • Rebased onto main, no merge commits

Notes for reviewer

Behaviour contract (unchanged): an invalid environment still prevents startup with exit code 1 and identical log output (Environment validation failed: + one - <error> line each). This is verified end-to-end by src/__tests__/server-boot.test.ts, which spawns the real src/server.ts via tsx with MONGODB_URI/JWT_SECRET removed nothing mocked.

API change (intentional): any future caller of validateEnv() now receives a catchable EnvValidationError instead of having its process killed. server.ts is currently the only caller.

Tests added (9): src/config/__tests__/envValidator.test.ts - missing/malformed MONGODB_URI, missing/short JWT_SECRET, non-numeric PORT, multiple failures aggregated into one error, valid env passes (8 tests). src/__tests__/server-boot.test.ts — boot-boundary smoke test (1 test).

Deliberately untested: the two-line rethrow of non-EnvValidationError exceptions in server.ts. Covering it would require fault-injection hooks in the validator, which conflicts with keeping this a single minimal change.

Pre-existing failures on main (not caused by this PR):

  • src/__tests__/journey-tracks.test.ts - 6 tests fail on a pristine clone of main (500 vs expected 200/404/400). Verified identical before/after this change.
  • pnpm run lint 1 pre-existing error on main (no-constant-condition in src/modules/ai/ai-client.service.ts:623). The four files in this PR lint with 0 errors and 0 warnings.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

validateEnv() calls process.exit(1) directly, untestable and unsafe outside the boot path

1 participant