-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
errors.go
129 lines (118 loc) · 4.46 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
package main
// errors module
// Copyright (c) 2022 - Valentin Kuznetsov <vkuznet@gmail.com>
import (
"errors"
"fmt"
"strings"
)
// GenericErr represents generic dbs error
var GenericErr = errors.New("dbs error")
// DatabaseErr represents generic database error
var DatabaseErr = errors.New("database error")
// InvalidParamErr represents generic error for invalid input parameter
var InvalidParamErr = errors.New("invalid parameter(s)")
// NotImplementedApiErr represents generic not implemented api error
var NotImplementedApiErr = errors.New("not implemented api error")
// DBS Error codes provides static representation of DBS errors, they cover 1xx range
const (
GenericErrorCode = iota + 100 // generic DBS error
DatabaseErrorCode // 101 database error
TransactionErrorCode // 102 transaction error
QueryErrorCode // 103 query error
RowsScanErrorCode // 104 row scan error
SessionErrorCode // 105 db session error
CommitErrorCode // 106 db commit error
ParseErrorCode // 107 parser error
GetIDErrorCode // 109 get id db error
InsertErrorCode // 110 db insert error
UpdateErrorCode // 111 update error
LastInsertErrorCode // 112 db last insert error
ValidateErrorCode // 113 validation error
DecodeErrorCode // 115 decode error
EncodeErrorCode // 116 encode error
NotImplementedApiCode // 119 not implemented API error
ReaderErrorCode // 120 io reader error
WriterErrorCode // 121 io writer error
UnmarshalErrorCode // 122 json unmarshal error
MarshalErrorCode // 123 marshal error
)
// DBError represents common structure for DB errors
type DBError struct {
Reason string `json:"reason"` // error string
Message string `json:"message"` // additional message describing the issue
Function string `json:"function"` // DB function
Code int `json:"code"` // DB error code
}
// Error function implements details of DBS error message
func (e *DBError) Error() string {
sep := ": "
if strings.Contains(e.Reason, "DBError") { // nested error
sep += "nested "
}
return fmt.Sprintf(
"DBError Code:%d Description:%s Function:%s Message:%s Error%s%v",
e.Code, e.Explain(), e.Function, e.Message, sep, e.Reason)
}
// Explain provides explanation of DB errors
//gocyclo:ignore
func (e *DBError) Explain() string {
switch e.Code {
case GenericErrorCode:
return "Generic DBS error"
case DatabaseErrorCode:
return "DBS DB error"
case TransactionErrorCode:
return "DBS DB transaction error"
case QueryErrorCode:
return "DBS DB query error, e.g. mailformed SQL statement"
case RowsScanErrorCode:
return "DBS DB row scane error, e.g. fail to get DB record from a database"
case SessionErrorCode:
return "DBS DB session error"
case CommitErrorCode:
return "DBS DB transaction commit error"
case ParseErrorCode:
return "DBS parser error, e.g. mailformed input parameter to the query"
case GetIDErrorCode:
return "DBS DB ID error for provided entity, e.g. there is no record in DB for provided value"
case InsertErrorCode:
return "DBS DB insert record error"
case UpdateErrorCode:
return "DBS DB update record error"
case LastInsertErrorCode:
return "DBS DB laster insert record error, e.g. fail to obtain last inserted ID"
case ValidateErrorCode:
return "DBS validation error, e.g. input parameter does not match lexicon rules"
case DecodeErrorCode:
return "DBS decode record failure, e.g. mailformed JSON"
case EncodeErrorCode:
return "DBS encode record failure, e.g. unable to convert structure to JSON"
case NotImplementedApiCode:
return "DBS Not implemented API error"
case ReaderErrorCode:
return "DBS reader I/O error, e.g. unable to read HTTP POST payload"
case WriterErrorCode:
return "DBS writer I/O error, e.g. unable to write record to HTTP response"
case UnmarshalErrorCode:
return "DBS unable to parse JSON record"
case MarshalErrorCode:
return "DBS unable to convert record to JSON"
default:
return "Not defined"
}
return "Not defined"
}
// helper function to create dbs error
func Error(err error, code int, msg, function string) error {
reason := "nil"
if err != nil {
reason = err.Error()
}
return &DBError{
Reason: reason,
Message: msg,
Code: code,
Function: function,
}
}