-
Notifications
You must be signed in to change notification settings - Fork 3
/
errors.go
393 lines (347 loc) · 12 KB
/
errors.go
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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
package errors
import (
"database/sql"
"fmt"
"os"
"regexp"
"runtime"
"strings"
"github.com/go-sql-driver/mysql"
"github.com/beaconsoftwarellc/gadget/v2/database/qb"
"github.com/beaconsoftwarellc/gadget/v2/errors"
"github.com/beaconsoftwarellc/gadget/v2/generator"
"github.com/beaconsoftwarellc/gadget/v2/log"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
)
// SQLQueryType indicates the type of query being executed that caused an error
type SQLQueryType string
const (
// Select indicates a SELECT statement triggered the error
Select SQLQueryType = "SELECT"
// Insert indicates an INSERT statement triggered the error
Insert = "INSERT"
// Delete indicates a DELETE statement triggered the error
Delete = "DELETE"
// Update indicates an UPDATE statement triggered the error
Update = "UPDATE"
)
const (
dbErrPrefix = "dberr"
invalidForeignKeyMsg = "invalid reference"
dataTooLongMsg = "data too long"
duplicateRecordMsg = "already exists"
mysqlDuplicateEntry = 1062
mysqlDataTooLong = 1406
mysqlInvalidForeignKey = 1452
primaryKeyConstraintCheck = "for key 'PRIMARY'"
)
// IsNotFoundError returns a boolean indicating that the passed error (can be nil) is of
// type *database.NotFoundError
func IsNotFoundError(err error) bool {
var dst *NotFoundError
return errors.As(err, &dst)
}
// ConnectionError is returned when unable to connect to database
type ConnectionError struct {
err error
trace []string
}
func (err *ConnectionError) Error() string {
return err.err.Error()
}
// Trace returns the stack trace for the error
func (err *ConnectionError) Trace() []string {
return err.trace
}
// NewDatabaseConnectionError instantiates a DatabaseConnectionError with a stack trace
func NewDatabaseConnectionError(err error) errors.TracerError {
return &ConnectionError{err: err, trace: errors.GetStackTrace()}
}
// TranslateError converts a mysql or other obtuse errors into discrete explicit errors
func TranslateError(err error, action SQLQueryType, stmt string) errors.TracerError {
if nil == err {
return nil
}
if sql.ErrNoRows == err {
return NewNotFoundError()
}
driverErr, ok := err.(*mysql.MySQLError)
if !ok {
return NewSystemError(action, stmt, err)
}
switch driverErr.Number {
// Duplicate primary key
case mysqlDuplicateEntry:
if strings.Contains(err.Error(), primaryKeyConstraintCheck) {
return NewDuplicateRecordError(action, stmt, err)
}
return NewUniqueConstraintError(action, stmt, err)
// Data too long for column
case mysqlDataTooLong:
return NewDataTooLongError(action, stmt, err)
// Invalid foreign key
case mysqlInvalidForeignKey:
return NewInvalidForeignKeyError(action, stmt, err)
default:
return NewExecutionError(action, stmt, err)
}
}
// SQLExecutionError is returned when a query against the database fails
type SQLExecutionError struct {
Action SQLQueryType
ReferenceID string
message string
Stmt string
ErrMsg string
trace []string
}
// NewExecutionError with wrapping the passed error with the passed action and sql statement
func NewExecutionError(action SQLQueryType, stmt string, err error) errors.TracerError {
e := &SQLExecutionError{
ReferenceID: generator.ID(dbErrPrefix),
Action: action,
Stmt: stmt,
ErrMsg: err.Error(),
trace: errors.GetStackTrace(),
}
e.message = fmt.Sprintf("%s: caused a database error", e.Action)
return e
}
// Error logs the reference id and statement for reference and returns a string
// representation of this error containing the reference ID
func (e *SQLExecutionError) Error() string {
log.Infof("[Ref:%s] ERROR STMT: %s", e.ReferenceID, e.Stmt)
return fmt.Sprintf("%s: %s [Ref:%s]",
e.message, e.ErrMsg, e.ReferenceID)
}
// Trace returns the stack trace for the error
func (e *SQLExecutionError) Trace() []string {
return e.trace
}
// NewValidationError returns a ValidationError with a stack trace
func NewValidationError(msg string, subs ...interface{}) errors.TracerError {
return &ValidationError{
message: fmt.Sprintf(msg, subs...),
trace: errors.GetStackTrace(),
}
}
// ValidationError is returned when a query against the database fails
type ValidationError struct {
message string
trace []string
}
// Error prints a ValidationError
func (e *ValidationError) Error() string {
return e.message
}
// Trace returns the stack trace for the error
func (e *ValidationError) Trace() []string {
return e.trace
}
// NewNotFoundError returns a NotFoundError with a stack trace
func NewNotFoundError() errors.TracerError {
return &NotFoundError{
trace: errors.GetStackTrace(),
}
}
// NotFoundError is returned when a query against the database fails
type NotFoundError struct {
trace []string
}
// Error prints a NotFoundError
func (e *NotFoundError) Error() string {
return "no-records"
}
// Trace returns the stack trace for the error
func (e *NotFoundError) Trace() []string {
return e.trace
}
// SQLSystemError is returned when a database action fails
type SQLSystemError struct {
SQLExecutionError
}
// NewSystemError returns an ExecutionError
func NewSystemError(action SQLQueryType, stmt string, err error) errors.TracerError {
return &SQLSystemError{
SQLExecutionError{
ErrMsg: err.Error(),
ReferenceID: generator.ID(dbErrPrefix),
Action: action,
message: "could not execute query",
Stmt: stmt,
trace: errors.GetStackTrace(),
},
}
}
// NotAPointerError indicates that a record object isn't a pointer
type NotAPointerError struct{ trace []string }
func (err *NotAPointerError) Error() string {
return "must be a pointer"
}
// Trace returns the stack trace for the error
func (err *NotAPointerError) Trace() []string {
return err.trace
}
// NewNotAPointerError instantiates a NotAPointerError with a stack trace
func NewNotAPointerError() errors.TracerError {
return &NotAPointerError{trace: errors.GetStackTrace()}
}
// DuplicateRecordError is returned when a mysql error #1062 occurs for a PrimaryKey
type DuplicateRecordError struct {
SQLExecutionError
}
// NewDuplicateRecordError is returned when a records is created/updated with a duplicate primary key
func NewDuplicateRecordError(action SQLQueryType, stmt string, err error) errors.TracerError {
return &DuplicateRecordError{
SQLExecutionError{ErrMsg: err.Error(),
ReferenceID: generator.ID(dbErrPrefix),
Action: action,
message: duplicateRecordMsg,
Stmt: stmt,
trace: errors.GetStackTrace(),
},
}
}
// UniqueConstraintError is returned when a mysql error #1062 occurs for a Unique constraint
type UniqueConstraintError struct {
SQLExecutionError
}
// NewUniqueConstraintError is returned when a record is created/updated with a duplicate primary key
func NewUniqueConstraintError(action SQLQueryType, stmt string, err error) errors.TracerError {
return &UniqueConstraintError{
SQLExecutionError{ErrMsg: err.Error(),
ReferenceID: generator.ID(dbErrPrefix),
Action: action,
message: duplicateRecordMsg,
Stmt: stmt,
trace: errors.GetStackTrace(),
},
}
}
// DataTooLongError is returned when a mysql error #1406 occurs
type DataTooLongError struct {
SQLExecutionError
}
// NewDataTooLongError wrapping the passer error with references to the passed sql statement
// and action
func NewDataTooLongError(action SQLQueryType, stmt string, err error) errors.TracerError {
return &DataTooLongError{
SQLExecutionError{ErrMsg: err.Error(),
ReferenceID: generator.ID(dbErrPrefix),
Action: action,
message: dataTooLongMsg,
Stmt: stmt,
trace: errors.GetStackTrace(),
},
}
}
// InvalidForeignKeyError is returned when a mysql error #1452 occurs
type InvalidForeignKeyError struct {
SQLExecutionError
}
// NewInvalidForeignKeyError wrapping the passed error with references to the passed
// sql and action.
func NewInvalidForeignKeyError(action SQLQueryType, stmt string, err error) errors.TracerError {
return &InvalidForeignKeyError{
SQLExecutionError{ErrMsg: err.Error(),
ReferenceID: generator.ID(dbErrPrefix),
Action: action,
message: invalidForeignKeyMsg,
Stmt: stmt,
trace: errors.GetStackTrace(),
},
}
}
// DatabaseToStatus translates the passed db error into a grpc Status with appropriate
// status code
func DatabaseToStatus(primary qb.Table, dbError error) *status.Status {
// we have to call through so we have the same stack offset in both
// public functions
return databaseToStatus(primary, dbError)
}
func databaseToStatus(primary qb.Table, dbError error) *status.Status {
if nil == dbError {
return nil
}
var grpcStatus *status.Status
prefix := getLogPrefix(3)
switch dbError.(type) {
case *NotFoundError:
grpcStatus = status.Newf(codes.NotFound, fmt.Sprintf("%s %s not found", prefix, primary.GetName()))
case *DataTooLongError:
grpcStatus = status.Newf(codes.InvalidArgument, fmt.Sprintf("%s %s field too long: %s",
prefix, primary.GetName(), dbError))
case *DuplicateRecordError:
grpcStatus = status.Newf(codes.AlreadyExists, fmt.Sprintf("%s %s record already exists: %s",
prefix, primary.GetName(), dbError))
case *UniqueConstraintError:
grpcStatus = status.Newf(codes.AlreadyExists, fmt.Sprintf("%s %s unique constraint violation: %s",
prefix, primary.GetName(), dbError))
case *InvalidForeignKeyError:
grpcStatus = status.Newf(codes.InvalidArgument, fmt.Sprintf("%s %s foreign key violation: %s",
prefix, primary.GetName(), dbError))
case *ValidationError:
grpcStatus = status.Newf(codes.InvalidArgument, fmt.Sprintf("%s operation on %s had a validation error: %s",
prefix, primary.GetName(), dbError))
case *ConnectionError, *NotAPointerError:
_ = log.Errorf("[GAD.DAT.321] unexpected run time database error: %s", dbError)
grpcStatus = status.Newf(codes.Internal, fmt.Sprintf("%s internal system error encountered", prefix))
default:
_ = log.Errorf("[GAD.DAT.324] unhandled error type %T: %s", dbError, dbError.Error())
grpcStatus = status.Newf(codes.Aborted, fmt.Sprintf("%s (%s) database error encountered: %s",
prefix, primary.GetName(), dbError))
}
return grpcStatus
}
// DatabaseToApiError handles conversion from a database error to a GRPC friendly
// error with code.
func DatabaseToApiError(primary qb.Table, dbError error) error {
return databaseToStatus(primary, dbError).Err()
}
func getLogPrefix(frameSkip int) string {
_, filePath, lineNumber, ok := runtime.Caller(frameSkip)
if !ok {
_ = log.Warnf("failed to lookup runtime.Caller(%d) lookup failed", frameSkip)
return "[UNK]"
}
pathSplit := strings.Split(filePath, string(os.PathSeparator))
var a, b string
if len(pathSplit) > 2 {
a = getPrefixPart(pathSplit[len(pathSplit)-2])
b = getPrefixPart(pathSplit[len(pathSplit)-3])
} else {
a = "UNK"
b = getPrefixPart(filePath)
}
return fmt.Sprintf("[%s.%s.%d]", b, a, lineNumber)
}
func getPrefixPart(s string) string {
runes := []rune(strings.TrimSpace(s))
part := []rune{'_', '_', '_'}
for i := 0; i < len(part) && i < len(runes); i++ {
part[i] = runes[i]
}
return strings.ToUpper(string(part))
}
type assertion interface {
EqualError(theError error, errString string, msgAndArgs ...interface{}) bool
}
var logPrefixRegex = regexp.MustCompile(`([^\[]*\[\w{1,3}\.\w{1,3}\.)(\d+)(\][^\[]*)`)
var dbErrRegex = regexp.MustCompile(fmt.Sprintf("\\b%s_?[^\\W]*", dbErrPrefix))
// EqualLogError asserts that a function returned an error (i.e. not `nil`)
// and that it is equal to the provided error, ignoring line number in the log prefix
// and any database error ids.
func EqualLogError(assert assertion, theError error, errString string, msgAndArgs ...interface{}) bool {
normError := theError
normErrorStr := errString
if nil != theError {
// remove log prefix line numbers
normErrorStr = logPrefixRegex.ReplaceAllString(errString, "${1}${3}")
normError = errors.New(logPrefixRegex.ReplaceAllString(theError.Error(), "${1}${3}"))
// remove db error string
normErrorStr = dbErrRegex.ReplaceAllString(normErrorStr, "")
normError = errors.New(dbErrRegex.ReplaceAllString(normError.Error(), ""))
}
return assert.EqualError(normError, normErrorStr, msgAndArgs...)
}