Effect CommunityEC
Effect Community•3y ago•
34 replies
Nico

Matching on All Possible Errors in a Pipe with Multiple Effects

Hey guys,
happy to joint the community. 🙂

I was getting started with effect yesterday, but I did not understand, how to match on all errors that could happen in a pipe.

E.g. if I have three effects, op1, op2 and op3 and they all could be either a success or failure, but depend on the value the operation before returns. How could I match on all three possible errors?

The code below is not working, but I want to achive something like this, but access any errors that might happen on the way. Do you think it's possible? I am trying to avoid to run each effect on it's own and then see if it was a success or failure. That would be very similar to the logic I am replacing right now.

class FooError {
  readonly _tag = 'FooError';
}

class BarError {
  readonly _tag = 'BarError';
}

class BazError {
  readonly _tag = 'BazError';
}

const op1 = (n: number): Effect.Effect<never, FooError, number> =>
  n > 0.5 ? Effect.fail(new FooError()) : Effect.succeed(n - 0.1);

const op2 = (n: number): Effect.Effect<never, BarError, number> =>
  n > 0.5 ? Effect.fail(new BarError()) : Effect.succeed(n - 0.1);

const op3 = (n: number): Effect.Effect<never, BazError, number> =>
  n > 0.5 ? Effect.fail(new BazError()) : Effect.succeed(n);

const fooBarBaz = Random.next.pipe(
  op1,
  op2,
  op3,
)
Was this page helpful?