This repository has been archived by the owner on May 10, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
errgo.go
90 lines (77 loc) · 1.76 KB
/
errgo.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
package errgo
import (
"fmt"
"path/filepath"
"runtime"
)
// Here info about code location with a string representation formatted as
// <dir>/<file>.go@<line>:<package>.<function>()
func Here(skip ...int) Loc {
sk := 1
if len(skip) > 0 && skip[0] > 1 {
sk = skip[0]
}
pc, fileName, fileLine, ok := runtime.Caller(sk)
fn := runtime.FuncForPC(pc)
var res Loc
defer func() {
if res.str != "" {
return
}
res.str = res.FuncName
}()
if !ok {
res.FuncName = "N/A"
return res
}
res.FileName = fileName
res.FileLine = fileLine
res.FuncName = fn.Name()
fileName = filepath.Join(filepath.Base(filepath.Dir(fileName)), filepath.Base(fileName))
res.str = fmt.Sprintf("%s@%d:%s()", fileName, res.FileLine, res.FuncName)
return res
}
// Loc info about code location with a string representation formatted as
// <dir>/<file>.go@<line>:<package>.<function>()
type Loc struct {
FuncName string
FileName string
FileLine int
str string
}
func (l Loc) String() string {
return l.str
}
//-----------------------------------------------------------------------------
type marker struct {
loc Loc
err error
}
func (m *marker) Error() string {
var cause string
if m.err != nil {
cause = m.err.Error()
} else {
cause = "CAUSE:N/A"
}
return m.loc.String() + " " + cause
}
// Cause implements Causer interface
func (m *marker) Cause() error { return m.err }
// Loc returns info on code location
func (m *marker) Loc() Loc { return m.loc }
// Mark ...
func Mark(cause error) error {
if cause == nil {
return nil
}
return &marker{
loc: Here(2),
err: cause,
}
}
//-----------------------------------------------------------------------------
// Markf ...
func Markf(format string, v ...interface{}) error {
return fmt.Errorf(Here(2).String()+" "+format, v...)
}