Effect CommunityEC
Effect Community3y ago
15 replies
Adam J

Migrating from fp-ts: Recovering from Errors in a Pipeline

Hi there, I'm new to Effect and looking to migrate from fp-ts. One approach of using fp-ts I would rely on is to recover from errors but ultimately fail the overall pipeline. A contrived example:

const somePipeline = (newUser): Result<Error, User> => pipe(
  TE.of(newUser),
  TE.flatMap(createUserInDB),
  TE.flatMap(someOtherAsyncStepThatMightFail),
  TE.orElse((originalError) => pipe(
    removeUserFromDBOnFail(newUser.email),
    // if we successfully removed user then return original error else return the error why we couldn't recover
    TE.map(() => originalError),
    TE.toUnion,
  )),
  // Result<E, never> or Result<never, User>
  TE.bimap(asFailure, asSuccess),
  // We remained on the left track even after successfully recovering by deleting the user we just added.
  TE.toUnion,
)


I was hoping something would be similar with Effect but I couldn't find a solution using Effect.catchTags or Effect.orElseFail since I get a nested promise and using Effect.runPromise doesn't help. Any suggestions would be much appreciated.
Was this page helpful?