-
Notifications
You must be signed in to change notification settings - Fork 0
/
trace.go
34 lines (29 loc) · 849 Bytes
/
trace.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
package errortrace
import (
"fmt"
"path/filepath"
"runtime"
)
// Wrap wraps an error with the file & line number that it came from. If the
// input error is nil, this will do nothing and return nil.
func Wrap(err error) error {
if err == nil {
return err
}
_, file, line, _ := runtime.Caller(1)
return tracedError{file, line, err}
}
// Errorf creates an error that captures the file & line that it was created at.
// The error content is defined by the format string and args like fmt.Errorf.
func Errorf(format string, args ...interface{}) error {
_, file, line, _ := runtime.Caller(1)
return tracedError{file, line, fmt.Errorf(format, args...)}
}
type tracedError struct {
file string
line int
err error
}
func (t tracedError) Error() string {
return fmt.Sprintf("[%s:%d] %s", filepath.Base(t.file), t.line, t.err.Error())
}