-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathErrors.go
165 lines (147 loc) · 4.09 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
package libICP
import (
"encoding/base64"
"fmt"
"runtime"
"strings"
"github.com/OpenICP-BR/asn1"
)
type stringI interface {
String() string
}
type CodedError interface {
error
Code() ErrorCode
CodeString() string
}
// This is the same as CodedError. There are two names just to make the API more obvious to the reader when a functions returns an array of errors and an array of warnings.
type CodedWarning interface {
CodedError
}
type pairErrorCodePos struct {
Error interface{}
Line int
File string
Function string
}
type MultiError struct {
message string
code ErrorCode
line int
file string
function string
parameters map[string]interface{}
errors []pairErrorCodePos
locked bool
}
func NewMultiError(message string, code ErrorCode, parameters map[string]interface{}, errors ...interface{}) MultiError {
merr := MultiError{}
merr.code = code
merr.message = message
merr.parameters = parameters
merr.errors = make([]pairErrorCodePos, len(errors))
for i := 0; i < len(errors); i++ {
merr.errors[i].Error = errors[i]
merr.errors[i].Function, merr.errors[i].File, merr.errors[i].Line = get_stack_pos(2)
}
merr.mark_position()
return merr
}
func (perr pairErrorCodePos) String() string {
return fmt.Sprintf("%s:%s:%d %s", perr.Function, perr.File, perr.Line, perr.Error)
}
func (merr MultiError) Error() string {
ans := fmt.Sprintf("%s (%s:%d): %s: %s", merr.function, merr.file, merr.line, merr.code, merr.message)
// Print parameters
if merr.parameters != nil && len(merr.parameters) > 0 {
ans += "\nParameters:"
for k, v := range merr.parameters {
switch v := v.(type) {
case []byte:
tmp := base64.StdEncoding.EncodeToString(v)
ans += fmt.Sprintf("\n\t%s:(base64): %+v", k, tmp)
case asn1.RawContent:
tmp := base64.StdEncoding.EncodeToString(v)
ans += fmt.Sprintf("\n\t%s:(base64): %+v", k, tmp)
default:
ans += fmt.Sprintf("\n\t%s: %+v", k, v)
}
}
}
// Print encapsulated errors
if merr.errors != nil && len(merr.errors) > 0 {
ans += "\nErrors: ["
for _, err := range merr.errors {
if err.Error == nil {
continue
}
tmp := "-"
switch terr := err.Error.(type) {
case error:
tmp = terr.Error()
case stringI:
tmp = terr.String()
default:
tmp = fmt.Sprintf("%+v", terr)
}
tmp = strings.Replace(tmp, "\n", "\n\t", -1)
tmp = strings.Replace(tmp, "\t", "\t\t", -1)
ans += fmt.Sprintf("\n\t(%s:%s:%d) %s", err.Function, err.File, err.Line, tmp)
}
ans += "\n]"
}
return ans
}
func (merr MultiError) Code() ErrorCode {
return merr.code
}
func (merr MultiError) CodeString() string {
return merr.code.String()
}
func (merr *MultiError) SetParam(key string, val interface{}) error {
if merr.locked {
return NewMultiError("attempted to edit locekd MultiError", ERR_LOCKED_MULTI_ERROR, nil, nil, nil)
}
if merr.parameters == nil {
merr.parameters = make(map[string]interface{})
}
merr.parameters[key] = val
return nil
}
func (merr *MultiError) AppendError(err error) error {
if merr.locked {
return NewMultiError("attempted to edit locekd MultiError", ERR_LOCKED_MULTI_ERROR, nil, nil, nil)
}
if merr.errors == nil {
merr.errors = make([]pairErrorCodePos, 0)
}
p := pairErrorCodePos{}
p.Error = err
p.Function, p.File, p.Line = get_stack_pos(2)
merr.errors = append(merr.errors, p)
return nil
}
// Sets the line number and function to match where this function is called and prevents further editing. Also returns itself.
func (merr *MultiError) Finish() *MultiError {
merr.mark_position()
merr.locked = true
return merr
}
func get_stack_pos(depth int) (string, string, int) {
function := "?"
// Get information about who created this error
pc, file, line, _ := runtime.Caller(depth)
// Print only the last part of the file path
tmp := strings.Split(file, "/")
file = tmp[len(tmp)-1]
// Try to get the function name
f := runtime.FuncForPC(pc)
if f != nil {
function = f.Name()
}
return function, file, line
}
func (merr *MultiError) mark_position() {
// Get information about who created this error
merr.function, merr.file, merr.line = get_stack_pos(3)
}