-
Notifications
You must be signed in to change notification settings - Fork 3
/
errors.go
152 lines (132 loc) · 2.71 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
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
package kvql
import (
"fmt"
"strings"
)
var (
_ error = (*SyntaxError)(nil)
_ error = (*ExecuteError)(nil)
_ QueryBinder = (*SyntaxError)(nil)
_ QueryBinder = (*ExecuteError)(nil)
DefaultErrorPadding = 7
)
type QueryBinder interface {
BindQuery(query string)
SetPadding(pad int)
}
type SyntaxError struct {
Query string
Message string
Pos int
Padding int
}
func NewSyntaxError(pos int, msg string, args ...any) error {
return &SyntaxError{
Message: fmt.Sprintf(msg, args...),
Pos: pos,
Padding: DefaultErrorPadding,
}
}
func (e *SyntaxError) BindQuery(query string) {
e.Query = query
}
func (e *SyntaxError) SetPadding(pad int) {
e.Padding = pad
}
func (e *SyntaxError) Error() string {
if e.Query == "" {
return e.simpleError()
}
return e.queryError()
}
func (e *SyntaxError) queryError() string {
ret := outputQueryAndErrPos(e.Query, e.Pos, e.Padding)
pad := generatePads(e.Padding)
ret += fmt.Sprintf("%sSyntax Error: %s", pad, e.Message)
return ret
}
func (e *SyntaxError) simpleError() string {
return fmt.Sprintf("Syntax Error: %s at %d", e.Message, e.Pos)
}
type ExecuteError struct {
Query string
Message string
Pos int
Padding int
}
func NewExecuteError(pos int, msg string, args ...any) error {
return &ExecuteError{
Pos: pos,
Message: fmt.Sprintf(msg, args...),
Padding: DefaultErrorPadding,
}
}
func (e *ExecuteError) BindQuery(query string) {
e.Query = query
}
func (e *ExecuteError) SetPadding(pad int) {
e.Padding = pad
}
func (e *ExecuteError) Error() string {
if e.Query == "" {
return e.simpleError()
}
return e.queryError()
}
func (e *ExecuteError) simpleError() string {
return fmt.Sprintf("Execute Error: %s at %d", e.Message, e.Pos)
}
func (e *ExecuteError) queryError() string {
ret := outputQueryAndErrPos(e.Query, e.Pos, e.Padding)
pad := generatePads(e.Padding)
ret += fmt.Sprintf("%sExecute Error: %s", pad, e.Message)
return ret
}
func generatePads(pad int) string {
ret := ""
for i := 0; i < pad; i++ {
ret += " "
}
return ret
}
func outputQueryAndErrPos(query string, pos int, adjust int) string {
tquery := strings.TrimSpace(query)
qlen := len(tquery)
if pos == -1 {
pos = qlen
}
trimLeft := false
trimRight := false
if qlen > 70 {
if pos <= 35 {
tquery = tquery[0:70]
trimRight = true
} else {
trimLeft = true
trim := pos - 35
restLen := qlen - trim
if restLen > 70 {
restLen = 70
trimRight = true
}
tquery = tquery[trim : trim+restLen]
pos -= trim
}
}
ret := ""
errPos := pos + adjust
if trimLeft {
ret = "... "
errPos += 4
}
ret += tquery
if trimRight {
ret += " ..."
}
ret += "\n"
for i := 0; i < errPos; i++ {
ret += " "
}
ret += "^--\n"
return ret
}