Repository navigation
Expand file tree
/
Copy patherrors.go
More file actions
135 lines (103 loc) · 5.32 KB
/
Copy patherrors.go
File metadata and controls
135 lines (103 loc) · 5.32 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
package pgkit
import (
"errors"
"fmt"
"github.com/jackc/pgx/v5/pgconn"
"github.com/retailancer/pgkit/query"
)
var (
// ErrNotFound is returned when no records match the query.
ErrNotFound = errors.New("pgkit: record not found")
// ErrMultipleRecords is returned when a single record was expected but multiple were returned.
ErrMultipleRecords = errors.New("pgkit: multiple records returned")
// ErrUniqueViolation is returned when a unique constraint is violated.
ErrUniqueViolation = errors.New("pgkit: unique constraint violation")
// ErrForeignKeyViolation is returned when a foreign key constraint is violated.
ErrForeignKeyViolation = errors.New("pgkit: foreign key constraint violation")
// ErrCheckViolation is returned when a check constraint is violated.
ErrCheckViolation = errors.New("pgkit: check constraint violation")
// ErrNullViolation is returned when a NOT NULL constraint is violated.
ErrNullViolation = errors.New("pgkit: not-null constraint violation")
// ErrConnectionFailed is returned when connection to database cannot be established.
ErrConnectionFailed = errors.New("pgkit: connection failed")
// ErrTxClosed is returned when an action is performed on an already committed/rolled back transaction.
ErrTxClosed = errors.New("pgkit: transaction already closed")
// ErrUnknownQuery is returned when an unsupported query type is executed.
ErrUnknownQuery = errors.New("pgkit: unknown query type")
// ErrTxAlreadyStarted is returned when starting an already active transaction on a client.
ErrTxAlreadyStarted = errors.New("pgkit: transaction already started")
// ErrInvalidPointer is returned when a non-pointer type is passed to Scan methods.
ErrInvalidPointer = query.ErrInvalidPointer
// ErrDataCannotBeScanned is returned when query result shape doesn't match scan destination structure.
ErrDataCannotBeScanned = query.ErrDataCannotBeScanned
// ErrColumnNotFound is returned when a column referenced by a query is not present
// on the target table. For example soft-deleting a table that lacks the configured
// SoftDeleteColumn, or explicitly requesting updated_at on a table without one.
ErrColumnNotFound = query.ErrColumnNotFound
// ErrTableNotFound is returned when the target table does not exist in the configured schema.
ErrTableNotFound = query.ErrTableNotFound
// ErrSoftDeleteNotConfigured is returned when a soft delete is requested but no
// SoftDeleteColumn was set in Options.
ErrSoftDeleteNotConfigured = query.ErrSoftDeleteNotConfigured
// ErrResolverRequired is returned when a query needs column introspection but has no
// resolver available. Queries using Omit, or Join without Selection, require a live handle.
ErrResolverRequired = query.ErrResolverRequired
// ErrSelectionConflict is returned when Selection and Omit are both set on the same
// Get or Join. They are mutually exclusive.
ErrSelectionConflict = query.ErrSelectionConflict
// ErrAllColumnsOmitted is returned when Omit would remove every column from a table.
ErrAllColumnsOmitted = query.ErrAllColumnsOmitted
// ErrJoinConditionMissing is returned when a Join has an empty On map.
ErrJoinConditionMissing = query.ErrJoinConditionMissing
// ErrEmptyData is returned when a write query has no fields to write.
ErrEmptyData = query.ErrEmptyData
// ErrEmptyConflictOn is returned when an Upsert has no conflict target.
ErrEmptyConflictOn = query.ErrEmptyConflictOn
// ErrEmptyValues is returned when an InsertMany has no rows.
ErrEmptyValues = query.ErrEmptyValues
// ErrRowLengthMismatch is returned when an InsertMany row has fewer values than fields.
ErrRowLengthMismatch = query.ErrRowLengthMismatch
// ErrIndexOutOfBounds is returned when a scan index falls outside the result set.
ErrIndexOutOfBounds = query.ErrIndexOutOfBounds
// ErrUnsupportedQuery is returned when an unrecognised query type is built.
ErrUnsupportedQuery = query.ErrUnsupportedQuery
// ErrColumnLookupFailed is returned when column introspection fails — for example
// when the information_schema query fails or the caller lacks privileges.
ErrColumnLookupFailed = query.ErrColumnLookupFailed
)
type SchemaError = query.SchemaError
func NewSchemaError(sentinel error, schema, table, column, field string) *SchemaError {
return query.NewSchemaError(sentinel, schema, table, column, field)
}
// IsPgErrorCode checks if err or any error in its chain is a PostgreSQL error with the specified code.
func IsPgErrorCode(err error, code string) bool {
var pgErr *pgconn.PgError
if errors.As(err, &pgErr) {
return pgErr.Code == code
}
return false
}
// MapError translates standard pgconn.PgError errors into friendly sentinel errors.
func MapError(err error) error {
if err == nil {
return nil
}
var pgErr *pgconn.PgError
if errors.As(err, &pgErr) {
switch pgErr.Code {
case "23505": // unique_violation
return fmt.Errorf("%w: %w", ErrUniqueViolation, pgErr)
case "23503": // foreign_key_violation
return fmt.Errorf("%w: %w", ErrForeignKeyViolation, pgErr)
case "23514": // check_violation
return fmt.Errorf("%w: %w", ErrCheckViolation, pgErr)
case "23502": // not_null_violation
return fmt.Errorf("%w: %w", ErrNullViolation, pgErr)
case "42703": // undefined_column
return fmt.Errorf("%w: %w", ErrColumnNotFound, pgErr)
case "42P01": // undefined_table
return fmt.Errorf("%w: %w", ErrTableNotFound, pgErr)
}
}
return err
}