Skip to content

Files

Latest commit

 

History

History
47 lines (34 loc) · 682 Bytes

README.md

File metadata and controls

47 lines (34 loc) · 682 Bytes

transactions

Simply transactions

Usage

package main

import (
	"context"
	"errors"

	"github.com/go-utils/transactions"
)

var failure = errors.New("failure")

func main() {
	ctx := context.Background()

	onTransaction := transactions.OnTransaction(
		func(ctx context.Context) error {
			return failure
		},
	)

	onRollback := transactions.OnRollback(
		func(ctx context.Context, err error) error {
			if !errors.Is(err, failure) {
				return errors.New("is not `failure` error")
			}
			return nil
		},
	)

	transaction := transactions.New(onTransaction, onRollback)


	if err := transaction.Execute(ctx); err != nil {
		panic(err)
	}
}

License

MIT