Skip to content

Commit 111340d

Browse files
committed
Add pqerror package
Add a pqerror package with a list of error codes. Typical usage might be something like: pqErr, ok := errors.AsType[*pq.Error](err) if ok && pqErr == pqerror.UniqueViolation { return fmt.Errorf("user already exists") } To make this a bit more convenient, it also adds a pqerror.As() function: pqErr := pqerror.As(err, pqerror.UniqueViolation) if pqErr != nil { log.Fatalf("email %q already exsts", email) } if err != nil { return err } This also moves most of the error stuff to the pqerror package, with type aliases in the pq package so it won't break anything. Has to export Error.Query to make this work, which is okay. Keep most of the tests in the main pq package for now, to ensure this works. Fixes 492
1 parent f3ef532 commit 111340d

File tree

12 files changed

+1185
-565
lines changed

12 files changed

+1185
-565
lines changed

CHANGELOG.md

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,22 @@ unreleased
88

99
- Support `sslmode=prefer` and `sslmode=allow` ([#1270]).
1010

11+
- Add a new `pqerror` package with PostgreSQL error codes ([#1275]).
12+
13+
For example, to test if an error is a UNIQUE constraint violation:
14+
15+
pqErr, ok := pq.AsType[*pq.Error](err)
16+
if ok && pqErr.Code == pqerror.UniqueViolation {
17+
log.Fatalf("email %q already exsts", email)
18+
}
19+
20+
To make this a bit more convenient, it also adds a pqerror.As() function:
21+
22+
pqErr := pqerror.As(err, pqerror.UniqueViolation)
23+
if pqErr != nil {
24+
log.Fatalf("email %q already exsts", email)
25+
}
26+
1127
### Fixes
1228

1329
- Fix SSL key permission check to allow modes stricter than 0600/0640#1265 ([#1265]).
@@ -23,6 +39,7 @@ unreleased
2339
[#1267]: https://github.com/lib/pq/pull/1267
2440
[#1270]: https://github.com/lib/pq/pull/1270
2541
[#1272]: https://github.com/lib/pq/pull/1272
42+
[#1275]: https://github.com/lib/pq/pull/1275
2643

2744
v1.11.2 (2026-02-10)
2845
--------------------
@@ -134,6 +151,8 @@ newer. Previously PostgreSQL 8.4 and newer were supported.
134151

135152
- Handle ErrorResponse in readReadyForQuery and return proper error ([#1136]).
136153

154+
- Detect COPY even if the query starts with whitespace or comments ([#1198]).
155+
137156
- CopyIn() and CopyInSchema() now work if the list of columns is empty, in which
138157
case it will copy all columns ([#1239]).
139158

@@ -159,6 +178,7 @@ newer. Previously PostgreSQL 8.4 and newer were supported.
159178
[#1180]: https://github.com/lib/pq/pull/1180
160179
[#1184]: https://github.com/lib/pq/pull/1184
161180
[#1188]: https://github.com/lib/pq/pull/1188
181+
[#1198]: https://github.com/lib/pq/pull/1198
162182
[#1211]: https://github.com/lib/pq/pull/1211
163183
[#1212]: https://github.com/lib/pq/pull/1212
164184
[#1214]: https://github.com/lib/pq/pull/1214

deprecated.go

Lines changed: 0 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -11,49 +11,6 @@ type PGError interface {
1111
Get(k byte) (v string)
1212
}
1313

14-
// Get implements the legacy PGError interface.
15-
//
16-
// Deprecated: new code should use the fields of the Error struct directly.
17-
func (e *Error) Get(k byte) (v string) {
18-
switch k {
19-
case 'S':
20-
return e.Severity
21-
case 'C':
22-
return string(e.Code)
23-
case 'M':
24-
return e.Message
25-
case 'D':
26-
return e.Detail
27-
case 'H':
28-
return e.Hint
29-
case 'P':
30-
return e.Position
31-
case 'p':
32-
return e.InternalPosition
33-
case 'q':
34-
return e.InternalQuery
35-
case 'W':
36-
return e.Where
37-
case 's':
38-
return e.Schema
39-
case 't':
40-
return e.Table
41-
case 'c':
42-
return e.Column
43-
case 'd':
44-
return e.DataTypeName
45-
case 'n':
46-
return e.Constraint
47-
case 'F':
48-
return e.File
49-
case 'L':
50-
return e.Line
51-
case 'R':
52-
return e.Routine
53-
}
54-
return ""
55-
}
56-
5714
// ParseURL converts a url to a connection string for driver.Open.
5815
//
5916
// Deprecated: directly passing an URL to sql.Open("postgres", "postgres://...")

0 commit comments

Comments
 (0)