-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patherror.go
73 lines (60 loc) · 1.12 KB
/
error.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
package qerror
import (
"bytes"
"runtime"
"strconv"
"time"
)
type BaseError struct {
dt time.Time
stacktrace []CallerInfo
}
type CallerInfo struct {
File string
Line int
FuncName string
}
func New(offset int) *BaseError {
e := &BaseError{
dt: time.Now(),
}
var (
pc uintptr
ok bool
info CallerInfo
)
for i := 1 + offset; ; i++ {
pc, info.File, info.Line, ok = runtime.Caller(i)
if !ok {
break
}
f := runtime.FuncForPC(pc)
if f != nil {
info.FuncName = f.Name()
}
e.stacktrace = append(e.stacktrace, info)
}
return e
}
func (e *BaseError) Error() string {
buf := &bytes.Buffer{}
buf.WriteString("DateTime: ")
buf.WriteString(e.dt.String())
buf.WriteString("\nStacktrace:")
for _, caller := range e.stacktrace {
buf.WriteByte('\n')
buf.WriteByte('\t')
buf.WriteString(caller.FuncName)
buf.WriteString(" at ")
buf.WriteString(caller.File)
buf.WriteByte(':')
buf.WriteString(strconv.Itoa(caller.Line))
}
return buf.String()
}
func (e *BaseError) Stacktrace() []CallerInfo {
return e.stacktrace
}
func (e *BaseError) Dt() time.Time {
return e.dt
}