Skip to content

Commit 45d7bef

Browse files
committed
Updated dependencies and error helpers
1 parent a920ac5 commit 45d7bef

File tree

3 files changed

+60
-6
lines changed

3 files changed

+60
-6
lines changed

errors.go

Lines changed: 57 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package postgres
33
import (
44
"errors"
55
"net"
6+
"strings"
67
)
78

89
// -----------------------------------------------------------------------------
@@ -40,6 +41,9 @@ type NoRowsError struct {
4041

4142
// Unwrap returns the underlying error.
4243
func (e *Error) Unwrap() error {
44+
if e == nil {
45+
return nil
46+
}
4347
return e.err
4448
}
4549

@@ -48,11 +52,43 @@ func (e *Error) Error() string {
4852
if e == nil {
4953
return ""
5054
}
51-
s := e.message
55+
56+
sb := strings.Builder{}
57+
_, _ = sb.WriteString(e.message)
5258
if e.err != nil {
53-
s += " [err=" + e.err.Error() + "]"
59+
_, _ = sb.WriteString(" [err=" + e.err.Error() + "]")
60+
}
61+
if e.Details != nil {
62+
_, _ = sb.WriteString(" [code=" + e.Details.Code + "]")
63+
}
64+
return sb.String()
65+
}
66+
67+
func (e *Error) IsDuplicateKeyError() bool {
68+
if e != nil && e.Details != nil {
69+
if e.Details.Code == "23505" {
70+
return true
71+
}
72+
}
73+
return false
74+
}
75+
76+
func (e *Error) IsConstraintViolationError() bool {
77+
if e != nil && e.Details != nil {
78+
switch e.Details.Code {
79+
case "23000":
80+
return true
81+
case "23502":
82+
return true
83+
case "23503":
84+
return true
85+
case "23514":
86+
return true
87+
case "23P01":
88+
return true
89+
}
5490
}
55-
return s
91+
return false
5692
}
5793

5894
func (e *NoRowsError) Error() string {
@@ -68,6 +104,24 @@ func IsDatabaseError(err error) bool {
68104
return errors.As(err, &e)
69105
}
70106

107+
func IsDuplicateKeyError(err error) bool {
108+
var e *Error
109+
110+
if errors.As(err, &e) {
111+
return e.IsDuplicateKeyError()
112+
}
113+
return false
114+
}
115+
116+
func IsConstraintViolationError(err error) bool {
117+
var e *Error
118+
119+
if errors.As(err, &e) {
120+
return e.IsConstraintViolationError()
121+
}
122+
return false
123+
}
124+
71125
// IsNoRowsError returns true if the given error is the result of returning an empty result set.
72126
func IsNoRowsError(err error) bool {
73127
var e *NoRowsError

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ module github.com/randlabs/go-postgres
22

33
go 1.19
44

5-
require github.com/jackc/pgx/v5 v5.5.4
5+
require github.com/jackc/pgx/v5 v5.6.0
66

77
require (
88
github.com/jackc/pgpassfile v1.0.0 // indirect

go.sum

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ github.com/jackc/pgpassfile v1.0.0 h1:/6Hmqy13Ss2zCq62VdNG8tM1wchn8zjSGOBJ6icpsI
44
github.com/jackc/pgpassfile v1.0.0/go.mod h1:CEx0iS5ambNFdcRtxPj5JhEz+xB6uRky5eyVu/W2HEg=
55
github.com/jackc/pgservicefile v0.0.0-20231201235250-de7065d80cb9 h1:L0QtFUgDarD7Fpv9jeVMgy/+Ec0mtnmYuImjTz6dtDA=
66
github.com/jackc/pgservicefile v0.0.0-20231201235250-de7065d80cb9/go.mod h1:5TJZWKEWniPve33vlWYSoGYefn3gLQRzjfDlhSJ9ZKM=
7-
github.com/jackc/pgx/v5 v5.5.4 h1:Xp2aQS8uXButQdnCMWNmvx6UysWQQC+u1EoizjguY+8=
8-
github.com/jackc/pgx/v5 v5.5.4/go.mod h1:ez9gk+OAat140fv9ErkZDYFWmXLfV+++K0uAOiwgm1A=
7+
github.com/jackc/pgx/v5 v5.6.0 h1:SWJzexBzPL5jb0GEsrPMLIsi/3jOo7RHlzTjcAeDrPY=
8+
github.com/jackc/pgx/v5 v5.6.0/go.mod h1:DNZ/vlrUnhWCoFGxHAG8U2ljioxukquj7utPDgtQdTw=
99
github.com/jackc/puddle/v2 v2.2.1 h1:RhxXJtFG022u4ibrCSMSiu5aOq1i77R3OHKNJj77OAk=
1010
github.com/jackc/puddle/v2 v2.2.1/go.mod h1:vriiEXHvEE654aYKXXjOvZM39qJ0q+azkZFrfEOc3H4=
1111
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=

0 commit comments

Comments
 (0)