Handling MongoDB Transaction Retries in TypeScript with `withTransaction` Helper

I'm writing helpers for handling mongodb transactions, and in my implementation of withTransaction I would like to catch all errors or defects which indicate that the transaction could be retried. Does the follow look like a sane way to do this?

Effect.catchAllCause(cause => {
  const maybeRetriableMongoError = Cause.find(
    cause,
    error => {
      const squashed = Cause.squash(error)
      return isRetriableTransactionError(squashed)
        ? Option.some(squashed)
        : Option.none()
    }
  )
  // then surface the error to typed channel if Some (so it can be later retried)
  // or die|fail otherwise


The above works for me so far, but so does the following:
Effect.catchAllCause(cause => {
  const error = Cause.squash(cause)
  const maybeRetriableMongoError = isRetriableTransactionError(error)
    ? Option.some(error)
    : Option.none()


Btw. why do we still need to squash to get the underlying error within find? Couldn't it just iterate over these underlying errors in the first place?

Thanks!
Was this page helpful?