|
| 1 | +package errx |
| 2 | + |
| 3 | +import ( |
| 4 | + "errors" |
| 5 | + "fmt" |
| 6 | + "strings" |
| 7 | +) |
| 8 | + |
| 9 | +var ( |
| 10 | + // ErrMissingIdentity indicates SQL generation/execution cannot proceed because |
| 11 | + // identity (primary key) columns were not detected. |
| 12 | + ErrMissingIdentity = errors.New("missing identity") |
| 13 | + |
| 14 | + // ErrDuplicateKey indicates an insert/update violated a unique constraint. |
| 15 | + // Note: many drivers return opaque error types; use IsDuplicateKey to detect. |
| 16 | + ErrDuplicateKey = errors.New("duplicate key") |
| 17 | + |
| 18 | + // ErrConstraint indicates a generic constraint violation (FK/CK/NOT NULL/etc). |
| 19 | + ErrConstraint = errors.New("constraint violation") |
| 20 | +) |
| 21 | + |
| 22 | +// Error carries structured context while remaining compatible with errors.Is(). |
| 23 | +type Error struct { |
| 24 | + Kind error |
| 25 | + Op string |
| 26 | + Table string |
| 27 | + Columns []string |
| 28 | + IdentityIndex int |
| 29 | + Cause error |
| 30 | +} |
| 31 | + |
| 32 | +func (e *Error) Error() string { |
| 33 | + sb := &strings.Builder{} |
| 34 | + sb.WriteString("sqlx") |
| 35 | + if e.Op != "" { |
| 36 | + sb.WriteString(" ") |
| 37 | + sb.WriteString(e.Op) |
| 38 | + } |
| 39 | + sb.WriteString(": ") |
| 40 | + if e.Kind != nil { |
| 41 | + sb.WriteString(e.Kind.Error()) |
| 42 | + } else { |
| 43 | + sb.WriteString("error") |
| 44 | + } |
| 45 | + if e.Table != "" { |
| 46 | + sb.WriteString(" table=") |
| 47 | + sb.WriteString(e.Table) |
| 48 | + } |
| 49 | + if e.IdentityIndex != 0 { |
| 50 | + sb.WriteString(fmt.Sprintf(" identityIndex=%d", e.IdentityIndex)) |
| 51 | + } |
| 52 | + if len(e.Columns) > 0 { |
| 53 | + sb.WriteString(" columns=[") |
| 54 | + sb.WriteString(strings.Join(e.Columns, ",")) |
| 55 | + sb.WriteString("]") |
| 56 | + } |
| 57 | + if e.Cause != nil { |
| 58 | + sb.WriteString(": ") |
| 59 | + sb.WriteString(e.Cause.Error()) |
| 60 | + } |
| 61 | + return sb.String() |
| 62 | +} |
| 63 | + |
| 64 | +func (e *Error) Unwrap() error { return e.Cause } |
| 65 | + |
| 66 | +func (e *Error) Is(target error) bool { |
| 67 | + if target == nil { |
| 68 | + return false |
| 69 | + } |
| 70 | + if e.Kind != nil && target == e.Kind { |
| 71 | + return true |
| 72 | + } |
| 73 | + if e.Cause != nil { |
| 74 | + return errors.Is(e.Cause, target) |
| 75 | + } |
| 76 | + return false |
| 77 | +} |
| 78 | + |
| 79 | +func MissingIdentity(op, table string, columns []string, identityIndex int) error { |
| 80 | + return &Error{ |
| 81 | + Kind: ErrMissingIdentity, |
| 82 | + Op: op, |
| 83 | + Table: table, |
| 84 | + Columns: columns, |
| 85 | + IdentityIndex: identityIndex, |
| 86 | + } |
| 87 | +} |
| 88 | + |
| 89 | +func DuplicateKey(op, table string, cause error) error { |
| 90 | + return &Error{ |
| 91 | + Kind: ErrDuplicateKey, |
| 92 | + Op: op, |
| 93 | + Table: table, |
| 94 | + Cause: cause, |
| 95 | + } |
| 96 | +} |
| 97 | + |
| 98 | +func Constraint(op, table string, cause error) error { |
| 99 | + return &Error{ |
| 100 | + Kind: ErrConstraint, |
| 101 | + Op: op, |
| 102 | + Table: table, |
| 103 | + Cause: cause, |
| 104 | + } |
| 105 | +} |
| 106 | + |
| 107 | +func IsMissingIdentity(err error) bool { return errors.Is(err, ErrMissingIdentity) } |
| 108 | + |
| 109 | +func IsDuplicateKey(err error) bool { |
| 110 | + if errors.Is(err, ErrDuplicateKey) { |
| 111 | + return true |
| 112 | + } |
| 113 | + msg := strings.ToLower(errString(err)) |
| 114 | + return strings.Contains(msg, "unique constraint") || |
| 115 | + strings.Contains(msg, "duplicate key") || |
| 116 | + strings.Contains(msg, "duplicate entry") |
| 117 | +} |
| 118 | + |
| 119 | +func IsConstraint(err error) bool { |
| 120 | + if errors.Is(err, ErrConstraint) { |
| 121 | + return true |
| 122 | + } |
| 123 | + msg := strings.ToLower(errString(err)) |
| 124 | + return strings.Contains(msg, "constraint failed") || |
| 125 | + strings.Contains(msg, "violates") || |
| 126 | + strings.Contains(msg, "not null constraint") || |
| 127 | + strings.Contains(msg, "foreign key constraint") || |
| 128 | + strings.Contains(msg, "check constraint") |
| 129 | +} |
| 130 | + |
| 131 | +func errString(err error) string { |
| 132 | + if err == nil { |
| 133 | + return "" |
| 134 | + } |
| 135 | + return err.Error() |
| 136 | +} |
0 commit comments