forked from postmanlabs/observability-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patherror.go
46 lines (38 loc) · 1.17 KB
/
error.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
package apidump
import (
"github.com/akitasoftware/akita-libs/api_schema"
"github.com/pkg/errors"
)
type ApidumpError struct {
err error
// Type of error to be reported as part of error telemetry.
errType api_schema.ApidumpErrorType
}
func NewApidumpError(errType api_schema.ApidumpErrorType, msg string) ApidumpError {
return ApidumpError{
err: errors.New(msg),
errType: errType,
}
}
func NewApidumpErrorf(errType api_schema.ApidumpErrorType, format string, args ...interface{}) ApidumpError {
return ApidumpError{
err: errors.Errorf(format, args...),
errType: errType,
}
}
func (e ApidumpError) Error() string {
return e.err.Error()
}
// Returns the error type if err contains an ApidumpError, or ApidumpError_Other
// otherwise.
func GetErrorType(err error) api_schema.ApidumpErrorType {
return GetErrorTypeWithDefault(err, api_schema.ApidumpError_Other)
}
// Returns the error type if err contains an ApidumpError, or def otherwise.
func GetErrorTypeWithDefault(err error, def api_schema.ApidumpErrorType) api_schema.ApidumpErrorType {
var apidumpErr ApidumpError
if ok := errors.As(err, &apidumpErr); ok {
return apidumpErr.errType
}
return def
}