Repository navigation
Description
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
effect4.1.4 through 4.1.6 (the latest release) andmain:packages/effect/src/Config.ts, themessagegetter ofConfigError.- Seen with
vitest5.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 functionExpected
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