-
Notifications
You must be signed in to change notification settings - Fork 1
/
ez.go
108 lines (96 loc) · 3.1 KB
/
ez.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
package ez
import (
"bytes"
"fmt"
)
// Application error codes
const (
ECONFLICT = "conflict" // action cannot be performed
EINTERNAL = "internal" // internal error
EINVALID = "invalid" // validation failed
ENOTFOUND = "not_found" // entity does not exist
ENOTAUTHORIZED = "not_authorized" // requester does not have permissions to perform action
ENOTAUTHENTICATED = "not_authenticated" // requester is not authenticated
ERESOURCEEXHAUSTED = "resource_exhausted" // the resource has been exhausted
ENOTIMPLEMENTED = "not_implemented" // the operation has not been implemented
EUNAVAILABLE = "unavailable" // the system or operation is not available
)
// Error defines a standar application error
type Error struct {
// Machine readable code
Code string `json:"code"`
// Human readable message
Message string `json:"message"`
// Logical operation
Op string `json:"op"`
// Nested error
Err error `json:"err"`
}
// New creates and returns a new error
func New(op, code, message string, err error) *Error {
return &Error{Op: op, Code: code, Message: message, Err: err}
}
// Wrap returns a new error that contains the passed error but with a different operation, useful for creating stacktraces
func Wrap(op string, err error) *Error {
return &Error{Op: op, Code: ErrorCode(err), Message: ErrorMessage(err), Err: err}
}
// Error returns the string representation of the error message.
func (e *Error) Error() string {
var buf bytes.Buffer
// Print the current operation in our stack, if any.
if e.Op != "" {
fmt.Fprintf(&buf, "%s: ", e.Op)
}
// If wrapping an error, print its Error() message.
// Otherwise print the error code & message.
if e.Err != nil {
buf.WriteString(e.Err.Error())
} else {
if e.Code != "" {
fmt.Fprintf(&buf, "<%s> ", e.Code)
}
buf.WriteString(e.Message)
}
return buf.String()
}
// String returns a simplified string representation of the error message
func (e *Error) String() string {
return fmt.Sprintf(`%s <%s> "%s"`, e.Op, e.Code, e.Message)
}
// ErrorCode returns the code of the root error, if available.
// Otherwise returns EINTERNAL.
func ErrorCode(err error) string {
if err == nil {
return ""
} else if e, ok := err.(*Error); ok && e.Code != "" {
return e.Code
} else if ok && e.Err != nil {
return ErrorCode(e.Err)
}
return EINTERNAL
}
// ErrorMessage returns the human-readable message of the error, if available.
// Otherwise returns a generic error message.
func ErrorMessage(err error) string {
if err == nil {
return ""
} else if e, ok := err.(*Error); ok && e.Message != "" {
return e.Message
} else if ok && e.Err != nil {
return ErrorMessage(e.Err)
}
return "An internal error has occurred. Please contact technical support."
}
// ErrorStacktrace prints a human-redable stacktrace of all nested errors.
func ErrorStacktrace(err error) {
if err == nil {
return
} else if e, ok := err.(*Error); ok {
fmt.Println(e.String())
ErrorStacktrace(e.Err)
} else if ok && e.Err != nil {
fmt.Println(e.String())
} else {
fmt.Println(err.Error())
}
}