-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patherrors.go
199 lines (177 loc) · 5.25 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
// Copyright 2015 ALRUX Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
/*
Package errors provides extended error handling.
When you need to retain more information about an error message than a single string allows, just substitute this package for the one in the standard library.
The `New` function still accepts a single string as argument, so no code will be broken. Where you need to include additional information, you can provide it to `New` in a `Desc` structure instead of the string, or you can add it to the error message using one of its setter methods.
The additional information can be used for smarter error handling and logging:
- `Level` differentiates between warnings, regular errors, panics converted to errors, and fatal errors;
- `Code` allows custom classification and prioritizing, by using ranges or bit-level masks;
- `Info` offers a store for arbitrary data and messages, besides the main error `Text`; the special string "debug.stack", if present as an element in the Info slice, is automatically replaced by a stack trace at the point the error message has been created.
*/
package errors
import (
"fmt"
"runtime"
)
// Error represents an error descriptor capable of storing more detailed
// information than possible with the standard errors package. It ensures that
// any implementation also satisfies the built-in `error` interface.
type Error interface {
error
Level() int8
SetLevel(int8) Error
Code() int
SetCode(int) Error
Text() string
SetText(string) Error
Info() []string
AddInfo(...string) Error
Log(Logger) Error
}
// Desc provides a means to convey detailed error information to New.
type Desc struct {
Level int8
Code int
Text string
Info []string
}
// errorMessage stores information about one error occurrence. Pointers to it
// implement the Error interface.
type errorMessage struct {
level int8
code int
text string
info []string
}
// New returns an error descriptor containing the given information. It accepts
// either a string or a Desc structure (or a pointer to either).
//
// It is a drop-in replacement for the corresponding function from the standard package.
func New(desc interface{}) Error {
switch desc := desc.(type) {
case string:
return &errorMessage{level: ERROR, text: desc}
case *string:
return &errorMessage{level: ERROR, text: *desc}
case Desc:
return newFromE(&desc)
case *Desc:
return newFromE(desc)
}
return newFromE(&Desc{
Code: ERR_NEW_ARG,
Text: fmt.Sprintf("unsupported error descriptor type %T", desc),
Info: []string{
fmt.Sprintf("%T", desc),
"debug.stack",
},
})
}
func newFromE(desc *Desc) Error {
return (&errorMessage{
level: ERROR,
code: desc.Code,
text: desc.Text,
}).addInfo(3, desc.Info...).SetLevel(desc.Level)
}
// Log sends the error to the provided log, using the appropriate
// logging function: FATAL conditions are logged using Fatal(), PANIC using
// Panic(), and anything else using Print().
func (em *errorMessage) Log(log Logger) Error {
switch em.level {
case FATAL:
log.Fatal(em)
case PANIC:
log.Panic(em)
default:
log.Print(em)
}
return em
}
// Level returns the error level.
func (em *errorMessage) Level() int8 {
return em.level
}
// SetLevel sets the error level.
func (em *errorMessage) SetLevel(l int8) Error {
if l >= minLevel && l <= maxLevel {
em.level = l
}
return em
}
// Code returns the error code.
func (em *errorMessage) Code() int {
return em.code
}
// SetCode sets the error code.
func (em *errorMessage) SetCode(c int) Error {
em.code = c
return em
}
// Text returns the error text.
func (em *errorMessage) Text() string {
return em.text
}
// SetText sets the error text.
func (em *errorMessage) SetText(t string) Error {
em.text = t
return em
}
// Info returns the error info.
func (em *errorMessage) Info() []string {
return em.info
}
// addInfo adds (more) error info.
func (em *errorMessage) addInfo(calldepth int, s ...string) Error {
for i, line := range s {
if line == "debug.stack" {
calldepth *= 2
buffer := make([]byte, 4096)
buffer = buffer[:runtime.Stack(buffer, true)]
var p1, p2, l int
for j, c := range buffer {
if c == 10 {
if l == 0 {
p1 = j + 1
} else if l == calldepth {
p2 = j + 1
break
}
l++
}
}
if p2 > 0 {
s[i] = string(buffer[:p1]) + string(buffer[p2:])
} else {
s[i] = string(buffer)
}
break
}
}
em.info = append(em.info, s...)
return em
}
// AddInfo adds (more) error info.
func (em *errorMessage) AddInfo(s ...string) Error {
return em.addInfo(2, s...)
}
// Error returns a text containing the error message and code;
// it is useful for satisfying the `error` interface.
func (em *errorMessage) Error() string {
if em.code != 0 {
return em.text + fmt.Sprintf(" (code: 0x%04x)", em.code)
}
return em.text
}