-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patherror.go
61 lines (50 loc) · 1.3 KB
/
error.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
package njson
import "fmt"
type typeError struct {
Type Type
Want Type
}
// newTypeError returns a type mismatch error.
func newTypeError(t, want Type) error {
return typeError{t, want}
}
func (e typeError) Error() string {
if e.Want&e.Type != 0 {
return fmt.Sprintf("Invalid value for type %s", e.Type)
}
return fmt.Sprintf("Invalid type %s not in %v", e.Type, e.Want.Types())
}
// ParseError signifies an invalid token in JSON data
type ParseError struct {
got interface{}
want interface{}
pos int
typ Type
}
// Type returns type of value that was being parsed when the error ocurred.
func (e *ParseError) Type() Type {
return e.typ
}
// Pos returns the offset at which the error ocurred
func (e *ParseError) Pos() int {
return e.pos
}
func (e *ParseError) Error() string {
if e == nil {
return fmt.Sprintf("%v", error(nil))
}
return fmt.Sprintf("Invalid token %q != %q at position %d while scanning %s", e.got, e.want, e.pos, e.typ.String())
}
// UnexpectedEOF signifies incomplete JSON data
type UnexpectedEOF Type
func (e UnexpectedEOF) Error() string {
return fmt.Sprintf("Unexpected end of input while scanning %s", Type(e).String())
}
func abort(pos int, typ Type, got interface{}, want interface{}) error {
return &ParseError{
pos: pos,
typ: typ,
got: got,
want: want,
}
}