Skip to content

ConfigError.message throws once its cause has been replaced, hiding the real message #10938

Done

Description

@RhysSullivan

Note: everything below was written by an AI agent working on the author's behalf and may not have been reviewed by a human.

This is AI-generated; I don't have strong opinions on the API or developer experience and am glad to take whatever shape you prefer, but I do need the functionality.

Versions

  • effect 4.1.4 through 4.1.6 (the latest release) and main: packages/effect/src/Config.ts, the message getter of ConfigError.
  • Seen with vitest 5.0.1. Node 24 and Bun 1.4 behave the same.

What happens

A test that fails with a ConfigError is reported as:

FAIL  repro.test.ts > a missing config value
ConfigError: <unserializable>: this.cause.toString is not a function
Caused by: SchemaError: Expected string

The error's own message (Expected string at ["PORT"]) is lost from the headline. Readers see a TypeError from Effect's source instead of the missing or invalid key.

Cause

ConfigError computes its message from its cause on every read:

export class ConfigError {
  readonly _tag = "ConfigError"
  readonly name: string = "ConfigError"
  readonly cause: SourceError | Schema.SchemaError
  constructor(cause: SourceError | Schema.SchemaError) {
    this.cause = cause
  }
  get message() {
    return this.cause.toString()
  }
  ...
}

Vitest's processError replaces every reported error's cause with its serialized copy before it serializes the error itself (err.cause = processError(err.cause, …)). The copy is a null-prototype object (Object.create(null)), so it has no toString. When Vitest then reads message while it serializes the ConfigError, the getter throws. Vitest catches that and reports <unserializable>: <the thrown message>.

Vitest is arguably at fault for rewriting cause. But the getter assumes cause always has a callable toString, so any code that copies, serializes or reassigns cause turns a readable config error into an unreadable one. SchemaError, by contrast, keeps its message.

Minimal reproduction

package.json:

{ "type": "module", "dependencies": { "effect": "4.1.6", "vitest": "5.0.1" } }

repro.test.ts, run with npx vitest run:

import { Config, ConfigProvider, Effect } from "effect"
import { test } from "vitest"

test("a missing config value", async () => {
  await Effect.runPromise(Config.String("PORT").parse(ConfigProvider.fromUnknown({})))
})

Without Vitest, the same failure in a plain script:

import { Cause, Config, ConfigProvider, Effect } from "effect"

const exit = Effect.runSyncExit(Config.String("PORT").parse(ConfigProvider.fromUnknown({})))
const error = Cause.squash((exit as any).cause) as any
console.log(error.message) // SchemaError(Expected string\n  at ["PORT"])
error.cause = Object.assign(Object.create(null), { _tag: "SchemaError", message: error.cause.message })
console.log(error.message) // TypeError: this.cause.toString is not a function

Expected

ConfigError.message keeps returning the cause's message (for example SchemaError(Expected string at ["PORT"])), whatever later happens to cause.

Possible fix

Compute the message once, when the error is made, instead of in a getter that reads cause again:

export class ConfigError {
  readonly _tag = "ConfigError"
  readonly name: string = "ConfigError"
  readonly cause: SourceError | Schema.SchemaError
  readonly message: string
  constructor(cause: SourceError | Schema.SchemaError) {
    this.cause = cause
    this.message = cause.toString()
  }
  toString() {
    return `ConfigError(${this.message})`
  }
}

One visible difference: message becomes an own property, so it also appears when the error is spread or passed to JSON.stringify. An alternative keeps the getter but guards it, falling back to the cause's message or String(cause) when toString is missing. I'll open a small PR with the first version; I'm happy to switch to the guard if you prefer it.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to 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