Hyperlinkv0.8.0-beta.28

Effect

Effect.catchNoSuchElementconsteffect/Effect.ts:3402
<A, E, R>(self: Effect<A, E, R>): Effect<
  Option<A>,
  Exclude<E, Cause.NoSuchElementError>,
  R
>

Catches NoSuchElementError failures and converts them to Option.none.

When to use

Use when you expect missing-value failures and want them to become an optional success while all other failures keep failing.

Details

Success values become Option.some, NoSuchElementError becomes Option.none, and all other errors are preserved.

Example (Recovering from missing Option values)

import { Effect, Option } from "effect"

const some = Effect.fromNullishOr(1).pipe(Effect.catchNoSuchElement)
const none = Effect.fromNullishOr(null).pipe(Effect.catchNoSuchElement)

Effect.runPromise(some).then(console.log) // { _id: 'Option', _tag: 'Some', value: 1 }
Effect.runPromise(none).then(console.log) // { _id: 'Option', _tag: 'None' }
Source effect/Effect.ts:34023 lines
export const catchNoSuchElement: <A, E, R>(
  self: Effect<A, E, R>
) => Effect<Option<A>, Exclude<E, Cause.NoSuchElementError>, R> = internal.catchNoSuchElement