-
Notifications
You must be signed in to change notification settings - Fork 1
/
errors.go
105 lines (92 loc) · 3.39 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
package lexicon
import (
"errors"
"fmt"
"runtime"
"strings"
)
// GenericErr represents generic lexicon error
var GenericErr = errors.New("lexicon error")
// InvalidParamErr represents generic error for invalid input parameter
var InvalidParamErr = errors.New("invalid parameter(s)")
// PatterErr represents generic error for invalid input parameter
var PatternErr = errors.New("invalid pattern")
// ValidationErr represents generic validation error
var ValidationErr = errors.New("validation error")
// NotImplementedApiErr represents generic not implemented api error
var NotImplementedApiErr = errors.New("not implemented api error")
// InvalidRequestErr represents generic invalid request error
var InvalidRequestErr = errors.New("invalid request error")
// Lexicon Error codes provides static representation of Lexicon errors, they cover 1xx range
const (
GenericErrorCode = iota + 10 // generic Lexicon error
ReaderErrorCode // 11 reader error
UnmarshalErrorCode // 12 unmarshal error
MarshalErrorCode // 13 marshal error
PatternErrorCode // 14 pattern error
ValidateErrorCode // 15 validate error
)
// helper function to create Lexicon error
func Error(err error, code int, msg, function string) error {
reason := "nil"
if err != nil {
reason = err.Error()
}
stackSlice := make([]byte, 1024*4)
s := runtime.Stack(stackSlice, false)
return &LexiconError{
Reason: reason,
Message: msg,
Code: code,
Function: function,
Stacktrace: fmt.Sprintf("\n%s", stackSlice[0:s]),
}
}
// LexiconError represents common structure for Lexicon errors
type LexiconError struct {
Reason string `json:"reason"` // error string
Message string `json:"message"` // additional message describing the issue
Function string `json:"function"` // Lexicon function
Code int `json:"code"` // Lexicon error code
Stacktrace string `json:"stacktrace"` // Go stack trace
}
// Error function implements details of Lexicon error message
func (e *LexiconError) Error() string {
sep := ": "
if strings.Contains(e.Reason, "LexiconError") { // nested error
sep += "nested "
}
return fmt.Sprintf(
"LexiconError Code:%d Description:%s Function:%s Message:%s Error%s%v",
e.Code, e.Explain(), e.Function, e.Message, sep, e.Reason)
}
// ErrorStacktrace function implements details of Lexicon error message and stacktrace
func (e *LexiconError) ErrorStacktrace() string {
sep := ": "
if strings.Contains(e.Reason, "LexiconError") { // nested error
sep += "nested "
}
return fmt.Sprintf(
"LexiconError Code:%d Description:%s Function:%s Message:%s Error%s%v Stacktrace: %v",
e.Code, e.Explain(), e.Function, e.Message, sep, e.Reason, e.Stacktrace)
}
// Explain provides explanation about the error
func (e *LexiconError) Explain() string {
switch e.Code {
case ReaderErrorCode:
return "Lexicon reader I/O error, e.g. unable to read HTTP POST payload"
case UnmarshalErrorCode:
return "Lexicon unable to parse JSON record"
case MarshalErrorCode:
return "Lexicon unable to convert record to JSON"
case GenericErrorCode:
return "Generic Lexicon error"
case PatternErrorCode:
return "Lexicon validation error when wrong pattern is provided"
case ValidateErrorCode:
return "Lexicon validation error, e.g. input parameter does not match lexicon rules"
default:
return "Not defined"
}
return "Not defined"
}