Refactor Effect Handling in Stream Transformation

This feels very verbose, is there a better way of doing this? What I'm trying to do is, run an effect in the element on the stream, if the transformation fails, I want to filter out that value and push it to a save queue. If the transformation suceeds, I want that value to continue the stream:
  const transcribedMessages = voiceMessages.pipe(
    Stream.mapEffect((msg) =>
      pipe(
        Effect.tryPromise(() => transcribeMessage(msg, 'es')),
        Effect.tapError((err) =>
          Effect.gen(function*(_) {
            yield* Effect.logError(`Failure while transcribing message "${msg.id.id}"`, err);
            yield* Queue.offer(saveQueue, msg);
          })
        ),
        Effect.either,
      )
    ),
    Stream.filterMap((x) =>
      pipe(
        x,
        Either.match({
          onLeft: () => Option.none(),
          onRight: (x) => Option.some(x),
        }),
      )
    ),
  );
Was this page helpful?