Created
March 7, 2024 11:14
-
-
Save jchapuis/9c0a0341bba76765daa5981797b17de7 to your computer and use it in GitHub Desktop.
Transfer
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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