generated from layer5io/layer5-repo-template
-
Notifications
You must be signed in to change notification settings - Fork 71
/
errors.go
145 lines (133 loc) · 5.57 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
// Package errors provides types and function used to implement MeshKit compatible errors across the Meshery code base.
//
// Uniform definition of errors using these types and functions facilitates extracting error information directly from the code
// and generating and publishing error code reference documentation automatically.
// The error utility tool in this module, /cmd/errorutil, is part of this toolchain.
//
// It depends on a few conventions in order to work:
//
// 1) An error code defined as a constant or variable (preferably constant), of type string.
// The naming convention for these variables is the regex "^Err[A-Z].+Code$", e.g. ErrApplyManifestCode.
// The initial value of the code is a placeholder string, e.g. "replace_me", set by the developer.
// The final value of the code is an integer, set by the errorutil tool, as part of a CI workflow.
//
// 2) Error details defined using the function New(...) in this package, see below for details.
//
// Additionally, the following conventions apply:
//
// Errors are defined in each package, in a file named error.go.
// Errors are namespaced to Meshery components, i.e. they need to be unique within a component.
// (Often, a specific component corresponds to one git repository.)
// There are no predefined error code ranges for components. Every component is free to use its own range.
// Codes carry no meaning, as e.g. HTTP status codes do.
//
// See also the doc command of errorutil, and https://docs.meshery.io/project/contributing/contributing-error.
//
// Example:
//
// const ErrConnectCode = "11000"
// func ErrConnect(err error) error {
// return errors.New(ErrConnectCode,
// errors.Alert,
// []string{"Connection to broker failed"},
// []string{err.Error()},
// []string{"Endpoint might not be reachable"},
// []string{"Make sure the NATS endpoint is reachable"})
// }
package errors
import "strings"
// Deprecated: NewDefault is deprecated, use New(...) instead.
func NewDefault(code string, ldescription ...string) *Error {
return &Error{
Code: code,
Severity: None,
ShortDescription: NoneString,
LongDescription: ldescription,
ProbableCause: NoneString,
SuggestedRemediation: NoneString,
}
}
// New returns a MeshKit error using the provided parameters.
//
// In order to create MeshKit compatible errors that can be handled by the errorutil tool, consider the following points:
//
// The first parameter, 'code', has to be passed as the error code constant (or variable), not a string literal.
//
// The second parameter, 'severity', has its own type; consult its Go-doc for further details.
//
// The remaining parameters are string arrays for short and long description, probable cause, and suggested remediation.
// Use string literals in these string arrays, not constants or variables, for any static texts or format strings.
// Capitalize the first letter of each statement.
// Call expressions can be used but will be ignored by the tool when exporting error details for the documentation.
// Do not concatenate strings using the '+' operator, just add multiple elements to the string array.
//
// Example:
//
// errors.New(ErrConnectCode,
// errors.Alert,
// []string{"Connection to broker failed"},
// []string{err.Error()},
// []string{"Endpoint might not be reachable"},
// []string{"Make sure the NATS endpoint is reachable"})
func New(code string, severity Severity, sdescription []string, ldescription []string, probablecause []string, remedy []string) *Error {
return &Error{
Code: code,
Severity: severity,
ShortDescription: sdescription,
LongDescription: ldescription,
ProbableCause: probablecause,
SuggestedRemediation: remedy,
}
}
func NewV2(code string, severity Severity, sdescription []string, ldescription []string, probablecause []string, remedy []string, additionalInfo interface{}) *ErrorV2 {
return &ErrorV2{
Code: code,
Severity: severity,
ShortDescription: sdescription,
LongDescription: ldescription,
ProbableCause: probablecause,
SuggestedRemediation: remedy,
AdditionalInfo: additionalInfo,
}
}
func (e *Error) Error() string { return strings.Join(e.LongDescription[:], ".") }
func (e *Error) ErrorV2(additionalInfo interface{}) ErrorV2 {
return ErrorV2{Code: e.Code, Severity: e.Severity, ShortDescription: e.ShortDescription, LongDescription: e.LongDescription, ProbableCause: e.ProbableCause, SuggestedRemediation: e.SuggestedRemediation, AdditionalInfo: additionalInfo}
}
func GetCode(err error) string {
if obj := err.(*Error); obj != nil && obj.Code != " " {
return obj.Code
}
return strings.Join(NoneString[:], "")
}
func GetSeverity(err error) Severity {
if obj := err.(*Error); obj != nil {
return obj.Severity
}
return None
}
func GetSDescription(err error) string {
if obj := err.(*Error); obj != nil {
return strings.Join(err.(*Error).ShortDescription[:], ".")
}
return strings.Join(NoneString[:], "")
}
func GetCause(err error) string {
if obj := err.(*Error); obj != nil {
return strings.Join(err.(*Error).ProbableCause[:], ".")
}
return strings.Join(NoneString[:], "")
}
func GetRemedy(err error) string {
if obj := err.(*Error); obj != nil {
return strings.Join(err.(*Error).SuggestedRemediation[:], ".")
}
return strings.Join(NoneString[:], "")
}
func Is(err error) (*Error, bool) {
if err != nil {
er, ok := err.(*Error)
return er, ok
}
return nil, false
}