Repository navigation
config: fail fast with a readable error when environment validation fails - #78
Merged
joelpeace48-cell merged 1 commit intoAug 29, 2026
Merged
Conversation
…ails
src/config/env.ts called schema.parse(process.env) at module load. On
failure this threw a raw ZodError — a wall of JSON at import time,
before any logger exists, with no clear indication of which variable
was wrong or what was expected.
- parseEnv now uses safeParse and, on failure, throws a formatted
Error instead: one line per invalid variable (" - VARNAME: reason"),
never the raw ZodError dump. It still throws synchronously (callers
and existing tests that assert on `parseEnv` rejecting a bad env
are unaffected), just with a readable message instead of a JSON
blob.
- Never echoes the offending value. Every Zod issue code this schema
actually produces (invalid_type, invalid_string, our own `custom`
issues) already keeps its default message value-free — the one
exception is invalid_enum_value, whose default message embeds the
bad value verbatim ("received 'x'"), so that one gets a synthesized
message ("must be one of: ...") instead.
- The module-level load (`loadEnv`) catches that error, logs it via
console.error, and calls process.exit(1) — a clear message and a
non-zero exit, instead of an uncaught-exception stack trace.
- redactedConfig() logs the resolved *effective* configuration at
boot (skipped under NODE_ENV=test, since every test in this repo
imports this module) so an operator can confirm what the process
actually loaded. Only SIGNING_ENV_SECRET_KEY — the one field that's
genuine secret material — is redacted; SIGNING_KMS_KEY_ID is a
reference to a key, not the key itself, so it's logged as-is.
## Testing
src/config/env.exit.test.ts exercises the actual subprocess exit
behavior (`loadEnv()`'s `process.exit(1)` can't be triggered
in-process without killing the test runner): a valid environment
exits 0 and logs the resolved config with the secret redacted; an
invalid one exits non-zero with a readable per-variable message and
no raw ZodError/JSON dump; and a deliberately "secret-shaped" bad
enum value is confirmed never to appear in the output.
`npx tsc --noEmit`, `npm run build`, `npm test` (69/69), and `npm
audit --audit-level=high` are all clean.
Closes FinesseStudioLab#3.
Contributor
|
Nice work @Codeinx |
thebabalola
added a commit
to thebabalola/modeltrace-backend
that referenced
this pull request
Aug 29, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
src/config/env.tscallsschema.parse(process.env)at module load. On failure this throws a rawZodError— a wall of JSON at import time, before the logger exists, with no indication of which variable is wrong or what was expected.What this does
parseEnvnow usessafeParseand, on failure, throws a formattedErrorinstead: one line per invalid variable (- VARNAME: reason), never the raw ZodError dump. It still throws synchronously (existing callers/tests that assert onparseEnvrejecting a bad env are unaffected), just with a readable message instead of a JSON blob.invalid_type,invalid_string, our owncustomissues) already keeps its default message value-free — the one exception isinvalid_enum_value, whose default message embeds the bad value verbatim ("received 'x'"), so that one gets a synthesized message ("must be one of: ...") instead.loadEnv) catches that error, logs it viaconsole.error, and callsprocess.exit(1)— a clear message and a non-zero exit, instead of an uncaught-exception stack trace.redactedConfig()logs the resolved effective configuration at boot (skipped underNODE_ENV=test, since every test in this repo imports this module) so an operator can confirm what the process actually loaded. OnlySIGNING_ENV_SECRET_KEY— the one field that's genuine secret material — is redacted;SIGNING_KMS_KEY_IDis a reference to a key, not the key itself, so it's logged as-is.Testing
src/config/env.exit.test.tsexercises the actual subprocess exit behavior (loadEnv()'sprocess.exit(1)can't be triggered in-process without killing the test runner): a valid environment exits 0 and logs the resolved config with the secret redacted; an invalid one exits non-zero with a readable per-variable message and no raw ZodError/JSON dump; and a deliberately "secret-shaped" bad enum value is confirmed never to appear in the output.npx tsc --noEmit,npm run build,npm test(69/69), andnpm audit --audit-level=highare all clean.Scope note
Touches only
src/config/env.ts(plus the new test file). No other currently-open PR touches this file.Closes #3.