-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
errors.go
252 lines (221 loc) · 6.17 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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
// Copyright 2022 Ainsley Clark. All rights reserved.
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.
// @see https://blog.carlmjohnson.net/post/2020/working-with-errors-as/
package errors
import (
"bytes"
"database/sql/driver"
"encoding/json"
"errors"
"fmt"
"net/http"
"runtime"
"strconv"
"strings"
)
// Application error codes.
const (
// CONFLICT - An action cannot be performed.
CONFLICT = "conflict"
// INTERNAL - Error within the application.
INTERNAL = "internal"
// INVALID - Validation failed.
INVALID = "invalid"
// NOTFOUND - Entity does not exist.
NOTFOUND = "not_found"
// UNKNOWN - Application unknown error.
UNKNOWN = "unknown"
// MAXIMUMATTEMPTS - More than allowed action.
MAXIMUMATTEMPTS = "maximum_attempts"
// EXPIRED - Subscription expired.
EXPIRED = "expired"
)
var (
// DefaultCode is the default code returned when
// none is specified.
DefaultCode = INTERNAL
// GlobalError is a general message when no error message
// has been found.
GlobalError = "An error has occurred."
)
// Error defines a standard application error.
type Error struct {
// The application error code.
Code string `json:"code" bson:"code"`
// A human-readable message to send back to the end user.
Message string `json:"message" bson:"message"`
// Defines what operation is currently being run.
Operation string `json:"operation" bson:"op"`
// The error that was returned from the caller.
Err error `json:"error" bson:"error"`
fileLine string
pcs []uintptr
}
// Error returns the string representation of the error
// message by implementing the error interface.
func (e *Error) Error() string {
var buf bytes.Buffer
// Print the error code if there is one.
if e.Code != "" {
buf.WriteString("<" + e.Code + "> ")
}
// Print the file-line, if any.
if e.fileLine != "" {
buf.WriteString(e.fileLine + " - ")
}
// Print the current operation in our stack, if any.
if e.Operation != "" {
buf.WriteString(e.Operation + ": ")
}
// Print the original error message, if any.
if e.Err != nil {
buf.WriteString(e.Err.Error() + ", ")
}
// Print the message, if any.
if e.Message != "" {
buf.WriteString(e.Message)
}
return strings.TrimSuffix(strings.TrimSpace(buf.String()), ",")
}
// NewE returns an Error with the DefaultCode.
func NewE(err error, message, op string) *Error {
return newError(err, message, DefaultCode, op)
}
// ErrorF returns an Error with the DefaultCode and
// formatted message arguments.
func ErrorF(err error, op, format string, args ...any) *Error {
return NewE(err, fmt.Sprintf(format, args...), op)
}
// FileLine returns the file and line in which the error
// occurred.
func (e *Error) FileLine() string {
return e.fileLine
}
// Unwrap unwraps the original error message.
func (e *Error) Unwrap() error {
return e.Err
}
// Wrap returns an error annotating err with a stack trace
// at the point Wrap is called, and the supplied message.
// If err is nil, Wrap returns nil.
func Wrap(err error, message string) *Error {
if err == nil {
return nil
}
return &Error{
Err: err,
Message: message,
}
}
// HTTPStatusCode is a convenience method used to get the appropriate
// HTTP response status code for the respective error type.
func (e *Error) HTTPStatusCode() int {
status := http.StatusInternalServerError
switch e.Code {
case CONFLICT:
return http.StatusConflict
case INVALID:
return http.StatusBadRequest
case NOTFOUND:
return http.StatusNotFound
case EXPIRED:
return http.StatusPaymentRequired
case MAXIMUMATTEMPTS:
return http.StatusTooManyRequests
}
return status
}
// RuntimeFrames returns function/file/line information.
func (e *Error) RuntimeFrames() *runtime.Frames {
return runtime.CallersFrames(e.pcs)
}
// ProgramCounters returns the slice of PC values associated
// with the error.
func (e *Error) ProgramCounters() []uintptr {
return e.pcs
}
// StackTrace returns a string representation of the errors
// stacktrace, where each trace is separated by a newline
// and tab '\t'.
func (e *Error) StackTrace() string {
trace := make([]string, 0, 100)
rFrames := e.RuntimeFrames()
frame, ok := rFrames.Next()
line := strconv.Itoa(frame.Line)
trace = append(trace, frame.Function+"(): "+e.Message)
for ok {
trace = append(trace, "\t"+frame.File+":"+line)
frame, ok = rFrames.Next()
}
return strings.Join(trace, "\n")
}
// StackTraceSlice returns a string slice of the errors
// stacktrace.
func (e *Error) StackTraceSlice() []string {
trace := make([]string, 0, 100)
rFrames := e.RuntimeFrames()
frame, ok := rFrames.Next()
line := strconv.Itoa(frame.Line)
trace = append(trace, frame.Function+"(): "+e.Message)
for ok {
trace = append(trace, frame.File+":"+line)
frame, ok = rFrames.Next()
}
return trace
}
// wrappingError is the wrapping error features the error
// and file line in strings suitable for json.Marshal.
type wrappingError struct {
Code string `json:"code"`
Message string `json:"message"`
Operation string `json:"operation"`
Err string `json:"error"`
FileLine string `json:"file_line"`
}
// MarshalJSON implements encoding/Marshaller to wrap the
// error as a string if there is one.
func (e *Error) MarshalJSON() ([]byte, error) {
err := wrappingError{
Code: e.Code,
Message: e.Message,
Operation: e.Operation,
}
if e.Err != nil {
err.Err = e.Err.Error()
err.FileLine = e.fileLine
}
return json.Marshal(err)
}
// UnmarshalJSON implements encoding/Marshaller to unmarshal
// the wrapping error to type Error.
func (e *Error) UnmarshalJSON(data []byte) error {
var err wrappingError
mErr := json.Unmarshal(data, &err)
if mErr != nil {
return mErr
}
e.Code = err.Code
e.Message = err.Message
e.Operation = err.Operation
e.fileLine = err.FileLine
if err.Err != "" {
e.Err = errors.New(err.Err)
}
return nil
}
// Scan implements the sql.Scanner interface.
func (e *Error) Scan(value any) error {
if value == nil {
return nil
}
buf, ok := value.([]byte)
if !ok || buf == nil {
return fmt.Errorf("scan not supported for *errors.Error")
}
return json.Unmarshal(buf, e)
}
// Value implements the driver.Valuer interface.
func (e *Error) Value() (driver.Value, error) {
return json.Marshal(e)
}