github.com/zerosnake0/jzon@v0.0.9-0.20230801092939-1b135cb83f7f/errors_decoder.go (about) 1 package jzon 2 3 import ( 4 "errors" 5 "fmt" 6 ) 7 8 // DecodeError describes the encountered error and its location. 9 type DecodeError struct { 10 reason error 11 location string 12 } 13 14 func (e *DecodeError) Error() string { 15 return fmt.Sprintf("%s (near %s)", e.reason.Error(), e.location) 16 } 17 18 // ErrDataRemained there is still data remained in the buffer 19 // normally returned by Unmarshal methods 20 var ErrDataRemained = errors.New("expecting EOF, but there is still data") 21 22 // ErrPointerReceiver if the decode target is not a pointer 23 var ErrPointerReceiver = errors.New("the receiver is not a pointer") 24 25 // ErrNilPointerReceiver if the decode target is nil 26 var ErrNilPointerReceiver = errors.New("the receiver is nil") 27 28 // ErrEmptyIFace if the decode target is an empty interface (with method) 29 var ErrEmptyIFace = errors.New("cannot unmarshal on empty iface") 30 31 // ErrNilEmbeddedPointer if the decode target has an unexported nil field 32 var ErrNilEmbeddedPointer = errors.New("cannot unmarshal on nil pointer (unexported embedded)") 33 34 // ErrEfaceLooping if we encountered a loop 35 // for example: 36 // type iface interface{} 37 // var o1 iface 38 // o1 = &o1 39 // err := DefaultDecoderConfig.Unmarshal([]byte(`1`), o1) 40 var ErrEfaceLooping = errors.New("eface looping detected") 41 42 // InvalidStringCharError there is an invalid character when reading string 43 type InvalidStringCharError struct { 44 c byte 45 } 46 47 func (e InvalidStringCharError) Error() string { 48 return fmt.Sprintf("invalid character %x found", e.c) 49 } 50 51 // InvalidEscapeCharError there is an invalid escape character (when reading string) 52 type InvalidEscapeCharError struct { 53 c byte 54 } 55 56 func (e InvalidEscapeCharError) Error() string { 57 return fmt.Sprintf("invalid escape character \\%x found", e.c) 58 } 59 60 // InvalidUnicodeCharError there is an invalid unicode character (when reading string) 61 type InvalidUnicodeCharError struct { 62 c byte 63 } 64 65 func (e InvalidUnicodeCharError) Error() string { 66 return fmt.Sprintf("invalid unicode character %x found", e.c) 67 } 68 69 // UnexpectedByteError there is an unexpected character 70 type UnexpectedByteError struct { 71 got byte 72 exp byte 73 exp2 byte 74 } 75 76 func (e UnexpectedByteError) Error() string { 77 if e.exp == 0 { 78 return fmt.Sprintf("unexpected character %q", e.got) 79 } 80 if e.exp2 == 0 { 81 return fmt.Sprintf("expecting %q but got %q", e.exp, e.got) 82 } 83 return fmt.Sprintf("expecting %q or %q but got %q", e.exp, e.exp2, e.got) 84 } 85 86 // IntOverflowError the integer overflows. 87 type IntOverflowError struct { 88 typ string 89 value string 90 } 91 92 func (e IntOverflowError) Error() string { 93 return fmt.Sprintf("overflow %s: %s", e.typ, e.value) 94 } 95 96 // InvalidDigitError there is an invalid digit when reading number 97 type InvalidDigitError struct { 98 c byte 99 } 100 101 func (e InvalidDigitError) Error() string { 102 return fmt.Sprintf("invalid digit character: %q", e.c) 103 } 104 105 // InvalidFloatError there is an invalid digit when reading float 106 type InvalidFloatError struct { 107 c byte 108 } 109 110 func (e InvalidFloatError) Error() string { 111 return fmt.Sprintf("invalid float character: %q", e.c) 112 } 113 114 // TypeNotSupportedError the decode target is not supported 115 type TypeNotSupportedError string 116 117 func (e TypeNotSupportedError) Error() string { 118 return fmt.Sprintf("%q is not supported", string(e)) 119 } 120 121 // UnknownFieldError there is an unknown field when decoding 122 type UnknownFieldError string 123 124 func (e UnknownFieldError) Error() string { 125 return fmt.Sprintf("unknown field %q", string(e)) 126 } 127 128 // BadQuotedStringError the value of field is not correctly quoted 129 type BadQuotedStringError string 130 131 func (e BadQuotedStringError) Error() string { 132 return fmt.Sprintf("bad quoted string %q", string(e)) 133 }