-
Notifications
You must be signed in to change notification settings - Fork 1
/
ez.go
71 lines (62 loc) · 1.7 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
package ez
import (
"bytes"
"fmt"
)
// 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"`
// Data about the error
Data map[string]interface{} `json:"data,omitempty"`
}
// 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}
}
// Root creates a new root error
func Root(op, code, message string) *Error {
return New(op, code, message, nil)
}
// 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 {
if e, ok := err.(*Error); ok {
return &Error{
Op: op,
Code: e.Code,
Message: e.Message,
Data: e.Data,
Err: err,
}
}
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)
}