Skip to content

Instantly share code, notes, and snippets.

@jchapuis
Created March 7, 2024 11:14
Show Gist options
  • Save jchapuis/9c0a0341bba76765daa5981797b17de7 to your computer and use it in GitHub Desktop.
Save jchapuis/9c0a0341bba76765daa5981797b17de7 to your computer and use it in GitHub Desktop.
Transfer
def transfer(from: AccountID, to: AccountID, amount: PosAmount): F[TransferFailure \/ Unit] =
coordinator
.create(TransferID.random, Transfer(from, to, amount), from, to)
.use(_.pollForFinalStatus())
.flatMap {
case Status.Committed => ().asRight[TransferFailure].pure
case Status.Aborted(reason) =>
reason match {
case AbortReason.Timeout =>
EitherT.leftT(TransferFailure.Timeout: TransferFailure).value
case AbortReason.Branches(reasons) => EitherT.leftT(reasons.head).value
case AbortReason.Client(Some(reason)) => EitherT.leftT(reason).value
case AbortReason.Client(None) => new Exception("Transaction aborted by client without justification").raiseError[F, TransferFailure \/ Unit]
}
case Status.Failed(errors) =>
Logger[F].error(show"Transaction failed: $errors") *> new Exception("Transaction failed due to branch error").raiseError[F, TransferFailure \/ Unit]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment