Hyperlinkv0.8.0-beta.28

Result

Result.mapErrorconsteffect/Result.ts:816
<E, E2>(f: (err: E) => E2): <A>(self: Result<A, E>) => Result<A, E2>
<A, E, E2>(self: Result<A, E>, f: (err: E) => E2): Result<A, E2>

Transforms the failure channel of a Result, leaving the success channel unchanged.

When to use

Use to transform only the failure channel while preserving success values.

Details

  • If the result is a Failure, applies f to the error and returns a new Failure
  • If the result is a Success, returns it as-is

Example (Adding context to an error)

import { pipe, Result } from "effect"

const result = pipe(
  Result.fail("not found"),
  Result.mapError((e) => `Error: ${e}`)
)
console.log(result)
// Output: { _tag: "Failure", failure: "Error: not found", ... }
mappingmapmapBoth
Source effect/Result.ts:8168 lines
export const mapError: {
  <E, E2>(f: (err: E) => E2): <A>(self: Result<A, E>) => Result<A, E2>
  <A, E, E2>(self: Result<A, E>, f: (err: E) => E2): Result<A, E2>
} = dual(
  2,
  <A, E, E2>(self: Result<A, E>, f: (err: E) => E2): Result<A, E2> =>
    isFailure(self) ? fail(f(self.failure)) : succeed(self.success)
)
Referenced by 4 symbols