effect icon indicating copy to clipboard operation
effect copied to clipboard

Possibility to handle remaining errors in `catchTags`

Open akoenig opened this issue 8 months ago • 1 comments

Type

  • [ ] Refactor
  • [x] Feature
  • [ ] Bug Fix
  • [ ] Optimization
  • [ ] Documentation Update

Description

Sometimes, it’s necessary to recover from specific errors in catchTags, while still handling all other possible errors in a general way.

This PR is based on this Discord thread and introduces a possible function as a second parameter to catchTags, allowing you to handle all the other errors that are not explicitly specified.

An example:

import { Data, Effect } from "effect"

class A extends Data.TaggedError("A") {}
class B extends Data.TaggedError("B") {}
class C extends Data.TaggedError("C") {}
class D extends Data.TaggedError("D") {}

const program = Effect.gen(function* () {
  const types = [A, B, C, D];
  const Exception = types[Math.floor(Math.random() * types.length)];

  return yield* new Exception
}).pipe(
  Effect.catchTags(
    {
      A: (a) => Effect.succeed(a._tag),
      B: (b) => Effect.succeed(b._tag),
    },
    remaining => Effect.succeed(`remaining: ${remaining._tag}`) // C | D
  )
);

akoenig avatar Apr 23 '25 17:04 akoenig

🦋 Changeset detected

Latest commit: 76e2fdadb4082682304ea145b60396fef20db218

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 32 packages
Name Type
effect Minor
@effect/cli Major
@effect/cluster Major
@effect/experimental Major
@effect/opentelemetry Major
@effect/platform-browser Major
@effect/platform-bun Major
@effect/platform-node-shared Major
@effect/platform-node Major
@effect/platform Major
@effect/printer-ansi Major
@effect/printer Major
@effect/rpc Major
@effect/sql-clickhouse Major
@effect/sql-d1 Major
@effect/sql-drizzle Major
@effect/sql-kysely Major
@effect/sql-libsql Major
@effect/sql-mssql Major
@effect/sql-mysql2 Major
@effect/sql-pg Major
@effect/sql-sqlite-bun Major
@effect/sql-sqlite-do Major
@effect/sql-sqlite-node Major
@effect/sql-sqlite-react-native Major
@effect/sql-sqlite-wasm Major
@effect/sql Major
@effect/typeclass Major
@effect/vitest Major
@effect/ai Major
@effect/ai-anthropic Major
@effect/ai-openai Major

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

changeset-bot[bot] avatar Apr 23 '25 17:04 changeset-bot[bot]