-
Notifications
You must be signed in to change notification settings - Fork 19
/
util.go
55 lines (44 loc) · 1.07 KB
/
util.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
package mongonet
import "fmt"
import "runtime"
import "strings"
// ---
type StackError struct {
Message string
Stacktrace []string
}
func NewStackErrorf(messageFmt string, args ...interface{}) *StackError {
return &StackError{
Message: fmt.Sprintf(messageFmt, args...),
Stacktrace: stacktrace(),
}
}
func (self *StackError) Error() string {
return fmt.Sprintf("%s\n\t%s", self.Message, strings.Join(self.Stacktrace, "\n\t"))
}
func stacktrace() []string {
ret := make([]string, 0, 2)
for skip := 2; true; skip++ {
_, file, line, ok := runtime.Caller(skip)
if ok == false {
break
}
ret = append(ret, fmt.Sprintf("at %s:%d", stripDirectories(file, 2), line))
}
return ret
}
func stripDirectories(filepath string, toKeep int) string {
var idxCutoff int
if idxCutoff = strings.LastIndex(filepath, "/"); idxCutoff == -1 {
return filepath
}
for dirToKeep := 0; dirToKeep < toKeep; dirToKeep++ {
switch idx := strings.LastIndex(filepath[:idxCutoff], "/"); idx {
case -1:
break
default:
idxCutoff = idx
}
}
return filepath[idxCutoff+1:]
}