-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patherrors.go
More file actions
226 lines (204 loc) · 5.28 KB
/
errors.go
File metadata and controls
226 lines (204 loc) · 5.28 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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
package gpamongo
import (
"strings"
"github.com/lemmego/gpa"
"go.mongodb.org/mongo-driver/mongo"
)
// =====================================
// Error Conversion
// =====================================
// convertMongoError converts MongoDB errors to GPA errors
func convertMongoError(err error) error {
if err == nil {
return nil
}
switch err {
case mongo.ErrNoDocuments:
return gpa.GPAError{
Type: gpa.ErrorTypeNotFound,
Message: "document not found",
Cause: err,
}
case mongo.ErrNilDocument:
return gpa.GPAError{
Type: gpa.ErrorTypeValidation,
Message: "nil document provided",
Cause: err,
}
case mongo.ErrNilValue:
return gpa.GPAError{
Type: gpa.ErrorTypeValidation,
Message: "nil value provided",
Cause: err,
}
}
// Check for MongoDB-specific errors
if mongoErr, ok := err.(mongo.WriteException); ok {
for _, writeErr := range mongoErr.WriteErrors {
switch writeErr.Code {
case 11000, 11001: // Duplicate key error
return gpa.GPAError{
Type: gpa.ErrorTypeDuplicate,
Message: "duplicate key violation",
Cause: err,
}
case 121: // Document validation failed
return gpa.GPAError{
Type: gpa.ErrorTypeValidation,
Message: "document validation failed",
Cause: err,
}
}
}
}
// Check for bulk write errors
if bulkErr, ok := err.(mongo.BulkWriteException); ok {
for _, writeErr := range bulkErr.WriteErrors {
switch writeErr.Code {
case 11000, 11001: // Duplicate key error
return gpa.GPAError{
Type: gpa.ErrorTypeDuplicate,
Message: "duplicate key violation in bulk operation",
Cause: err,
}
}
}
}
// Check for command errors
if cmdErr, ok := err.(mongo.CommandError); ok {
switch cmdErr.Code {
case 11000, 11001: // Duplicate key error
return gpa.GPAError{
Type: gpa.ErrorTypeDuplicate,
Message: "duplicate key violation",
Cause: err,
}
case 26: // NamespaceNotFound
return gpa.GPAError{
Type: gpa.ErrorTypeNotFound,
Message: "collection not found",
Cause: err,
}
case 48: // CollectionNotFound
return gpa.GPAError{
Type: gpa.ErrorTypeNotFound,
Message: "collection not found",
Cause: err,
}
case 13: // Unauthorized
return gpa.GPAError{
Type: gpa.ErrorTypeConnection,
Message: "unauthorized access",
Cause: err,
}
case 18: // AuthenticationFailed
return gpa.GPAError{
Type: gpa.ErrorTypeConnection,
Message: "authentication failed",
Cause: err,
}
case 251: // NoSuchTransaction
return gpa.GPAError{
Type: gpa.ErrorTypeTransaction,
Message: "transaction not found",
Cause: err,
}
case 244: // TransactionTooOld
return gpa.GPAError{
Type: gpa.ErrorTypeTransaction,
Message: "transaction too old",
Cause: err,
}
}
}
// Check for network timeout errors
errStr := strings.ToLower(err.Error())
if strings.Contains(errStr, "timeout") || strings.Contains(errStr, "deadline exceeded") {
return gpa.GPAError{
Type: gpa.ErrorTypeTimeout,
Message: "operation timeout",
Cause: err,
}
}
// Check for connection errors
if strings.Contains(errStr, "connection") || strings.Contains(errStr, "network") {
return gpa.GPAError{
Type: gpa.ErrorTypeConnection,
Message: "connection error",
Cause: err,
}
}
// Check for validation errors
if strings.Contains(errStr, "validation") || strings.Contains(errStr, "invalid") {
return gpa.GPAError{
Type: gpa.ErrorTypeValidation,
Message: "validation error",
Cause: err,
}
}
// Default to generic error
return gpa.GPAError{
Type: gpa.ErrorTypeConnection,
Message: "database operation failed",
Cause: err,
}
}
// =====================================
// Specific Error Constructors
// =====================================
// NewNotFoundError creates a not found error
func NewNotFoundError(message string) error {
return gpa.GPAError{
Type: gpa.ErrorTypeNotFound,
Message: message,
}
}
// NewValidationError creates a validation error
func NewValidationError(message string) error {
return gpa.GPAError{
Type: gpa.ErrorTypeValidation,
Message: message,
}
}
// NewDuplicateError creates a duplicate error
func NewDuplicateError(message string) error {
return gpa.GPAError{
Type: gpa.ErrorTypeDuplicate,
Message: message,
}
}
// NewConnectionError creates a connection error
func NewConnectionError(message string, cause error) error {
return gpa.GPAError{
Type: gpa.ErrorTypeConnection,
Message: message,
Cause: cause,
}
}
// NewTransactionError creates a transaction error
func NewTransactionError(message string, cause error) error {
return gpa.GPAError{
Type: gpa.ErrorTypeTransaction,
Message: message,
Cause: cause,
}
}
// NewTimeoutError creates a timeout error
func NewTimeoutError(message string, cause error) error {
return gpa.GPAError{
Type: gpa.ErrorTypeTimeout,
Message: message,
Cause: cause,
}
}
// NewUnsupportedError creates an unsupported operation error
func NewUnsupportedError(message string) error {
return gpa.GPAError{
Type: gpa.ErrorTypeUnsupported,
Message: message,
}
}
// =====================================
// Registration
// =====================================
// Legacy registration removed - use NewProvider() instead