github.com/3JoB/go-json@v0.10.4/internal/errors/error.go (about) 1 package errors 2 3 import ( 4 "fmt" 5 "strconv" 6 7 "github.com/3JoB/go-reflect" 8 ) 9 10 // New returns an error that formats as the given text. 11 // Each call to New returns a distinct error value even if the text is identical. 12 func New(text string) error { 13 return &errorString{s: text} 14 } 15 16 // errorString is a trivial implementation of error. 17 type errorString struct { 18 s string 19 } 20 21 func (e *errorString) Error() string { 22 return e.s 23 } 24 25 type InvalidUTF8Error struct { 26 S string // the whole string value that caused the error 27 } 28 29 func (e *InvalidUTF8Error) Error() string { 30 return fmt.Sprintf("json: invalid UTF-8 in string: %s", strconv.Quote(e.S)) 31 } 32 33 type InvalidUnmarshalError struct { 34 Type reflect.Type 35 } 36 37 func (e *InvalidUnmarshalError) Error() string { 38 if e.Type == nil { 39 return "json: Unmarshal(nil)" 40 } 41 42 if e.Type.Kind() != reflect.Ptr { 43 return fmt.Sprintf("json: Unmarshal(non-pointer %s)", e.Type) 44 } 45 return fmt.Sprintf("json: Unmarshal(nil %s)", e.Type) 46 } 47 48 // A MarshalerError represents an error from calling a MarshalJSON or MarshalText method. 49 type MarshalerError struct { 50 Type reflect.Type 51 Err error 52 sourceFunc string 53 } 54 55 func (e *MarshalerError) Error() string { 56 srcFunc := e.sourceFunc 57 if srcFunc == "" { 58 srcFunc = "MarshalJSON" 59 } 60 return fmt.Sprintf("json: error calling %s for type %s: %s", srcFunc, e.Type, e.Err.Error()) 61 } 62 63 // Unwrap returns the underlying error. 64 func (e *MarshalerError) Unwrap() error { return e.Err } 65 66 // A SyntaxError is a description of a JSON syntax error. 67 type SyntaxError struct { 68 msg string // description of error 69 Offset int64 // error occurred after reading Offset bytes 70 } 71 72 func (e *SyntaxError) Error() string { return e.msg } 73 74 // An UnmarshalFieldError describes a JSON object key that 75 // led to an unexported (and therefore unwritable) struct field. 76 // 77 // Deprecated: No longer used; kept for compatibility. 78 type UnmarshalFieldError struct { 79 Key string 80 Type reflect.Type 81 Field reflect.StructField 82 } 83 84 func (e *UnmarshalFieldError) Error() string { 85 return fmt.Sprintf("json: cannot unmarshal object key %s into unexported field %s of type %s", 86 strconv.Quote(e.Key), e.Field.Name, e.Type.String(), 87 ) 88 } 89 90 // An UnmarshalTypeError describes a JSON value that was 91 // not appropriate for a value of a specific Go type. 92 type UnmarshalTypeError struct { 93 Value string // description of JSON value - "bool", "array", "number -5" 94 Type reflect.Type // type of Go value it could not be assigned to 95 Offset int64 // error occurred after reading Offset bytes 96 Struct string // name of the struct type containing the field 97 Field string // the full path from root node to the field 98 } 99 100 func (e *UnmarshalTypeError) Error() string { 101 if e.Struct != "" || e.Field != "" { 102 return fmt.Sprintf("json: cannot unmarshal %s into Go struct field %s.%s of type %s", 103 e.Value, e.Struct, e.Field, e.Type, 104 ) 105 } 106 return fmt.Sprintf("json: cannot unmarshal %s into Go value of type %s", e.Value, e.Type) 107 } 108 109 // An UnsupportedTypeError is returned by Marshal when attempting 110 // to encode an unsupported value type. 111 type UnsupportedTypeError struct { 112 Type reflect.Type 113 } 114 115 func (e *UnsupportedTypeError) Error() string { 116 return fmt.Sprintf("json: unsupported type: %s", e.Type) 117 } 118 119 type UnsupportedValueError struct { 120 Value reflect.Value 121 Str string 122 } 123 124 func (e *UnsupportedValueError) Error() string { 125 return fmt.Sprintf("json: unsupported value: %s", e.Str) 126 } 127 128 func ErrSyntax(msg string, offset int64) *SyntaxError { 129 return &SyntaxError{msg: msg, Offset: offset} 130 } 131 132 func ErrMarshaler(typ reflect.Type, err error, msg string) *MarshalerError { 133 return &MarshalerError{ 134 Type: typ, 135 Err: err, 136 sourceFunc: msg, 137 } 138 } 139 140 func ErrExceededMaxDepth(c byte, cursor int64) *SyntaxError { 141 return &SyntaxError{ 142 msg: fmt.Sprintf(`invalid character "%c" exceeded max depth`, c), 143 Offset: cursor, 144 } 145 } 146 147 func ErrNotAtBeginningOfValue(cursor int64) *SyntaxError { 148 return &SyntaxError{msg: "not at beginning of value", Offset: cursor} 149 } 150 151 func ErrUnexpectedEndOfJSON(msg string, cursor int64) *SyntaxError { 152 return &SyntaxError{ 153 msg: fmt.Sprintf("json: %s unexpected end of JSON input", msg), 154 Offset: cursor, 155 } 156 } 157 158 func ErrExpected(msg string, cursor int64) *SyntaxError { 159 return &SyntaxError{msg: fmt.Sprintf("expected %s", msg), Offset: cursor} 160 } 161 162 func ErrInvalidCharacter(c byte, context string, cursor int64) *SyntaxError { 163 if c == 0 { 164 return &SyntaxError{ 165 msg: fmt.Sprintf("json: invalid character as %s", context), 166 Offset: cursor, 167 } 168 } 169 return &SyntaxError{ 170 msg: fmt.Sprintf("json: invalid character %c as %s", c, context), 171 Offset: cursor, 172 } 173 } 174 175 func ErrInvalidBeginningOfValue(c byte, cursor int64) *SyntaxError { 176 return &SyntaxError{ 177 msg: fmt.Sprintf("invalid character '%c' looking for beginning of value", c), 178 Offset: cursor, 179 } 180 } 181 182 type PathError struct { 183 msg string 184 } 185 186 func (e *PathError) Error() string { 187 return fmt.Sprintf("json: invalid path format: %s", e.msg) 188 } 189 190 func ErrInvalidPath(msg string, args ...any) *PathError { 191 if len(args) != 0 { 192 return &PathError{msg: fmt.Sprintf(msg, args...)} 193 } 194 return &PathError{msg: msg} 195 } 196 197 func ErrEmptyPath() *PathError { 198 return &PathError{msg: "path is empty"} 199 }