-
Notifications
You must be signed in to change notification settings - Fork 38
/
errors.go
48 lines (40 loc) · 1.1 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
package jstream
import (
"fmt"
"strconv"
)
// Predefined errors
var (
ErrSyntax = DecoderError{msg: "invalid character"}
ErrUnexpectedEOF = DecoderError{msg: "unexpected end of JSON input"}
)
type errPos [2]int // line number, byte offset where error occurred
type DecoderError struct {
msg string // description of error
context string // additional error context
pos errPos
atChar byte
readerErr error // underlying reader error, if any
}
func (e DecoderError) ReaderErr() error { return e.readerErr }
func (e DecoderError) Error() string {
loc := fmt.Sprintf("%s [%d,%d]", quoteChar(e.atChar), e.pos[0], e.pos[1])
s := fmt.Sprintf("%s %s: %s", e.msg, e.context, loc)
if e.readerErr != nil {
s += "\nreader error: " + e.readerErr.Error()
}
return s
}
// quoteChar formats c as a quoted character literal
func quoteChar(c byte) string {
// special cases - different from quoted strings
if c == '\'' {
return `'\''`
}
if c == '"' {
return `'"'`
}
// use quoted string with different quotation marks
s := strconv.Quote(string(c))
return "'" + s[1:len(s)-1] + "'"
}