github.com/trim21/go-phpserialize@v0.0.22-0.20240301204449-2fca0319b3f0/internal/errors/error.go (about) 1 package errors 2 3 import ( 4 "fmt" 5 "reflect" 6 ) 7 8 type InvalidUnmarshalError struct { 9 Type reflect.Type 10 } 11 12 func (e *InvalidUnmarshalError) Error() string { 13 if e.Type == nil { 14 return "php: Unmarshal(nil)" 15 } 16 17 if e.Type.Kind() != reflect.Ptr { 18 return fmt.Sprintf("php: Unmarshal(non-pointer %s)", e.Type) 19 } 20 return fmt.Sprintf("php: Unmarshal(nil %s)", e.Type) 21 } 22 23 // A SyntaxError is a description of a JSON syntax error. 24 type SyntaxError struct { 25 msg string // description of error 26 Offset int64 // error occurred after reading Offset bytes 27 } 28 29 func (e *SyntaxError) Error() string { return e.msg } 30 31 // An UnmarshalTypeError describes a JSON value that was 32 // not appropriate for a value of a specific Go type. 33 type UnmarshalTypeError struct { 34 Value string // description of JSON value - "bool", "array", "number -5" 35 Type reflect.Type // type of Go value it could not be assigned to 36 Offset int64 // error occurred after reading Offset bytes 37 Struct string // name of the struct type containing the field 38 Field string // the full path from root node to the field 39 } 40 41 func (e *UnmarshalTypeError) Error() string { 42 if e.Struct != "" || e.Field != "" { 43 return fmt.Sprintf("php: cannot unmarshal %s into Go struct field %s.%s of type %s (offset %d)", 44 e.Value, e.Struct, e.Field, e.Type, e.Offset, 45 ) 46 } 47 return fmt.Sprintf("php: cannot unmarshal %s into Go value of type %s (offset: %d)", e.Value, e.Type, e.Offset) 48 } 49 50 // An UnsupportedTypeError is returned by Marshal when attempting 51 // to encode an unsupported value type. 52 type UnsupportedTypeError struct { 53 Type reflect.Type 54 } 55 56 func (e *UnsupportedTypeError) Error() string { 57 return fmt.Sprintf("php: unsupported type: %s", e.Type) 58 } 59 60 type UnsupportedValueError struct { 61 Value reflect.Value 62 Str string 63 } 64 65 func (e *UnsupportedValueError) Error() string { 66 return fmt.Sprintf("php: unsupported value: %s", e.Str) 67 } 68 69 func ErrSyntax(msg string, offset int64) *SyntaxError { 70 return &SyntaxError{msg: msg, Offset: offset} 71 } 72 73 func ErrExceededMaxDepth(c byte, cursor int64) *SyntaxError { 74 return &SyntaxError{ 75 msg: fmt.Sprintf(`invalid character "%c" exceeded max depth`, c), 76 Offset: cursor, 77 } 78 } 79 80 func ErrUnexpectedStart(typ string, buf []byte, cursor int64) *SyntaxError { 81 return &SyntaxError{ 82 msg: fmt.Sprintf("php: unexpected %c at beginneng of %s", buf[cursor], typ), 83 Offset: cursor, 84 } 85 } 86 87 func ErrUnexpectedEnd(msg string, cursor int64) *SyntaxError { 88 return &SyntaxError{ 89 msg: fmt.Sprintf("php: %s unexpected end of input", msg), 90 Offset: cursor, 91 } 92 } 93 94 func ErrUnexpectedLength(buf []byte, cursor int64) *SyntaxError { 95 return &SyntaxError{ 96 msg: fmt.Sprintf("php: unexpected char %c in length", buf[cursor]), 97 Offset: cursor, 98 } 99 } 100 101 func ErrExpected(msg string, cursor int64) *SyntaxError { 102 return &SyntaxError{msg: fmt.Sprintf("expected %s", msg), Offset: cursor} 103 } 104 105 func ErrInvalidCharacter(c byte, context string, cursor int64) *SyntaxError { 106 if c == 0 { 107 return &SyntaxError{ 108 msg: fmt.Sprintf("php: invalid character as %s", context), 109 Offset: cursor, 110 } 111 } 112 return &SyntaxError{ 113 msg: fmt.Sprintf("php: invalid character %c as %s", c, context), 114 Offset: cursor, 115 } 116 } 117 118 func ErrInvalidBeginningOfValue(c byte, cursor int64) *SyntaxError { 119 return &SyntaxError{ 120 msg: fmt.Sprintf("invalid character '%c' looking for beginning of value", c), 121 Offset: cursor, 122 } 123 } 124 125 func ErrInvalidBeginningOfArray(c byte, cursor int64) *SyntaxError { 126 return &SyntaxError{ 127 msg: fmt.Sprintf("invalid character '%c' looking for beginning of array", c), 128 Offset: cursor, 129 } 130 }