Repository navigation
Expand file tree
/
Copy pathgerror_error_stack.go
More file actions
173 lines (163 loc) · 4.27 KB
/
Copy pathgerror_error_stack.go
File metadata and controls
173 lines (163 loc) · 4.27 KB
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
// Copyright GoFrame Author(https://goframe.org). All Rights Reserved.
//
// This Source Code Form is subject to the terms of the MIT License.
// If a copy of the MIT was not distributed with this file,
// You can obtain one at https://github.com/gogf/gf.
package gerror
import (
"bytes"
"container/list"
"fmt"
"runtime"
"strings"
"github.com/gogf/gf/v2/internal/consts"
"github.com/gogf/gf/v2/internal/errors"
)
// stackInfo manages stack info of certain error.
type stackInfo struct {
Index int // Index is the index of current error in whole error stacks.
Message string // Error information string.
Lines *list.List // Lines contains all error stack lines of current error stack in sequence.
}
// stackLine manages each line info of stack.
type stackLine struct {
Function string // Function name, which contains its full package path.
FileLine string // FileLine is the source file name and its line number of Function.
}
// Stack returns the error stack information as string.
func (err *Error) Stack() string {
if err == nil {
return ""
}
var (
loop = err
index = 1
infos []*stackInfo
isStackModeBrief = errors.IsStackModeBrief()
)
for loop != nil {
info := &stackInfo{
Index: index,
Message: fmt.Sprintf("%-v", loop),
}
index++
infos = append(infos, info)
loopLinesOfStackInfo(loop.stack, info, isStackModeBrief)
if loop.error != nil {
if e, ok := loop.error.(*Error); ok {
loop = e
} else {
infos = append(infos, &stackInfo{
Index: index,
Message: loop.error.Error(),
})
break
}
} else {
break
}
}
filterLinesOfStackInfos(infos)
return formatStackInfos(infos)
}
// filterLinesOfStackInfos removes repeated lines, which exist in subsequent stacks, from top errors.
func filterLinesOfStackInfos(infos []*stackInfo) {
var (
ok bool
set = make(map[string]struct{})
info *stackInfo
line *stackLine
removes []*list.Element
)
for i := len(infos) - 1; i >= 0; i-- {
info = infos[i]
if info.Lines == nil {
continue
}
for n, e := 0, info.Lines.Front(); n < info.Lines.Len(); n, e = n+1, e.Next() {
line = e.Value.(*stackLine)
if _, ok = set[line.FileLine]; ok {
removes = append(removes, e)
} else {
set[line.FileLine] = struct{}{}
}
}
if len(removes) > 0 {
for _, e := range removes {
info.Lines.Remove(e)
}
}
removes = removes[:0]
}
}
// formatStackInfos formats and returns error stack information as string.
func formatStackInfos(infos []*stackInfo) string {
buffer := bytes.NewBuffer(nil)
for i, info := range infos {
fmt.Fprintf(buffer, "%d. %s\n", i+1, info.Message)
if info.Lines != nil && info.Lines.Len() > 0 {
formatStackLines(buffer, info.Lines)
}
}
return buffer.String()
}
// formatStackLines formats and returns error stack lines as string.
func formatStackLines(buffer *bytes.Buffer, lines *list.List) string {
var (
line *stackLine
space = " "
length = lines.Len()
)
for i, e := 0, lines.Front(); i < length; i, e = i+1, e.Next() {
line = e.Value.(*stackLine)
// Graceful indent.
if i >= 9 {
space = " "
}
fmt.Fprintf(
buffer,
" %d).%s%s\n %s\n",
i+1, space, line.Function, line.FileLine,
)
}
return buffer.String()
}
// loopLinesOfStackInfo iterates the stack info lines and produces the stack line info.
func loopLinesOfStackInfo(st stack, info *stackInfo, isStackModeBrief bool) {
if st == nil {
return
}
for _, p := range st {
if fn := runtime.FuncForPC(p - 1); fn != nil {
file, line := fn.FileLine(p - 1)
if isStackModeBrief {
// filter whole GoFrame packages stack paths.
if strings.Contains(file, consts.StackFilterKeyForGoFrame) {
continue
}
} else {
// package path stack filtering.
if strings.Contains(file, stackFilterKeyLocal) {
continue
}
}
// Avoid stack string like "`autogenerated`"
if strings.Contains(file, "<") {
continue
}
// Ignore GO ROOT paths.
if goRootForFilter != "" &&
len(file) >= len(goRootForFilter) &&
file[0:len(goRootForFilter)] == goRootForFilter {
continue
}
if info.Lines == nil {
info.Lines = list.New()
}
info.Lines.PushBack(&stackLine{
Function: fn.Name(),
FileLine: fmt.Sprintf(`%s:%d`, file, line),
})
}
}
}