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
|
package error
import (
"bufio"
"fmt"
"os"
"sort"
"strings"
)
type SpecErrors []*SpecError
func (e SpecErrors) Error() string {
if len(e) == 0 {
return ""
}
sorted := make([]*SpecError, len(e))
copy(sorted, e)
sort.SliceStable(sorted, func(i, j int) bool {
return sorted[i].Row < sorted[j].Row
})
sort.SliceStable(sorted, func(i, j int) bool {
return sorted[i].FilePath < sorted[j].FilePath
})
var b strings.Builder
fmt.Fprintf(&b, "%v", sorted[0])
for _, err := range sorted[1:] {
fmt.Fprintf(&b, "\n%v", err)
}
return b.String()
}
type SpecError struct {
Cause error
Detail string
FilePath string
SourceName string
Row int
Col int
}
func (e *SpecError) Error() string {
var b strings.Builder
if e.SourceName != "" {
fmt.Fprintf(&b, "%v: ", e.SourceName)
}
if e.Row != 0 && e.Col != 0 {
fmt.Fprintf(&b, "%v:%v: ", e.Row, e.Col)
}
fmt.Fprintf(&b, "error: %v", e.Cause)
if e.Detail != "" {
fmt.Fprintf(&b, ": %v", e.Detail)
}
line := readLine(e.FilePath, e.Row)
if line != "" {
fmt.Fprintf(&b, "\n %v", line)
}
return b.String()
}
func readLine(filePath string, row int) string {
if filePath == "" || row <= 0 {
return ""
}
f, err := os.Open(filePath)
if err != nil {
return ""
}
i := 1
s := bufio.NewScanner(f)
for s.Scan() {
if i == row {
return s.Text()
}
i++
}
return ""
}
|