1 package errors
2
3 import (
4 "fmt"
5 "reflect"
6 "strconv"
7 )
8
9 type InvalidUTF8Error struct {
10 S string
11 }
12
13 func (e *InvalidUTF8Error) Error() string {
14 return fmt.Sprintf("json: invalid UTF-8 in string: %s", strconv.Quote(e.S))
15 }
16
17 type InvalidUnmarshalError struct {
18 Type reflect.Type
19 }
20
21 func (e *InvalidUnmarshalError) Error() string {
22 if e.Type == nil {
23 return "json: Unmarshal(nil)"
24 }
25
26 if e.Type.Kind() != reflect.Ptr {
27 return fmt.Sprintf("json: Unmarshal(non-pointer %s)", e.Type)
28 }
29 return fmt.Sprintf("json: Unmarshal(nil %s)", e.Type)
30 }
31
32
33 type MarshalerError struct {
34 Type reflect.Type
35 Err error
36 sourceFunc string
37 }
38
39 func (e *MarshalerError) Error() string {
40 srcFunc := e.sourceFunc
41 if srcFunc == "" {
42 srcFunc = "MarshalJSON"
43 }
44 return fmt.Sprintf("json: error calling %s for type %s: %s", srcFunc, e.Type, e.Err.Error())
45 }
46
47
48 func (e *MarshalerError) Unwrap() error { return e.Err }
49
50
51 type SyntaxError struct {
52 msg string
53 Offset int64
54 }
55
56 func (e *SyntaxError) Error() string { return e.msg }
57
58
59
60
61
62 type UnmarshalFieldError struct {
63 Key string
64 Type reflect.Type
65 Field reflect.StructField
66 }
67
68 func (e *UnmarshalFieldError) Error() string {
69 return fmt.Sprintf("json: cannot unmarshal object key %s into unexported field %s of type %s",
70 strconv.Quote(e.Key), e.Field.Name, e.Type.String(),
71 )
72 }
73
74
75
76 type UnmarshalTypeError struct {
77 Value string
78 Type reflect.Type
79 Offset int64
80 Struct string
81 Field string
82 }
83
84 func (e *UnmarshalTypeError) Error() string {
85 if e.Struct != "" || e.Field != "" {
86 return fmt.Sprintf("json: cannot unmarshal %s into Go struct field %s.%s of type %s",
87 e.Value, e.Struct, e.Field, e.Type,
88 )
89 }
90 return fmt.Sprintf("json: cannot unmarshal %s into Go value of type %s", e.Value, e.Type)
91 }
92
93
94
95 type UnsupportedTypeError struct {
96 Type reflect.Type
97 }
98
99 func (e *UnsupportedTypeError) Error() string {
100 return fmt.Sprintf("json: unsupported type: %s", e.Type)
101 }
102
103 type UnsupportedValueError struct {
104 Value reflect.Value
105 Str string
106 }
107
108 func (e *UnsupportedValueError) Error() string {
109 return fmt.Sprintf("json: unsupported value: %s", e.Str)
110 }
111
112 func ErrSyntax(msg string, offset int64) *SyntaxError {
113 return &SyntaxError{msg: msg, Offset: offset}
114 }
115
116 func ErrMarshaler(typ reflect.Type, err error, msg string) *MarshalerError {
117 return &MarshalerError{
118 Type: typ,
119 Err: err,
120 sourceFunc: msg,
121 }
122 }
123
124 func ErrExceededMaxDepth(c byte, cursor int64) *SyntaxError {
125 return &SyntaxError{
126 msg: fmt.Sprintf(`invalid character "%c" exceeded max depth`, c),
127 Offset: cursor,
128 }
129 }
130
131 func ErrNotAtBeginningOfValue(cursor int64) *SyntaxError {
132 return &SyntaxError{msg: "not at beginning of value", Offset: cursor}
133 }
134
135 func ErrUnexpectedEndOfJSON(msg string, cursor int64) *SyntaxError {
136 return &SyntaxError{
137 msg: fmt.Sprintf("json: %s unexpected end of JSON input", msg),
138 Offset: cursor,
139 }
140 }
141
142 func ErrExpected(msg string, cursor int64) *SyntaxError {
143 return &SyntaxError{msg: fmt.Sprintf("expected %s", msg), Offset: cursor}
144 }
145
146 func ErrInvalidCharacter(c byte, context string, cursor int64) *SyntaxError {
147 if c == 0 {
148 return &SyntaxError{
149 msg: fmt.Sprintf("json: invalid character as %s", context),
150 Offset: cursor,
151 }
152 }
153 return &SyntaxError{
154 msg: fmt.Sprintf("json: invalid character %c as %s", c, context),
155 Offset: cursor,
156 }
157 }
158
159 func ErrInvalidBeginningOfValue(c byte, cursor int64) *SyntaxError {
160 return &SyntaxError{
161 msg: fmt.Sprintf("invalid character '%c' looking for beginning of value", c),
162 Offset: cursor,
163 }
164 }
165
166 type PathError struct {
167 msg string
168 }
169
170 func (e *PathError) Error() string {
171 return fmt.Sprintf("json: invalid path format: %s", e.msg)
172 }
173
174 func ErrInvalidPath(msg string, args ...interface{}) *PathError {
175 if len(args) != 0 {
176 return &PathError{msg: fmt.Sprintf(msg, args...)}
177 }
178 return &PathError{msg: msg}
179 }
180
181 func ErrEmptyPath() *PathError {
182 return &PathError{msg: "path is empty"}
183 }
184
View as plain text