forked from amamla/jsonparser
-
Notifications
You must be signed in to change notification settings - Fork 1
/
errors.go
75 lines (60 loc) · 2.2 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
package jsonparser
// Errors
var (
keyPathNotFoundError = 1
unknownValueTypeError = 2
malformedJsonError = 3
malformedStringError = 4
malformedArrayError = 5
malformedObjectError = 6
malformedValueError = 7
malformedStringEscapeError = 8
)
type ParserError struct {
offset int
s string
errorType int
}
func (err *ParserError) Error() string {
return err.s
}
func (err *ParserError) Offset() int {
return err.offset
}
func MalformedObjectError(offset int) error {
return &ParserError{s: "Value looks like object, but can't find closing '}' symbol", offset: offset, errorType: malformedObjectError}
}
func UnknownValueTypeError(offset int) error {
return &ParserError{s:"Unknown value type", offset: offset, errorType: unknownValueTypeError}
}
func KeyPathNotFoundError(offset int) error {
return &ParserError{s: "Key path not found", offset: offset, errorType: keyPathNotFoundError}
}
func MalformedJsonError(offset int) error {
return &ParserError{s: "Malformed JSON error", offset: offset, errorType: malformedJsonError}
}
func MalformedStringError(offset int) error {
return &ParserError{s: "Value is string, but can't find closing '\"' symbol", offset: offset, errorType: malformedStringError}
}
func MalformedArrayError(offset int) error {
return &ParserError{s: "Value is array, but can't find closing ']' symbol", offset: offset, errorType: malformedArrayError}
}
func MalformedValueError(offset int) error {
return &ParserError{s: "Value looks like Number/Boolean/None, but can't find its end: ',' or '}' symbol", offset: offset, errorType: malformedValueError}
}
func MalformedStringEscapeError(offset int) error {
return &ParserError{s: "Encountered an invalid escape sequence in a string", offset: offset, errorType: malformedStringEscapeError}
}
func addToErrorOffset(err error, offset int) error {
if parserError, ok := err.(*ParserError); ok {
parserError.offset = parserError.offset + offset
}
return err
}
func isKeyPathNotFoundError(err error) bool {
return isParserErrorOfType(err, keyPathNotFoundError)
}
func isParserErrorOfType(err error, errorType int) bool {
parserError, ok := err.(*ParserError)
return ok && parserError.errorType == errorType
}