-
Notifications
You must be signed in to change notification settings - Fork 0
/
errors.go
56 lines (51 loc) · 1009 Bytes
/
errors.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
package logging
import (
"errors"
"fmt"
"runtime"
"strings"
)
func assert(e interface{}, ntfy ...interface{}) {
switch e.(type) {
case nil:
case bool:
if !e.(bool) {
mesg := "assertion failed"
if len(ntfy) > 0 {
mesg = ntfy[0].(string)
if len(ntfy) > 1 {
mesg = fmt.Sprintf(mesg, ntfy[1:]...)
}
}
panic(errors.New(mesg))
}
case error:
panic(e)
default:
panic(fmt.Errorf("assert: expect error or bool, got %T", e))
}
}
type exception []string
func (e exception) Error() string {
return strings.Join(e, "\n")
}
func trace(msg string, args ...interface{}) error {
ex := exception{fmt.Sprintf(msg, args...)}
n := 1
for {
n++
pc, file, line, ok := runtime.Caller(n)
if !ok {
break
}
f := runtime.FuncForPC(pc)
name := f.Name()
if strings.HasPrefix(name, "runtime.") {
continue
}
fn := strings.Split(file, "/")
file = strings.Join(fn[len(fn)-2:], "/")
ex = append(ex, fmt.Sprintf("\t(%s:%d) %s", file, line, name))
}
return ex
}