refactor
This commit is contained in:
38
pkg/transaction/wrap.go
Normal file
38
pkg/transaction/wrap.go
Normal file
@@ -0,0 +1,38 @@
|
||||
package transaction
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
|
||||
"github.com/jackc/pgx/v5"
|
||||
"github.com/rs/zerolog/log"
|
||||
)
|
||||
|
||||
func Wrap(ctx context.Context, fn func(context.Context) error) error {
|
||||
tx, err := pool.Begin(ctx)
|
||||
if err != nil {
|
||||
return fmt.Errorf("pool.Begin: %w", err)
|
||||
}
|
||||
|
||||
defer func() {
|
||||
err = tx.Rollback(ctx)
|
||||
if err != nil && !errors.Is(err, pgx.ErrTxClosed) {
|
||||
log.Error().Err(err).Msg("transaction: Rollback")
|
||||
}
|
||||
}()
|
||||
|
||||
ctx = context.WithValue(ctx, ctxKey{}, &Transaction{tx})
|
||||
|
||||
err = fn(ctx)
|
||||
if err != nil {
|
||||
return fmt.Errorf("fn: %w", err)
|
||||
}
|
||||
|
||||
err = tx.Commit(ctx)
|
||||
if err != nil {
|
||||
return fmt.Errorf("tx.Commit: %w", err)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
Reference in New Issue
Block a user