| 1 |
package errors |
| 2 |
|
| 3 |
import ( |
| 4 |
"fmt" |
| 5 |
"reflect" |
| 6 |
"strconv" |
| 7 |
) |
| 8 |
|
| 9 |
type InvalidUTF8Error struct { |
| 10 |
S string // the whole string value that caused the error |
| 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 |
// A MarshalerError represents an error from calling a MarshalJSON or MarshalText method. |
| 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 |
// Unwrap returns the underlying error. |
| 48 |
func (e *MarshalerError) Unwrap() error { return e.Err } |
| 49 |
|
| 50 |
// A SyntaxError is a description of a JSON syntax error. |
| 51 |
type SyntaxError struct { |
| 52 |
msg string // description of error |
| 53 |
Offset int64 // error occurred after reading Offset bytes |
| 54 |
// atEnd is whether the error is the end of the input, where more of it would have been read: a stream |
| 55 |
// reports it as io.ErrUnexpectedEOF. |
| 56 |
atEnd bool |
| 57 |
// valueAt is the position of the byte of the error, which is not the start of a value where one is waited |
| 58 |
// for, plus one, or 0 for any other error ( see ValueStartAt ). |
| 59 |
valueAt int64 |
| 60 |
} |
| 61 |
|
| 62 |
func (e *SyntaxError) Error() string { return e.msg } |
| 63 |
|
| 64 |
// An UnmarshalFieldError describes a JSON object key that |
| 65 |
// led to an unexported (and therefore unwritable) struct field. |
| 66 |
// |
| 67 |
// Deprecated: No longer used; kept for compatibility. |
| 68 |
type UnmarshalFieldError struct { |
| 69 |
Key string |
| 70 |
Type reflect.Type |
| 71 |
Field reflect.StructField |
| 72 |
} |
| 73 |
|
| 74 |
func (e *UnmarshalFieldError) Error() string { |
| 75 |
return fmt.Sprintf("json: cannot unmarshal object key %s into unexported field %s of type %s", |
| 76 |
strconv.Quote(e.Key), e.Field.Name, e.Type.String(), |
| 77 |
) |
| 78 |
} |
| 79 |
|
| 80 |
// An UnmarshalTypeError describes a JSON value that was |
| 81 |
// not appropriate for a value of a specific Go type. |
| 82 |
type UnmarshalTypeError struct { |
| 83 |
Value string // description of JSON value - "bool", "array", "number -5" |
| 84 |
Type reflect.Type // type of Go value it could not be assigned to |
| 85 |
Offset int64 // error occurred after reading Offset bytes |
| 86 |
Struct string // name of the struct type containing the field, or of the root type ( see Error ) |
| 87 |
Field string // the full path from root node to the field |
| 88 |
Err error // the cause of the error, which encoding/json of Go 1.27 reports, or nil |
| 89 |
} |
| 90 |
|
| 91 |
// Error returns the message of encoding/json of the Go version ( see typeErrorMessage ). |
| 92 |
func (e *UnmarshalTypeError) Error() string { |
| 93 |
return typeErrorMessage(e) |
| 94 |
} |
| 95 |
|
| 96 |
func (e *UnmarshalTypeError) Unwrap() error { |
| 97 |
return e.Err |
| 98 |
} |
| 99 |
|
| 100 |
// An UnsupportedTypeError is returned by Marshal when attempting |
| 101 |
// to encode an unsupported value type. |
| 102 |
type UnsupportedTypeError struct { |
| 103 |
Type reflect.Type |
| 104 |
} |
| 105 |
|
| 106 |
func (e *UnsupportedTypeError) Error() string { |
| 107 |
return fmt.Sprintf("json: unsupported type: %s", e.Type) |
| 108 |
} |
| 109 |
|
| 110 |
type UnsupportedValueError struct { |
| 111 |
Value reflect.Value |
| 112 |
Str string |
| 113 |
} |
| 114 |
|
| 115 |
func (e *UnsupportedValueError) Error() string { |
| 116 |
return fmt.Sprintf("json: unsupported value: %s", e.Str) |
| 117 |
} |
| 118 |
|
| 119 |
func ErrSyntax(msg string, offset int64) *SyntaxError { |
| 120 |
return &SyntaxError{msg: msg, Offset: offset} |
| 121 |
} |
| 122 |
|
| 123 |
// ErrSyntaxAtEnd is ErrSyntax for the end of the input, where more of it would have been read. |
| 124 |
func ErrSyntaxAtEnd(msg string, offset int64) *SyntaxError { |
| 125 |
return &SyntaxError{msg: msg, Offset: offset, atEnd: true} |
| 126 |
} |
| 127 |
|
| 128 |
// WithValueStartAt marks e as the error of the byte at cursor, which is not the start of a value where one is |
| 129 |
// waited for, and returns it. |
| 130 |
func WithValueStartAt(e *SyntaxError, cursor int64) *SyntaxError { |
| 131 |
e.valueAt = cursor + 1 |
| 132 |
return e |
| 133 |
} |
| 134 |
|
| 135 |
// ValueStartAt returns the position of the byte of err, if it is the syntax error of a byte which is not the start |
| 136 |
// of a value where one is waited for ( see WithValueStartAt ). |
| 137 |
func ValueStartAt(err error) (int64, bool) { |
| 138 |
e, ok := err.(*SyntaxError) |
| 139 |
if !ok || e.valueAt == 0 { |
| 140 |
return 0, false |
| 141 |
} |
| 142 |
return e.valueAt - 1, true |
| 143 |
} |
| 144 |
|
| 145 |
// IsAtEnd reports whether err is a syntax error of the end of the input ( see ErrSyntaxAtEnd ). |
| 146 |
func IsAtEnd(err error) bool { |
| 147 |
e, ok := err.(*SyntaxError) |
| 148 |
return ok && e.atEnd |
| 149 |
} |
| 150 |
|
| 151 |
func ErrMarshaler(typ reflect.Type, err error, msg string) *MarshalerError { |
| 152 |
return &MarshalerError{ |
| 153 |
Type: typ, |
| 154 |
Err: err, |
| 155 |
sourceFunc: msg, |
| 156 |
} |
| 157 |
} |
| 158 |
|
| 159 |
func ErrExceededMaxDepth(c byte, cursor int64) *SyntaxError { |
| 160 |
return &SyntaxError{ |
| 161 |
msg: fmt.Sprintf(`invalid character "%c" exceeded max depth`, c), |
| 162 |
Offset: cursor, |
| 163 |
} |
| 164 |
} |
| 165 |
|
| 166 |
func ErrNotAtBeginningOfValue(cursor int64) *SyntaxError { |
| 167 |
return &SyntaxError{msg: "not at beginning of value", Offset: cursor} |
| 168 |
} |
| 169 |
|
| 170 |
func ErrUnexpectedEndOfJSON(msg string, cursor int64) *SyntaxError { |
| 171 |
return &SyntaxError{ |
| 172 |
msg: fmt.Sprintf("json: %s unexpected end of JSON input", msg), |
| 173 |
Offset: cursor, |
| 174 |
} |
| 175 |
} |
| 176 |
|
| 177 |
func ErrExpected(msg string, cursor int64) *SyntaxError { |
| 178 |
return &SyntaxError{msg: fmt.Sprintf("expected %s", msg), Offset: cursor} |
| 179 |
} |
| 180 |
|
| 181 |
func ErrInvalidCharacter(c byte, context string, cursor int64) *SyntaxError { |
| 182 |
if c == 0 { |
| 183 |
return &SyntaxError{ |
| 184 |
msg: fmt.Sprintf("json: invalid character as %s", context), |
| 185 |
Offset: cursor, |
| 186 |
} |
| 187 |
} |
| 188 |
return &SyntaxError{ |
| 189 |
msg: fmt.Sprintf("json: invalid character %c as %s", c, context), |
| 190 |
Offset: cursor, |
| 191 |
} |
| 192 |
} |
| 193 |
|
| 194 |
func ErrInvalidBeginningOfValue(c byte, cursor int64) *SyntaxError { |
| 195 |
return &SyntaxError{ |
| 196 |
msg: fmt.Sprintf("invalid character '%c' looking for beginning of value", c), |
| 197 |
Offset: cursor, |
| 198 |
} |
| 199 |
} |
| 200 |
|
| 201 |
type PathError struct { |
| 202 |
msg string |
| 203 |
} |
| 204 |
|
| 205 |
func (e *PathError) Error() string { |
| 206 |
return fmt.Sprintf("json: invalid path format: %s", e.msg) |
| 207 |
} |
| 208 |
|
| 209 |
func ErrInvalidPath(msg string, args ...any) *PathError { |
| 210 |
if len(args) != 0 { |
| 211 |
return &PathError{msg: fmt.Sprintf(msg, args...)} |
| 212 |
} |
| 213 |
return &PathError{msg: msg} |
| 214 |
} |
| 215 |
|
| 216 |
func ErrEmptyPath() *PathError { |
| 217 |
return &PathError{msg: "path is empty"} |
| 218 |
} |