Skip to content

Commit

Permalink
avoid usage of pkg/errors and support of Go 1.13 errors wrapping (#1)
Browse files Browse the repository at this point in the history
* avoid usage of pkg/errors

* update go
  • Loading branch information
auvn authored Oct 22, 2020
1 parent 7240fdd commit 95c1a87
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 23 deletions.
3 changes: 1 addition & 2 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
module github.com/humans-net/pq

go 1.12
go 1.14

require (
github.com/jackc/pgconn v1.7.0
github.com/jackc/pgx/v4 v4.9.0
github.com/opentracing/opentracing-go v1.1.0
github.com/pkg/errors v0.8.1
github.com/prometheus/client_golang v1.1.0
go.uber.org/zap v1.10.0
)
36 changes: 16 additions & 20 deletions pgx_adapter.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import (
"github.com/jackc/pgx/v4"
"github.com/jackc/pgx/v4/log/zapadapter"
"github.com/jackc/pgx/v4/pgxpool"
"github.com/pkg/errors"
)

type PgxAdapter struct {
Expand All @@ -19,12 +18,12 @@ type PgxAdapter struct {
name string
}

var _ Client = &PgxAdapter{}
var _ Client = (*PgxAdapter)(nil)

func (p *PgxAdapter) Transaction(ctx context.Context, f func(context.Context, Executor) error) error {
tx, er := p.pool.BeginTx(ctx, defaultTxOptions)
if er != nil {
return er
tx, err := p.pool.BeginTx(ctx, defaultTxOptions)
if err != nil {
return fmt.Errorf("begin transaction: %w", err)
}

var txAdapter Executor = &PgxTxAdapter{tx}
Expand All @@ -34,20 +33,19 @@ func (p *PgxAdapter) Transaction(ctx context.Context, f func(context.Context, Ex
if p.withMetrics {
txAdapter = &metricsAdapter{Executor: txAdapter, name: p.name}
}
execErr := f(ctx, txAdapter)
var err error

if execErr != nil {
err = errors.Wrap(execErr, "failed to exec transaction")
rbErr := tx.Rollback(ctx)
if rbErr != nil {
err = errors.Wrapf(err, "failed to rollback failed transaction: %v", rbErr)

if txErr := f(ctx, txAdapter); txErr != nil {
txErr = fmt.Errorf("exec transaction: %w", txErr)

if err := tx.Rollback(ctx); err != nil {
txErr = fmt.Errorf("%w: rollback: %v", txErr, err)
}
return err

return txErr
}

if err := tx.Commit(ctx); err != nil {
return errors.Wrap(err, "failed to commit transaction")
return fmt.Errorf("commit transaction: %w", err)
}

return nil
Expand All @@ -74,9 +72,7 @@ func NewClient(ctx context.Context, cfg Config) Client {

poolCfg, err := pgxpool.ParseConfig(cfg.ConnString)
if err != nil {
if err != nil {
panic(fmt.Sprintf("failed to connect to postgres %s: %v", cfg.ConnString, err))
}
panic(fmt.Errorf("connect to postgres %s: %v", cfg.ConnString, err))
}

if cfg.TCPKeepAlivePeriod == 0 {
Expand All @@ -98,11 +94,11 @@ func NewClient(ctx context.Context, cfg Config) Client {

connPool, err := pgxpool.ConnectConfig(ctx, poolCfg)
if err != nil {
panic(fmt.Sprintf("failed to connect to postgres %s: %v", cfg.ConnString, err))
panic(fmt.Errorf("connect to postgres %s: %v", cfg.ConnString, err))
}

if err := collector.register(cfg.Name, connPool); err != nil {
panic(fmt.Sprintf("failed to register dbx pool %q: %v", cfg.Name, err))
panic(fmt.Errorf("register dbx pool %q: %v", cfg.Name, err))
}

var adapter Client = &PgxAdapter{
Expand Down
2 changes: 1 addition & 1 deletion pgx_tx_adapter.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ type PgxTxAdapter struct {
tx pgx.Tx
}

var _ Executor = &PgxTxAdapter{}
var _ Executor = (*PgxTxAdapter)(nil)

func (p *PgxTxAdapter) Exec(ctx context.Context, sql string, args ...interface{}) (result RowsAffected, err error) {
return p.tx.Exec(ctx, sql, args...)
Expand Down

0 comments on commit 95c1a87

Please sign in to comment.