forked from spyzhov/ajson
-
Notifications
You must be signed in to change notification settings - Fork 0
/
errors.go
102 lines (90 loc) · 2.02 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
package ajson
import "fmt"
// Error is common struct to provide internal errors
type Error struct {
Type ErrorType
Index int
Char byte
Message string
Value interface{}
}
// ErrorType is container for reflection type of error
type ErrorType int
const (
// WrongSymbol means that system found symbol than not allowed to be
WrongSymbol ErrorType = iota
// UnexpectedEOF means that data ended, leaving the node undone
UnexpectedEOF
// WrongType means that wrong type requested
WrongType
// WrongRequest means that wrong range requested
WrongRequest
// Unparsed means that json structure wasn't parsed yet
Unparsed
// UnsupportedType means that wrong type was given
UnsupportedType
)
func errorSymbol(b *buffer) error {
symbol, err := b.current()
if err != nil {
symbol = 0
}
return Error{
Type: WrongSymbol,
Index: b.index,
Char: symbol,
}
}
func errorAt(index int, symbol byte) error {
return Error{
Type: WrongSymbol,
Index: index,
Char: symbol,
}
}
func errorEOF(b *buffer) error {
return Error{
Type: UnexpectedEOF,
Index: b.index,
}
}
func errorType() error {
return Error{
Type: WrongType,
}
}
func unsupportedType(value interface{}) error {
return Error{
Type: UnsupportedType,
Value: value,
}
}
func errorUnparsed() error {
return Error{
Type: Unparsed,
}
}
func errorRequest(format string, args ...interface{}) error {
return Error{
Type: WrongRequest,
Message: fmt.Sprintf(format, args...),
}
}
// Error interface implementation
func (err Error) Error() string {
switch err.Type {
case WrongSymbol:
return fmt.Sprintf("wrong symbol '%s' at %d", []byte{err.Char}, err.Index)
case UnexpectedEOF:
return "unexpected end of file"
case WrongType:
return "wrong type of Node"
case UnsupportedType:
return fmt.Sprintf("unsupported type was given: '%T'", err.Value)
case Unparsed:
return "not parsed yet"
case WrongRequest:
return fmt.Sprintf("wrong request: %s", err.Message)
}
return fmt.Sprintf("unknown error: '%s' at %d", []byte{err.Char}, err.Index)
}