Hyperlinkv0.8.0-beta.28

Result

Result.getOrThrowWithconsteffect/Result.ts:1216
<E>(onFailure: (err: E) => unknown): <A>(self: Result<A, E>) => A
<A, E>(self: Result<A, E>, onFailure: (err: E) => unknown): A

Extracts the success value or throws a custom error derived from the failure.

When to use

Use when converting a Result into a thrown exception with a custom error message or error type.

Details

  • Success<A> returns A
  • Failure<E> throws the value returned by onFailure(e)

Example (Throwing a custom error)

import { Result } from "effect"

console.log(
  Result.getOrThrowWith(Result.succeed(1), () => new Error("fail"))
)
// Output: 1

// This would throw: new Error("Unexpected: oops")
// Result.getOrThrowWith(
//   Result.fail("oops"),
//   (err) => new Error(`Unexpected: ${err}`)
// )
Source effect/Result.ts:12169 lines
export const getOrThrowWith: {
  <E>(onFailure: (err: E) => unknown): <A>(self: Result<A, E>) => A
  <A, E>(self: Result<A, E>, onFailure: (err: E) => unknown): A
} = dual(2, <A, E>(self: Result<A, E>, onFailure: (err: E) => unknown): A => {
  if (isSuccess(self)) {
    return self.success
  }
  throw onFailure(self.failure)
})
Referenced by 1 symbols