Skip to content

Commit 0d20981

Browse files
committed
Don't move pq.Error to pqerror.Error
Still move pq.ErrorCode and pq.ErrorClass to pqerror.Code and pqerror.Class, so we can use it in pqerror. Also move the error severity values here. Seems to make sense.
1 parent 4332138 commit 0d20981

16 files changed

Lines changed: 749 additions & 749 deletions

‎CHANGELOG.md‎

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -32,14 +32,13 @@ unreleased
3232

3333
For example, to test if an error is a UNIQUE constraint violation:
3434

35-
pqErr, ok := pq.AsType[*pq.Error](err)
36-
if ok && pqErr.Code == pqerror.UniqueViolation {
35+
if pqErr, ok := errors.AsType[*pq.Error](err); ok && pqErr.Code == pqerror.UniqueViolation {
3736
log.Fatalf("email %q already exsts", email)
3837
}
3938

40-
To make this a bit more convenient, it also adds a pqerror.As() function:
39+
To make this a bit more convenient, it also adds a `pq.As()` function:
4140

42-
pqErr := pqerror.As(err, pqerror.UniqueViolation)
41+
pqErr := pq.As(err, pqerror.UniqueViolation)
4342
if pqErr != nil {
4443
log.Fatalf("email %q already exsts", email)
4544
}
@@ -74,7 +73,6 @@ unreleased
7473
[#1283]: https://github.com/lib/pq/pull/1283
7574
[#1285]: https://github.com/lib/pq/pull/1285
7675

77-
7876
v1.11.2 (2026-02-10)
7977
--------------------
8078
This fixes two regressions:

‎README.md‎

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -90,8 +90,17 @@ The libpq way (which also works in pq) is to use `options='-c k=v'` like so:
9090

9191
Errors
9292
------
93-
Errors from PostgreSQL are returned as [pq.Error]; the Error() string contains
94-
the error message and code:
93+
Errors from PostgreSQL are returned as [pq.Error]; [pq.As] can be used to
94+
convert an error to `pq.Error`:
95+
96+
```go
97+
pqErr := pq.As(err, pqerror.UniqueViolation)
98+
if pqErr != nil {
99+
return fmt.Errorf("email %q already exsts", email)
100+
}
101+
```
102+
103+
the Error() string contains the error message and code:
95104

96105
pq: duplicate key value violates unique constraint "users_lower_idx" (23505)
97106

@@ -118,6 +127,7 @@ It contains the context where this error occurred:
118127
^
119128

120129
[pq.Error]: https://pkg.go.dev/github.com/lib/pq#Error
130+
[pq.As]: https://pkg.go.dev/github.com/lib/pq#As
121131

122132
PostgreSQL features
123133
-------------------

pqerror/as.go renamed to as.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,16 @@
11
//go:build !go1.26
22

3-
package pqerror
3+
package pq
44

55
import (
66
"errors"
77
"slices"
88
)
99

10-
// As asserts that the given error is [pqerror.Error] and returns it.
10+
// As asserts that the given error is [pq.Error] and returns it, returning nil
11+
// if it's not a pq.Error.
1112
//
12-
// It will return nil if the Error is not one of the given error codes. If no
13+
// It will return nil if the pq.Error is not one of the given error codes. If no
1314
// codes are given it will always return the Error.
1415
//
1516
// This is safe to call with a nil error.

pqerror/as_126.go renamed to as_go126.go

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,21 @@
11
//go:build go1.26
22

3-
package pqerror
3+
package pq
44

55
import (
66
"errors"
7+
"github.com/lib/pq/pqerror"
78
"slices"
89
)
910

10-
// As asserts that the given error is [pqerror.Error] and returns it.
11+
// As asserts that the given error is [pq.Error] and returns it, returning nil
12+
// if it's not a pq.Error.
1113
//
12-
// It will return nil if the Error is not one of the given error codes. If no
14+
// It will return nil if the pq.Error is not one of the given error codes. If no
1315
// codes are given it will always return the Error.
1416
//
1517
// This is safe to call with a nil error.
16-
func As(err error, codes ...ErrorCode) *Error {
18+
func As(err error, codes ...pqerror.Code) *Error {
1719
if pqErr, ok := errors.AsType[*Error](err); ok && (len(codes) == 0 || slices.Contains(codes, pqErr.Code)) {
1820
return pqErr
1921
}

‎conn_test.go‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -642,7 +642,7 @@ func TestErrorDuringStartupClosesConn(t *testing.T) {
642642

643643
func TestBadConn(t *testing.T) {
644644
t.Parallel()
645-
for _, tt := range []error{io.EOF, &Error{Severity: Efatal}} {
645+
for _, tt := range []error{io.EOF, &Error{Severity: pqerror.SeverityFatal}} {
646646
t.Run(fmt.Sprintf("%s", tt), func(t *testing.T) {
647647
var cn conn
648648
err := cn.handleError(tt)

‎deprecated.go‎

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,23 @@ package pq
33
import (
44
"bytes"
55
"database/sql"
6+
7+
"github.com/lib/pq/pqerror"
8+
)
9+
10+
// [pq.Error.Severity] values.
11+
//
12+
// Deprecated: use pqerror.Severity[..] values.
13+
//
14+
//go:fix inline
15+
const (
16+
Efatal = pqerror.SeverityFatal
17+
Epanic = pqerror.SeverityPanic
18+
Ewarning = pqerror.SeverityWarning
19+
Enotice = pqerror.SeverityNotice
20+
Edebug = pqerror.SeverityDebug
21+
Einfo = pqerror.SeverityInfo
22+
Elog = pqerror.SeverityLog
623
)
724

825
// PGError is an interface used by previous versions of pq.
@@ -14,6 +31,49 @@ type PGError interface {
1431
Get(k byte) (v string)
1532
}
1633

34+
// Get implements the legacy PGError interface.
35+
//
36+
// Deprecated: new code should use the fields of the Error struct directly.
37+
func (e *Error) Get(k byte) (v string) {
38+
switch k {
39+
case 'S':
40+
return e.Severity
41+
case 'C':
42+
return string(e.Code)
43+
case 'M':
44+
return e.Message
45+
case 'D':
46+
return e.Detail
47+
case 'H':
48+
return e.Hint
49+
case 'P':
50+
return e.Position
51+
case 'p':
52+
return e.InternalPosition
53+
case 'q':
54+
return e.InternalQuery
55+
case 'W':
56+
return e.Where
57+
case 's':
58+
return e.Schema
59+
case 't':
60+
return e.Table
61+
case 'c':
62+
return e.Column
63+
case 'd':
64+
return e.DataTypeName
65+
case 'n':
66+
return e.Constraint
67+
case 'F':
68+
return e.File
69+
case 'L':
70+
return e.Line
71+
case 'R':
72+
return e.Routine
73+
}
74+
return ""
75+
}
76+
1777
// ParseURL converts a url to a connection string for driver.Open.
1878
//
1979
// Deprecated: directly passing an URL to sql.Open("postgres", "postgres://...")

0 commit comments

Comments
 (0)