Hyperlinkv0.8.0-beta.28

Channel

Channel.unwrapReasonconsteffect/Channel.ts:5485
<K extends TagsWithReason<OutErr>, OutErr>(errorTag: K): <
  OutElem,
  OutDone,
  InElem,
  InErr,
  InDone,
  Env
>(
  self: Channel<OutElem, OutErr, OutDone, InElem, InErr, InDone, Env>
) => Channel<
  OutElem,
  | Types.ExcludeTag<OutErr, K>
  | Types.ReasonOf<Types.ExtractTag<OutErr, K>>,
  OutDone,
  InElem,
  InErr,
  InDone,
  Env
>
<
  OutElem,
  OutErr,
  OutDone,
  InElem,
  InErr,
  InDone,
  Env,
  K extends TagsWithReason<OutErr>
>(
  self: Channel<OutElem, OutErr, OutDone, InElem, InErr, InDone, Env>,
  errorTag: K
): Channel<
  OutElem,
  | Types.ExcludeTag<OutErr, K>
  | Types.ReasonOf<Types.ExtractTag<OutErr, K>>,
  OutDone,
  InElem,
  InErr,
  InDone,
  Env
>

Promotes nested reason errors into the channel error, replacing the parent error.

Example (Promoting nested reasons)

import { Channel, Data } from "effect"

class RateLimitError extends Data.TaggedError("RateLimitError")<{
  retryAfter: number
}> {}

class QuotaExceededError extends Data.TaggedError("QuotaExceededError")<{
  limit: number
}> {}

class AiError extends Data.TaggedError("AiError")<{
  reason: RateLimitError | QuotaExceededError
}> {}

const channel = Channel.fail(
  new AiError({ reason: new RateLimitError({ retryAfter: 60 }) })
)

const unwrapped = channel.pipe(Channel.unwrapReason("AiError"))
error handling
Source effect/Channel.ts:548565 lines
export const unwrapReason: {
  <
    K extends TagsWithReason<OutErr>,
    OutErr
  >(
    errorTag: K
  ): <OutElem, OutDone, InElem, InErr, InDone, Env>(
    self: Channel<OutElem, OutErr, OutDone, InElem, InErr, InDone, Env>
  ) => Channel<
    OutElem,
    Types.ExcludeTag<OutErr, K> | Types.ReasonOf<Types.ExtractTag<OutErr, K>>,
    OutDone,
    InElem,
    InErr,
    InDone,
    Env
  >
  <
    OutElem,
    OutErr,
    OutDone,
    InElem,
    InErr,
    InDone,
    Env,
    K extends TagsWithReason<OutErr>
  >(
    self: Channel<OutElem, OutErr, OutDone, InElem, InErr, InDone, Env>,
    errorTag: K
  ): Channel<
    OutElem,
    Types.ExcludeTag<OutErr, K> | Types.ReasonOf<Types.ExtractTag<OutErr, K>>,
    OutDone,
    InElem,
    InErr,
    InDone,
    Env
  >
} = dual(2, <
  OutElem,
  OutErr,
  OutDone,
  InElem,
  InErr,
  InDone,
  Env,
  K extends TagsWithReason<OutErr>
>(
  self: Channel<OutElem, OutErr, OutDone, InElem, InErr, InDone, Env>,
  errorTag: K
): Channel<
  OutElem,
  Types.ExcludeTag<OutErr, K> | Types.ReasonOf<Types.ExtractTag<OutErr, K>>,
  OutDone,
  InElem,
  InErr,
  InDone,
  Env
> =>
  catchFilter(
    self,
    (error) =>
      isTagged(error, errorTag) && hasProperty(error, "reason") ? Result.succeed(error.reason) : Result.fail(error),
    fail
  ) as any)