Skip to content

Commit

Permalink
update to go 1.13 errors
Browse files Browse the repository at this point in the history
  • Loading branch information
acoshift committed Sep 8, 2019
1 parent 61bf950 commit c57f1d7
Show file tree
Hide file tree
Showing 5 changed files with 14 additions and 9 deletions.
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
language: go

go:
- 1.12.x
- 1.13.x

services:
- postgresql
Expand Down
10 changes: 7 additions & 3 deletions error.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package pgsql

import (
"errors"
"regexp"

"github.com/lib/pq"
Expand All @@ -18,7 +19,8 @@ func contains(xs []string, x string) bool {
// IsUniqueViolation checks is error an unique_violation with given constraint,
// constraint can be empty to ignore constraint name checks
func IsUniqueViolation(err error, constraint ...string) bool {
if pqErr, ok := err.(*pq.Error); ok && pqErr.Code == "23505" {
var pqErr *pq.Error
if errors.As(err, &pqErr) && pqErr.Code == "23505" {
if len(constraint) == 0 {
return true
}
Expand All @@ -29,15 +31,17 @@ func IsUniqueViolation(err error, constraint ...string) bool {

// IsInvalidTextRepresentation checks is error an invalid_text_representation
func IsInvalidTextRepresentation(err error) bool {
if pqErr, ok := err.(*pq.Error); ok && pqErr.Code == "22P02" {
var pqErr *pq.Error
if errors.As(err, &pqErr) && pqErr.Code == "22P02" {
return true
}
return false
}

// IsForeignKeyViolation checks is error an foreign_key_violation
func IsForeignKeyViolation(err error, constraint ...string) bool {
if pqErr, ok := err.(*pq.Error); ok && pqErr.Code == "23503" {
var pqErr *pq.Error
if errors.As(err, &pqErr) && pqErr.Code == "23503" {
if len(constraint) == 0 {
return true
}
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module github.com/acoshift/pgsql

go 1.12
go 1.13

require (
github.com/DATA-DOG/go-sqlmock v1.3.3
Expand Down
3 changes: 2 additions & 1 deletion pgctx/pgctx.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package pgctx
import (
"context"
"database/sql"
"errors"
"net/http"

"github.com/acoshift/pgsql"
Expand Down Expand Up @@ -53,7 +54,7 @@ func RunInTx(ctx context.Context, f func(ctx context.Context) error) error {
ctx := context.WithValue(ctx, ctxKeyQueryer{}, tx)
ctx = context.WithValue(ctx, ctxKeyCommitted{}, cm)
err := f(ctx)
if err == pgsql.ErrAbortTx {
if errors.Is(err, pgsql.ErrAbortTx) {
abort = true
}
return err
Expand Down
6 changes: 3 additions & 3 deletions tx.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,11 +75,11 @@ func RunInTxContext(ctx context.Context, db BeginTxer, opts *TxOptions, fn func(

for i := 0; i < option.MaxAttempts; i++ {
err := f()
if err == nil || err == ErrAbortTx {
if err == nil || errors.Is(err, ErrAbortTx) {
return nil
}
pqErr, ok := err.(*pq.Error)
if retryable := ok && (pqErr.Code == "40001"); !retryable {
var pqErr *pq.Error
if retryable := errors.As(err, &pqErr) && (pqErr.Code == "40001"); !retryable {
return err
}
}
Expand Down

0 comments on commit c57f1d7

Please sign in to comment.