Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patherror.go
More file actions
Latest commit
101 lines (81 loc) · 2.6 KB
/
Copy patherror.go
File metadata and controls
101 lines (81 loc) · 2.6 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
package errors
import (
"bytes"
"errors"
"fmt"
"runtime"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
)
// WithCaller sets the position at which the error was formed. If this is
// false, errors will be wrapped with no location information.
varWithCaller=true
// WithCallerVerbose just adds file:line components alongside the caller information.
varWithCallerVerbose=false
// WithStack provides a stack trace at the point the error was yielded, either
// via Error() method or direct function call. This may be set at any time to
// get a stack trace when calling Error() on a WithLocation error.
varWithStack=false
// StackBufferSize can impact performance and should be adjusted if you have
// small or larger stack traces than the default size, this is the bytes
// buffer that will be created for calculating stack traces.
varStackBufferSize=8192
// WithLocation is a kind of error that stores the location at which it was
// created at. Dereference `Err` to get at the actual error.
typeWithLocationstruct {
Filestring
Lineint
Locuintptr
Errerror
}
func (wlWithLocation) Error() string {
s:=wl.Err.Error()
ifwl.Loc!=0 {
verbose:=""
ifwl.File!=""&&wl.Line!=0&&WithCallerVerbose {
verbose=fmt.Sprintf("[%v:%v]", wl.File, wl.Line)
}
s=fmt.Sprintf("[%v]%v: %v", runtime.FuncForPC(wl.Loc).Name(), verbose, s)
}
sbuf:=bytes.NewBufferString(s)
ifWithStack {
buf:=make([]byte, StackBufferSize)
sbuf.WriteString("\nSTACK TRACE:\n-----------\n")
sbuf.Write(buf[:runtime.Stack(buf, false)])
}
returnsbuf.String()
}
// New creates a new error with caller identification.
funcNew(sstring) error {
returnmkError(errors.New(s))
}
// Errorf returns a formatted error with caller identification.
funcErrorf(fstring, args...interface{}) error {
returnmkError(fmt.Errorf(f, args...))
}
// WithError combines two errors to be compatible with errors.As() and
// similar functions in the stdlib errors package.
funcWithError(errerror, fstring, args...interface{}) error {
returnmkError(fmt.Errorf("%v: %w", fmt.Sprintf(f, args...), err))
}
// GRPC formats a GRPC error in our caller ID pattern.
funcGRPC(code codes.Code, fstring, args...interface{}) error {
returntransformWithCaller(2, status.Errorf(code, f, args...))
}
functransformWithCaller(depthint, eerror) error {
ifWithCaller {
pc, file, line, ok:=runtime.Caller(depth)
ifok {
returnWithLocation{
File: file,
Line: line,
Loc: pc,
Err: e,
}
}
}
returnWithLocation{Err: e}
}
funcmkError(eerror) error {
returntransformWithCaller(3, e)
}