Test Code Not Catching Error with Effect.catchAll
A effect noob, and not sure why this test code does not catch the error. Would appreciate any help/insight:
import { Data, Effect } from "effect"
import assert from "node:assert"
import { describe, it } from "node:test"
// Corrected TestError
export class TestError extends Data.TaggedError("TestError")<{
message: string
cause?: unknown
_stack?: string[]
}> {
constructor(details: { message: string; cause?: unknown; _stack?: string | string[] }) {
const _stack = Array.isArray(details._stack)
? details._stack
: typeof details._stack === "string"
? details._stack.split("\n")
: (new Error()).stack?.split("\n").slice(1) ?? []
super({
message: details.message,
cause: details.cause,
_stack
})
}
}
const delayedError = Effect.gen(function*() {
yield* Effect.sleep(100) // Simulate delay
console.log("Throwing TestError...")
throw new TestError({ message: "Simulated error" })
})
describe("Effect.catchAll Minimal Example", () => {
it("should handle errors from an asynchronous Effect", async () => {
// Simulate an asynchronous operation that throws an error
// Use Effect.catchAll to handle the error
const result = await Effect.runPromise(
Effect.catchAll(
delayedError,
(error: TestError) => {
// Log and assert the error
console.log("Caught error:", error)
assert.ok(error instanceof TestError, "Error is not an instance of TestError")
return Effect.succeed("Fallback value")
}
)
)
console.log("completed test")
})
})import { Data, Effect } from "effect"
import assert from "node:assert"
import { describe, it } from "node:test"
// Corrected TestError
export class TestError extends Data.TaggedError("TestError")<{
message: string
cause?: unknown
_stack?: string[]
}> {
constructor(details: { message: string; cause?: unknown; _stack?: string | string[] }) {
const _stack = Array.isArray(details._stack)
? details._stack
: typeof details._stack === "string"
? details._stack.split("\n")
: (new Error()).stack?.split("\n").slice(1) ?? []
super({
message: details.message,
cause: details.cause,
_stack
})
}
}
const delayedError = Effect.gen(function*() {
yield* Effect.sleep(100) // Simulate delay
console.log("Throwing TestError...")
throw new TestError({ message: "Simulated error" })
})
describe("Effect.catchAll Minimal Example", () => {
it("should handle errors from an asynchronous Effect", async () => {
// Simulate an asynchronous operation that throws an error
// Use Effect.catchAll to handle the error
const result = await Effect.runPromise(
Effect.catchAll(
delayedError,
(error: TestError) => {
// Log and assert the error
console.log("Caught error:", error)
assert.ok(error instanceof TestError, "Error is not an instance of TestError")
return Effect.succeed("Fallback value")
}
)
)
console.log("completed test")
})
})