forked from trustwallet/blockatlas
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patherrors.go
More file actions
135 lines (121 loc) · 2.47 KB
/
errors.go
File metadata and controls
135 lines (121 loc) · 2.47 KB
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
package errors
import (
"encoding/json"
"errors"
"fmt"
"runtime"
"strings"
)
type Params map[string]interface{}
// Error represents a error's specification.
type Error struct {
Err error
Type Type
meta map[string]interface{}
stack []string
}
var (
_ error = (*Error)(nil)
)
func (e *Error) isEmpty() bool {
return e.meta == nil && e.Type == TypeNone && e.Err == nil
}
func (e *Error) Error() string {
r, err := e.MarshalJSON()
if err != nil {
return e.Err.Error()
}
return string(r)
}
func (e *Error) String() string {
msg := e.Err.Error()
if e.Type != TypeNone {
msg = fmt.Sprintf("%s | Type: %s", msg, e.Type.String())
}
if len(e.Meta()) > 0 {
msg = fmt.Sprintf("%s | Meta: %s", msg, e.Meta())
}
if len(e.stack) > 0 {
msg = fmt.Sprintf("%s | Stack: %s", msg, e.stack)
}
return msg
}
// SetMeta sets the error's meta data.
func (e *Error) SetMeta(data Params) *Error {
e.meta = data
return e
}
func (e *Error) Meta() string {
r, err := json.Marshal(e.meta)
if err != nil {
return ""
}
return string(r)
}
// JSON creates a properly formatted JSON
func (e *Error) JSON() interface{} {
p := Params{}
if e.meta != nil {
p["meta"] = e.meta
}
if e.Err != nil {
p["error"] = e.Err.Error()
}
if e.Type != TypeNone {
p["type"] = e.Type.String()
}
if len(e.stack) > 0 {
p["stack"] = e.stack
}
return p
}
// MarshalJSON implements the json.Marshaller interface.
func (e *Error) MarshalJSON() ([]byte, error) {
return json.Marshal(e.JSON())
}
func (e *Error) PushToSentry() *Error {
SendError(e)
return e
}
// T create a new error with runtime stack trace.
func T(args ...interface{}) *Error {
e := E(args...)
for i := 1; i <= 5; i++ {
_, fn, line, ok := runtime.Caller(i)
if ok {
e.stack = append(e.stack, fmt.Sprintf("%s:%d", fn, line))
}
}
return e
}
// E create a new error.
func E(args ...interface{}) *Error {
e := &Error{Type: TypeNone, meta: make(Params)}
var message []string
for _, arg := range args {
switch arg := arg.(type) {
case nil:
continue
case string:
message = append(message, arg)
case *Error:
message = append([]string{arg.Err.Error()}, message...)
appendMap(e.meta, arg.meta)
case error:
message = append([]string{arg.Error()}, message...)
case Type:
e.Type = arg
case Params:
appendMap(e.meta, arg)
case map[string]interface{}:
appendMap(e.meta, arg)
default:
continue
}
}
if len(message) > 0 {
msg := strings.Join(message[:], ": ")
e.Err = errors.New(msg)
}
return e
}