-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patherrors.go
150 lines (122 loc) · 3.64 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
package errors
import (
"fmt"
"github.com/pkg/errors"
)
// ErrorCode holds the type of errors
type ErrorCode string
const (
InternalError ErrorCode = "Internal Error"
NotFound ErrorCode = "Not Found"
InvalidArgument ErrorCode = "Invalid Argument"
Unauthenticated ErrorCode = "Unauthenticated"
PermissionDenied ErrorCode = "Permission Denied"
Unknown ErrorCode = "Unknown"
)
type customError struct {
code ErrorCode
originalError error
displayMessage string
}
// New creates a new customError
func (code ErrorCode) New(msg string) error {
return customError{code: code, originalError: errors.New(msg)}
}
// Newf creates a new customError with formatted message
func (code ErrorCode) Newf(msg string, args ...interface{}) error {
return customError{code: code, originalError: fmt.Errorf(msg, args...)}
}
// Wrap creates a new wrapped error
func (code ErrorCode) Wrap(err error, msg string) error {
return code.Wrapf(err, msg)
}
// Wrapf creates a new wrapped error with formatted message
func (code ErrorCode) Wrapf(err error, msg string, args ...interface{}) error {
return customError{code: code, originalError: errors.Wrapf(err, msg, args...)}
}
// New creates a no type error
func New(msg string) error {
return customError{code: InternalError, originalError: errors.New(msg)}
}
// Newf creates an InternalError error with formatted message
func Newf(msg string, args ...interface{}) error {
return customError{
code: InternalError,
originalError: errors.New(fmt.Sprintf(msg, args...)),
}
}
// Wrapf an error with format string
func Wrapf(err error, msg string, args ...interface{}) error {
wrappedError := errors.Wrapf(err, msg, args...)
var customErr customError
if errors.As(err, &customErr) {
return customError{
code: customErr.code,
originalError: wrappedError,
}
}
return customError{code: InternalError, originalError: wrappedError}
}
// Wrap an error with a string
func Wrap(err error, msg string) error {
return Wrapf(err, msg)
}
// WithDisplayMessage returns a error containing a display message
func WithDisplayMessage(err error, msg string) error {
var customErr customError
if errors.As(err, &customErr) {
return customError{
code: customErr.code,
originalError: err,
displayMessage: msg,
}
}
return customError{code: InternalError, originalError: err, displayMessage: msg}
}
// Code retrives the error code from an error, defaults to InternalError
func Code(err error) ErrorCode {
var customErr customError
if errors.As(err, &customErr) {
return customErr.code
}
return InternalError
}
// Cause retrives the original error
// Note that it will return the error created internally from github.com/pkg/errors
func Cause(err error) error {
var customErr customError
if errors.As(err, &customErr) {
return Cause(errors.Cause(customErr.originalError))
}
return errors.Cause(err)
}
// DisplayMessage retrives the display message
func DisplayMessage(err error) string {
var customErr customError
if errors.As(err, &customErr) {
if customErr.displayMessage != "" {
return customErr.displayMessage
}
return string(customErr.code)
}
return string(InternalError)
}
// Error returns the mssage of a customError
func (error customError) Error() string {
return error.originalError.Error()
}
type stackTracer interface {
StackTrace() errors.StackTrace
}
// StackTrace retrives the stack trace of an error
func StackTrace(err error) errors.StackTrace {
custom, ok := err.(customError)
if ok {
return StackTrace(custom.originalError)
}
_, ok = err.(stackTracer)
if !ok {
err = errors.WithStack(err)
}
return err.(stackTracer).StackTrace()[1:]
}