Handling errors in Effect's `tryPromise` can be a bit tricky if you're not familiar with how Effe...

Hi, I'm struggling to handle properly an error thrown in a tryPromise.
export class PatriciaMerkleTreeDatasourceLive extends Effect.Service<PatriciaMerkleTreeDatasourceLive>()(
  "PatriciaMerkleTreeDatasourceLive",
  {
    dependencies: [PatriciaTrie.Default],
    effect: Effect.gen(function* () {
      const trie = yield* PatriciaTrie.trie;

      const insert = (address: string) =>
        Effect.gen(function* () {
          // return yield* new AlreadyExistsError({ address });
          yield* Effect.tryPromise({
            try: () => trie.insert(address, "0"),
            catch: () => new AlreadyExistsError({ address }),
          });
          return trie.hash.toString("hex");
        });

      return {
        insert,
      };
    }),
  },
) {}

If I return directly the error like in the commented line of the insert, I get the error I can evaluate in my tests. But if the error is return from the tryPromise, it seems to act as a thrown error, and I can't find a way to handle it in tests in an Effect's way. What bother me is that with the immediate return of the error, or the tryPromise code insert has the same error signature Effect.Effect<never ( or string), AlreadyExistsError, never >
Was this page helpful?