Skip to content

Commit 2897579

Browse files
committed
added pqconn.IsQueryCanceled
1 parent f473a89 commit 2897579

File tree

1 file changed

+18
-4
lines changed

1 file changed

+18
-4
lines changed

pqconn/errors.go

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package pqconn
22

33
import (
4+
"context"
45
"errors"
56
"slices"
67

@@ -31,6 +32,8 @@ func WrapKnownErrors(err error) error {
3132
return errors.Join(sqldb.ErrUniqueViolation{Constraint: e.Constraint}, err)
3233
case "23514":
3334
return errors.Join(sqldb.ErrCheckViolation{Constraint: e.Constraint}, err)
35+
case "57014":
36+
return errors.Join(context.Canceled, err)
3437
case "23P01":
3538
return errors.Join(sqldb.ErrExclusionViolation{Constraint: e.Constraint}, err)
3639
case "P0001":
@@ -42,9 +45,9 @@ func WrapKnownErrors(err error) error {
4245

4346
// Class 23 — Integrity Constraint Violation
4447

45-
func IsIntegrityConstraintViolation(err error) bool {
48+
func IsIntegrityConstraintViolationClass(err error) bool {
4649
var e *pq.Error
47-
return errors.As(err, &e) && e.Code == "23000" // integrity_constraint_violation
50+
return errors.As(err, &e) && e.Code.Class() == "23"
4851
}
4952

5053
func IsRestrictViolation(err error) bool {
@@ -78,11 +81,22 @@ func IsExclusionViolation(err error) bool {
7881
return errors.As(err, &e) && e.Code == "23P01" // exclusion_violation
7982
}
8083

84+
// Class 57 - Operator Intervention
85+
86+
// IsQueryCanceled indicates if the passed error
87+
// was caused by a user cancellation of a query.
88+
// The pq error might not unwrap to context.Canceled
89+
// even when it was caused by a context cancellation.
90+
func IsQueryCanceled(err error) bool {
91+
var e *pq.Error
92+
return errors.As(err, &e) && e.Code == "57014"
93+
}
94+
8195
// Class P0 — PL/pgSQL Error
8296

83-
func IsPLPGSQLError(err error) bool {
97+
func IsPLPGSQLErrorClass(err error) bool {
8498
var e *pq.Error
85-
return errors.As(err, &e) && e.Code == "P0000"
99+
return errors.As(err, &e) && e.Code.Class() == "P0"
86100
}
87101

88102
func IsRaisedException(err error) bool {

0 commit comments

Comments
 (0)