Effect CommunityEC
Effect Community2mo ago
10 replies
m9tdev

Handling Known Errors in Effect Transactions with Prisma

I'm trying to add known errors to effect-prisma-generator, but I'm running into an issue for transactions. I'm currently having this:

export class PrismaService extends Service<PrismaService>()("PrismaService", {
  effect: Effect.gen(function* () {
    return {
      $transaction: <R, E, A>(
        effect: Effect.Effect<A, E, R>,
        options?: {
          maxWait?: number;
          timeout?: number;
          isolationLevel?: Prisma.TransactionIsolationLevel;
        },
      ) =>
        Effect.flatMap(
          Effect.all([PrismaClientService, Effect.runtime<R>()]),
          ([{ client, tx }, runtime]): Effect.Effect<A, E | PrismaError, R> => {
            const isRootClient = "$transaction" in tx;
            if (!isRootClient) {
              return effect;
            }
            return Effect.tryPromise(() =>
              client.$transaction(async (tx) => {
                return await Runtime.runPromise(runtime)(
                  effect.pipe(
                    Effect.provideService(PrismaClientService, {
                      tx,
                      client,
                    }),
                  ) as Effect.Effect<A, E, R>,
                );
              }, options),
            ) as unknown as Effect.Effect<A, E, R>;
          },
        ),
...
    };
  }),
}) {}


But if known errors happen inside a transaction I'm now losing that information because of the Effect -> Promise -> Effect flow. Is there a way to work around this easily?
Was this page helpful?