-
Notifications
You must be signed in to change notification settings - Fork 2
/
errors.go
95 lines (87 loc) · 1.98 KB
/
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
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
91
92
93
94
95
package errors
import (
"fmt"
"io/ioutil"
"os"
"strings"
)
// RepoName is repository name, but should be dynamic
const RepoName = "hiromaiy"
// GetErrorStack is to return formatted error stack as string
func GetErrorStack(e error) string {
goPath := fmt.Sprintf("%s/src/", os.Getenv("GOPATH"))
//get directory names
dirNames, err := getDirNames(goPath)
if err != nil {
return ""
}
errs := strings.Split(fmt.Sprintf("%+v", e), "\n")
//format
//message is done after dir name is found
var (
msgIsDone bool
stacks string
idx int
)
for i := 0; i < len(errs); i++ {
if !msgIsDone {
if isFound(errs[i], dirNames) || errs[i] == "main.main" {
//handle as dir name
msgIsDone = true
} else {
//handle as message
continue
}
}
//stack trace
//1.funcName
paths := strings.Split(errs[i], "/")
if len(paths) == 1 {
stacks += fmt.Sprintf("\n[%d][func]%s(): ", idx, paths[0])
} else {
stacks += fmt.Sprintf("\n[%d][func]%s/%s(): ", idx, paths[len(paths)-2], paths[len(paths)-1])
}
//2.source code
i++
if strings.Contains(errs[i], RepoName) {
tmp := strings.Split(errs[i], RepoName)
stacks += fmt.Sprintf("[file].%s", tmp[1])
} else {
tmp := strings.Split(errs[i], "/")
stacks += fmt.Sprintf("[file].%s/%s", tmp[len(tmp)-2], tmp[len(tmp)-1])
break
}
if errs[i-1] == "main.main" {
break
}
idx++
}
return stacks
}
// ErrorStack is to return formatted error stack as string
func ErrorStack(err error) string {
stacks := GetErrorStack(err)
return fmt.Sprintf("%s%s", err.Error(), stacks)
}
func getDirNames(dir string) ([]string, error) {
files, err := ioutil.ReadDir(dir)
if err != nil {
return nil, err
}
var paths []string
for _, file := range files {
if file.IsDir() {
paths = append(paths, file.Name())
continue
}
}
return paths, nil
}
func isFound(targetStr string, dirNames []string) bool {
for _, dir := range dirNames {
if strings.Contains(targetStr, dir) {
return true
}
}
return false
}