error.go 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. package errors
  2. import (
  3. "fmt"
  4. "reflect"
  5. "strconv"
  6. )
  7. type InvalidUTF8Error struct {
  8. S string // the whole string value that caused the error
  9. }
  10. func (e *InvalidUTF8Error) Error() string {
  11. return fmt.Sprintf("json: invalid UTF-8 in string: %s", strconv.Quote(e.S))
  12. }
  13. type InvalidUnmarshalError struct {
  14. Type reflect.Type
  15. }
  16. func (e *InvalidUnmarshalError) Error() string {
  17. if e.Type == nil {
  18. return "json: Unmarshal(nil)"
  19. }
  20. if e.Type.Kind() != reflect.Ptr {
  21. return fmt.Sprintf("json: Unmarshal(non-pointer %s)", e.Type)
  22. }
  23. return fmt.Sprintf("json: Unmarshal(nil %s)", e.Type)
  24. }
  25. // A MarshalerError represents an error from calling a MarshalJSON or MarshalText method.
  26. type MarshalerError struct {
  27. Type reflect.Type
  28. Err error
  29. sourceFunc string
  30. }
  31. func (e *MarshalerError) Error() string {
  32. srcFunc := e.sourceFunc
  33. if srcFunc == "" {
  34. srcFunc = "MarshalJSON"
  35. }
  36. return fmt.Sprintf("json: error calling %s for type %s: %s", srcFunc, e.Type, e.Err.Error())
  37. }
  38. // Unwrap returns the underlying error.
  39. func (e *MarshalerError) Unwrap() error { return e.Err }
  40. // A SyntaxError is a description of a JSON syntax error.
  41. type SyntaxError struct {
  42. msg string // description of error
  43. Offset int64 // error occurred after reading Offset bytes
  44. }
  45. func (e *SyntaxError) Error() string { return e.msg }
  46. // An UnmarshalFieldError describes a JSON object key that
  47. // led to an unexported (and therefore unwritable) struct field.
  48. //
  49. // Deprecated: No longer used; kept for compatibility.
  50. type UnmarshalFieldError struct {
  51. Key string
  52. Type reflect.Type
  53. Field reflect.StructField
  54. }
  55. func (e *UnmarshalFieldError) Error() string {
  56. return fmt.Sprintf("json: cannot unmarshal object key %s into unexported field %s of type %s",
  57. strconv.Quote(e.Key), e.Field.Name, e.Type.String(),
  58. )
  59. }
  60. // An UnmarshalTypeError describes a JSON value that was
  61. // not appropriate for a value of a specific Go type.
  62. type UnmarshalTypeError struct {
  63. Value string // description of JSON value - "bool", "array", "number -5"
  64. Type reflect.Type // type of Go value it could not be assigned to
  65. Offset int64 // error occurred after reading Offset bytes
  66. Struct string // name of the struct type containing the field
  67. Field string // the full path from root node to the field
  68. }
  69. func (e *UnmarshalTypeError) Error() string {
  70. if e.Struct != "" || e.Field != "" {
  71. return fmt.Sprintf("json: cannot unmarshal %s into Go struct field %s.%s of type %s",
  72. e.Value, e.Struct, e.Field, e.Type,
  73. )
  74. }
  75. return fmt.Sprintf("json: cannot unmarshal %s into Go value of type %s", e.Value, e.Type)
  76. }
  77. // An UnsupportedTypeError is returned by Marshal when attempting
  78. // to encode an unsupported value type.
  79. type UnsupportedTypeError struct {
  80. Type reflect.Type
  81. }
  82. func (e *UnsupportedTypeError) Error() string {
  83. return fmt.Sprintf("json: unsupported type: %s", e.Type)
  84. }
  85. type UnsupportedValueError struct {
  86. Value reflect.Value
  87. Str string
  88. }
  89. func (e *UnsupportedValueError) Error() string {
  90. return fmt.Sprintf("json: unsupported value: %s", e.Str)
  91. }
  92. func ErrSyntax(msg string, offset int64) *SyntaxError {
  93. return &SyntaxError{msg: msg, Offset: offset}
  94. }
  95. func ErrMarshaler(typ reflect.Type, err error, msg string) *MarshalerError {
  96. return &MarshalerError{
  97. Type: typ,
  98. Err: err,
  99. sourceFunc: msg,
  100. }
  101. }
  102. func ErrExceededMaxDepth(c byte, cursor int64) *SyntaxError {
  103. return &SyntaxError{
  104. msg: fmt.Sprintf(`invalid character "%c" exceeded max depth`, c),
  105. Offset: cursor,
  106. }
  107. }
  108. func ErrNotAtBeginningOfValue(cursor int64) *SyntaxError {
  109. return &SyntaxError{msg: "not at beginning of value", Offset: cursor}
  110. }
  111. func ErrUnexpectedEndOfJSON(msg string, cursor int64) *SyntaxError {
  112. return &SyntaxError{
  113. msg: fmt.Sprintf("json: %s unexpected end of JSON input", msg),
  114. Offset: cursor,
  115. }
  116. }
  117. func ErrExpected(msg string, cursor int64) *SyntaxError {
  118. return &SyntaxError{msg: fmt.Sprintf("expected %s", msg), Offset: cursor}
  119. }
  120. func ErrInvalidCharacter(c byte, context string, cursor int64) *SyntaxError {
  121. if c == 0 {
  122. return &SyntaxError{
  123. msg: fmt.Sprintf("json: invalid character as %s", context),
  124. Offset: cursor,
  125. }
  126. }
  127. return &SyntaxError{
  128. msg: fmt.Sprintf("json: invalid character %c as %s", c, context),
  129. Offset: cursor,
  130. }
  131. }
  132. func ErrInvalidBeginningOfValue(c byte, cursor int64) *SyntaxError {
  133. return &SyntaxError{
  134. msg: fmt.Sprintf("invalid character '%c' looking for beginning of value", c),
  135. Offset: cursor,
  136. }
  137. }