Change NewError to accept message as a fmt with values following
Taco de Wolff
4 years ago
1 | 1 | |
2 | 2 | import ( |
3 | 3 | "bytes" |
4 | "fmt" | |
5 | 4 | "io" |
6 | 5 | "strconv" |
7 | 6 | |
213 | 212 | // parse error |
214 | 213 | p.initBuf() |
215 | 214 | p.l.r.Move(-len(p.data)) |
216 | p.err = parse.NewErrorLexer(fmt.Sprintf("CSS parse error: unexpected token '%s' in declaration", string(p.data)), p.l.r) | |
215 | p.err = parse.NewErrorLexer(p.l.r, "CSS parse error: unexpected token '%s' in declaration", string(p.data)) | |
217 | 216 | p.l.r.Move(len(p.data)) |
218 | 217 | |
219 | 218 | if p.tt == RightBraceToken { |
336 | 335 | p.state = append(p.state, (*Parser).parseQualifiedRuleDeclarationList) |
337 | 336 | return BeginRulesetGrammar |
338 | 337 | } else if tt == ErrorToken { |
339 | p.err = parse.NewErrorLexer("CSS parse error: unexpected ending in qualified rule", p.l.r) | |
338 | p.err = parse.NewErrorLexer(p.l.r, "CSS parse error: unexpected ending in qualified rule") | |
340 | 339 | return ErrorGrammar |
341 | 340 | } else if tt == LeftParenthesisToken || tt == LeftBraceToken || tt == LeftBracketToken || tt == FunctionToken { |
342 | 341 | p.level++ |
381 | 380 | tt, data := p.popToken(false) |
382 | 381 | if tt != ColonToken { |
383 | 382 | p.l.r.Move(-len(data)) |
384 | p.err = parse.NewErrorLexer("CSS parse error: expected colon in declaration", p.l.r) | |
383 | p.err = parse.NewErrorLexer(p.l.r, "CSS parse error: expected colon in declaration") | |
385 | 384 | p.l.r.Move(len(data)) |
386 | 385 | p.pushBuf(ttName, dataName) |
387 | 386 | return p.parseDeclarationError(tt, data) |
438 | 437 | p.initBuf() |
439 | 438 | if tt, data := p.popToken(false); tt != ColonToken { |
440 | 439 | p.l.r.Move(-len(data)) |
441 | p.err = parse.NewErrorLexer("CSS parse error: expected colon in custom property", p.l.r) | |
440 | p.err = parse.NewErrorLexer(p.l.r, "CSS parse error: expected colon in custom property") | |
442 | 441 | p.l.r.Move(len(data)) |
443 | 442 | return ErrorGrammar |
444 | 443 | } |
15 | 15 | } |
16 | 16 | |
17 | 17 | // NewError creates a new error |
18 | func NewError(msg string, r io.Reader, offset int) *Error { | |
18 | func NewError(r io.Reader, offset int, message string, a ...interface{}) *Error { | |
19 | 19 | line, column, context := Position(r, offset) |
20 | if 0 < len(a) { | |
21 | message = fmt.Sprintf(message, a...) | |
22 | } | |
20 | 23 | return &Error{ |
21 | Message: msg, | |
24 | Message: message, | |
22 | 25 | Line: line, |
23 | 26 | Column: column, |
24 | 27 | Context: context, |
26 | 29 | } |
27 | 30 | |
28 | 31 | // NewErrorLexer creates a new error from an active Lexer. |
29 | func NewErrorLexer(msg string, l *buffer.Lexer) *Error { | |
32 | func NewErrorLexer(l *buffer.Lexer, message string, a ...interface{}) *Error { | |
30 | 33 | r := buffer.NewReader(l.Bytes()) |
31 | 34 | offset := l.Offset() |
32 | return NewError(msg, r, offset) | |
35 | return NewError(r, offset, message, a...) | |
33 | 36 | } |
34 | 37 | |
35 | 38 | // Positions returns the line, column, and context of the error. |
8 | 8 | ) |
9 | 9 | |
10 | 10 | func TestError(t *testing.T) { |
11 | err := NewError("message", bytes.NewBufferString("buffer"), 3) | |
11 | err := NewError(bytes.NewBufferString("buffer"), 3, "message") | |
12 | 12 | |
13 | 13 | line, column, context := err.Position() |
14 | 14 | test.T(t, line, 1, "line") |
21 | 21 | func TestErrorLexer(t *testing.T) { |
22 | 22 | l := buffer.NewLexer(bytes.NewBufferString("buffer")) |
23 | 23 | l.Move(3) |
24 | err := NewErrorLexer("message", l) | |
24 | err := NewErrorLexer(l, "message") | |
25 | 25 | |
26 | 26 | line, column, context := err.Position() |
27 | 27 | test.T(t, line, 1, "line") |
30 | 30 | |
31 | 31 | test.T(t, err.Error(), "message on line 1 and column 4\n 1: buffer\n ^", "error") |
32 | 32 | } |
33 | ||
34 | func TestErrorMessages(t *testing.T) { | |
35 | err := NewError(bytes.NewBufferString("buffer"), 3, "message %d", 5) | |
36 | test.T(t, err.Error(), "message 5 on line 1 and column 4\n 1: buffer\n ^", "error") | |
37 | } |
460 | 460 | } |
461 | 461 | } else if c == 0 { |
462 | 462 | if l.r.Err() == nil { |
463 | l.err = parse.NewErrorLexer("HTML parse error: unexpected null character", l.r) | |
463 | l.err = parse.NewErrorLexer(l.r, "HTML parse error: unexpected NULL character") | |
464 | 464 | } |
465 | 465 | return l.r.Shift() |
466 | 466 | } else { |
475 | 475 | break |
476 | 476 | } else if c == 0 { |
477 | 477 | if l.r.Err() == nil { |
478 | l.err = parse.NewErrorLexer("HTML parse error: unexpected null character", l.r) | |
478 | l.err = parse.NewErrorLexer(l.r, "HTML parse error: unexpected NULL character") | |
479 | 479 | } |
480 | 480 | return l.r.Shift() |
481 | 481 | } |
1 | 1 | package json |
2 | 2 | |
3 | 3 | import ( |
4 | "fmt" | |
5 | 4 | "io" |
6 | 5 | "strconv" |
7 | 6 | |
127 | 126 | state := p.state[len(p.state)-1] |
128 | 127 | if c == ',' { |
129 | 128 | if state != ArrayState && state != ObjectKeyState { |
130 | p.err = parse.NewErrorLexer("JSON parse error: unexpected comma character", p.r) | |
129 | p.err = parse.NewErrorLexer(p.r, "JSON parse error: unexpected comma character") | |
131 | 130 | return ErrorGrammar, nil |
132 | 131 | } |
133 | 132 | p.r.Move(1) |
138 | 137 | p.r.Skip() |
139 | 138 | |
140 | 139 | if p.needComma && c != '}' && c != ']' && c != 0 { |
141 | p.err = parse.NewErrorLexer("JSON parse error: expected comma character or an array or object ending", p.r) | |
140 | p.err = parse.NewErrorLexer(p.r, "JSON parse error: expected comma character or an array or object ending") | |
142 | 141 | return ErrorGrammar, nil |
143 | 142 | } else if c == '{' { |
144 | 143 | p.state = append(p.state, ObjectKeyState) |
146 | 145 | return StartObjectGrammar, p.r.Shift() |
147 | 146 | } else if c == '}' { |
148 | 147 | if state != ObjectKeyState { |
149 | p.err = parse.NewErrorLexer("JSON parse error: unexpected right brace character", p.r) | |
148 | p.err = parse.NewErrorLexer(p.r, "JSON parse error: unexpected right brace character") | |
150 | 149 | return ErrorGrammar, nil |
151 | 150 | } |
152 | 151 | p.needComma = true |
163 | 162 | } else if c == ']' { |
164 | 163 | p.needComma = true |
165 | 164 | if state != ArrayState { |
166 | p.err = parse.NewErrorLexer("JSON parse error: unexpected right bracket character", p.r) | |
165 | p.err = parse.NewErrorLexer(p.r, "JSON parse error: unexpected right bracket character") | |
167 | 166 | return ErrorGrammar, nil |
168 | 167 | } |
169 | 168 | p.state = p.state[:len(p.state)-1] |
174 | 173 | return EndArrayGrammar, p.r.Shift() |
175 | 174 | } else if state == ObjectKeyState { |
176 | 175 | if c != '"' || !p.consumeStringToken() { |
177 | p.err = parse.NewErrorLexer("JSON parse error: expected object key to be a quoted string", p.r) | |
176 | p.err = parse.NewErrorLexer(p.r, "JSON parse error: expected object key to be a quoted string") | |
178 | 177 | return ErrorGrammar, nil |
179 | 178 | } |
180 | 179 | n := p.r.Pos() |
181 | 180 | p.moveWhitespace() |
182 | 181 | if c := p.r.Peek(0); c != ':' { |
183 | p.err = parse.NewErrorLexer("JSON parse error: expected colon character after object key", p.r) | |
182 | p.err = parse.NewErrorLexer(p.r, "JSON parse error: expected colon character after object key") | |
184 | 183 | return ErrorGrammar, nil |
185 | 184 | } |
186 | 185 | p.r.Move(1) |
200 | 199 | } |
201 | 200 | c := p.r.Peek(0) // pick up movement from consumeStringToken to detect NULL or EOF |
202 | 201 | if c == 0 && p.r.Err() == nil { |
203 | p.err = parse.NewErrorLexer("JSON parse error: unexpected NULL character", p.r) | |
202 | p.err = parse.NewErrorLexer(p.r, "JSON parse error: unexpected NULL character") | |
204 | 203 | return ErrorGrammar, nil |
205 | 204 | } else if c == 0 { // EOF |
206 | 205 | return ErrorGrammar, nil |
207 | 206 | } |
208 | 207 | } |
209 | p.err = parse.NewErrorLexer(fmt.Sprintf("JSON parse error: unexpected character '%c'", c), p.r) | |
208 | p.err = parse.NewErrorLexer(p.r, "JSON parse error: unexpected character '%c'", c) | |
210 | 209 | return ErrorGrammar, nil |
211 | 210 | } |
212 | 211 |
121 | 121 | } |
122 | 122 | if c == 0 { |
123 | 123 | if l.r.Err() == nil { |
124 | l.err = parse.NewErrorLexer("XML parse error: unexpected null character", l.r) | |
124 | l.err = parse.NewErrorLexer(l.r, "XML parse error: unexpected NULL character") | |
125 | 125 | } |
126 | 126 | return ErrorToken, nil |
127 | 127 | } else if c != '>' && (c != '/' && c != '?' || l.r.Peek(1) != '>') { |
179 | 179 | return TextToken, l.text |
180 | 180 | } |
181 | 181 | if l.r.Err() == nil { |
182 | l.err = parse.NewErrorLexer("XML parse error: unexpected null character", l.r) | |
182 | l.err = parse.NewErrorLexer(l.r, "XML parse error: unexpected NULL character") | |
183 | 183 | } |
184 | 184 | return ErrorToken, nil |
185 | 185 | } |