-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy patherrors.go
64 lines (51 loc) · 1.91 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
package jsonutils
import (
"fmt"
"yunion.io/x/pkg/errors"
)
const (
ErrJsonDictFailInsert = errors.Error("fail to insert object")
ErrInvalidJsonDict = errors.Error("not a valid JSONDict")
ErrInvalidJsonArray = errors.Error("not a valid JSONArray")
ErrInvalidJsonInt = errors.Error("not a valid number")
ErrInvalidJsonFloat = errors.Error("not a valid float")
ErrInvalidJsonBoolean = errors.Error("not a valid boolean")
ErrInvalidJsonString = errors.Error("not a valid string")
ErrJsonDictKeyNotFound = errors.Error("key not found")
ErrUnsupported = errors.Error("unsupported operation")
ErrOutOfKeyRange = errors.Error("out of key range")
ErrOutOfIndexRange = errors.Error("out of index range")
ErrInvalidChar = errors.Error("invalid char")
ErrInvalidHex = errors.Error("invalid hex")
ErrInvalidRune = errors.Error("invalid 4 byte rune")
ErrTypeMismatch = errors.Error("unmarshal type mismatch")
ErrArrayLengthMismatch = errors.Error("unmarshal array length mismatch")
ErrInterfaceUnsupported = errors.Error("do not known how to deserialize json into this interface type")
ErrMapKeyMustString = errors.Error("map key must be string")
ErrMissingInputField = errors.Error("missing input field")
ErrNilInputField = errors.Error("nil input field")
ErrYamlMissingDictKey = errors.Error("Cannot find JSONDict key")
ErrYamlIllFormat = errors.Error("Illformat")
)
type JSONError struct {
pos int
substr string
msg string
}
func (e *JSONError) Error() string {
return fmt.Sprintf("JSON error %s at %d: %s...", e.msg, e.pos, e.substr)
}
func NewJSONError(str []byte, pos int, msg string) *JSONError {
sublen := 10
start := pos - sublen
end := pos + sublen
if start < 0 {
start = 0
}
if end > len(str) {
end = len(str)
}
substr := append(str[start:pos], '^')
substr = append(substr, str[pos:end]...)
return &JSONError{pos: pos, substr: string(substr), msg: msg}
}