Errors

Every EnvaptError code, what throws it, and how to catch and narrow on it.

When envapt rejects a value or a configuration, it throws an EnvaptError. The error carries a numeric code from the EnvaptErrorCodes enum, so you can branch on the exact cause.

try {
    .('DATABASE_URL');
} catch () {
    if ( instanceof ) {
        .code;
EnvaptError.code: EnvaptErrorCodes
if (. === .) { .?.(() => .(.)); } } }

The error shape

EnvaptError extends Error with:

  • code: the EnvaptErrorCodes member.
  • name: set to `EnvaptError [<code>]` (e.g. EnvaptError [305]).
  • message: a human-readable description.
  • issues: the validator's issue list, populated only for SchemaValidationFailed (208); undefined for every other code.
  • cause: the original error when one is chained, used by SchemaThrew (209).

Codes

Fallback (101-105)

CodeNameThrown when
101InvalidFallbackA fallback value is invalid for the converter (for example, a non-array fallback for an array converter).
102InvalidFallbackTypeThe fallback value's type doesn't match the converter's return type.
103ArrayFallbackElementTypeMismatchAn array fallback contains an element of the wrong type.
104FallbackConverterTypeMismatchThe fallback type doesn't match the specified converter.
105MalformedTimeFallbackA time-string fallback isn't <integer><unit> (for example 90m).

Converter (201-209)

CodeNameThrown when
201InvalidArrayConverterTypeThe Converters.array configuration is malformed.
202InvalidBuiltInConverterAn unknown built-in converter token is used.
203InvalidCustomConverterA custom converter is not a function.
204InvalidConverterTypeThe converter type is not recognized.
205PrimitiveCoercionFailedCoercing a fallback through a primitive constructor fails.
206ArrayElementConversionFailedAn array element fails to convert to the configured element type.
207EmptyArrayElementStrict mode only: an array element is empty or whitespace.
208SchemaValidationFailedA Standard Schema returns issues for a non-empty value. The only code that populates issues.
209SchemaThrewA schema's validate itself throws. The original error is on cause.

Configuration and lookup (301-305)

CodeNameThrown when
301MissingDelimiterAn array converter is missing its delimiter.
302InvalidUserDefinedConfigA bad decorator/options combination: required with fallback, schema with converter, a non-StandardSchemaV1 schema, an async schema, or an invalid debug level.
303EnvFilesNotFoundA configured .env path (via envPaths or configureProfiles) doesn't exist.
304InvalidKeyInputNo key, a non-string key, or an empty-string key was passed.
305MissingEnvValueA required value is missing or empty: require, { required: true }, a schema with no fallback, or an unresolved template under strict mode.
NOTE

issues is set only for SchemaValidationFailed (208), so you can read error.issues after checking error.code === EnvaptErrorCodes.SchemaValidationFailed without a cast. See Standard Schema for where 208 and 209 come from.

On this page