-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathderror.go
59 lines (49 loc) · 1.43 KB
/
derror.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
package errors
import (
"fmt"
"github.com/gofrs/uuid"
)
// derror is the internal error type used by this package.
// Its name contains a d from decorated and for avoiding to clash with the
// standard error interface.
// derror holds a Code which identifies the error, an ID which uniquely
// identifies the error (it can be useful for correlating different log entries
// for identifying that it's the same error instance), metadata associated
// to the instance of the error and the call stack.
type derror struct {
c Code
id uuid.UUID
mds mDatas
werr error
cs callStack
}
// Format satisfies the fmt.Formatter interface.
func (err derror) Format(state fmt.State, verb rune) {
if verb == 's' {
_, _ = fmt.Fprintf(state, "%s: %s", err.c.String(), err.c.Message())
return
}
if verb != 'v' {
return
}
_, _ = fmt.Fprintf(state, "%s: %s\n\tid: %s\n\tmetadata: %v", err.c.String(), err.c.Message(), err.id, err.mds)
if !state.Flag('+') {
return
}
if err.werr != nil {
_, _ = fmt.Fprintf(state, "\n\twrapped error: %+v", err.werr)
}
if len(err.cs) == 0 {
return
}
if state.Flag('-') {
_, _ = fmt.Fprintf(state, "\n\tcall stack (compacted):\n%-v", err.cs)
} else {
_, _ = fmt.Fprintf(state, "\n\tcall stack:\n%v", err.cs)
}
}
// Error satisfies the standard error interface.
// It returns a string which is the same output than fmt.Printf("%s", err).
func (err derror) Error() string {
return fmt.Sprintf("%s", err)
}