errors.go 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. package document
  2. import (
  3. "fmt"
  4. "reflect"
  5. )
  6. // UnmarshalTypeError is an error type representing an error
  7. // unmarshaling a Smithy document to a Go value type. This is different
  8. // from UnmarshalError in that it does not wrap an underlying error type.
  9. type UnmarshalTypeError struct {
  10. Value string
  11. Type reflect.Type
  12. }
  13. // Error returns the string representation of the error.
  14. // Satisfying the error interface.
  15. func (e *UnmarshalTypeError) Error() string {
  16. return fmt.Sprintf("unmarshal failed, cannot unmarshal %s into Go value type %s",
  17. e.Value, e.Type.String())
  18. }
  19. // An InvalidUnmarshalError is an error type representing an invalid type
  20. // encountered while unmarshaling a Smithy document to a Go value type.
  21. type InvalidUnmarshalError struct {
  22. Type reflect.Type
  23. }
  24. // Error returns the string representation of the error.
  25. // Satisfying the error interface.
  26. func (e *InvalidUnmarshalError) Error() string {
  27. var msg string
  28. if e.Type == nil {
  29. msg = "cannot unmarshal to nil value"
  30. } else if e.Type.Kind() != reflect.Ptr {
  31. msg = fmt.Sprintf("cannot unmarshal to non-pointer value, got %s", e.Type.String())
  32. } else {
  33. msg = fmt.Sprintf("cannot unmarshal to nil value, %s", e.Type.String())
  34. }
  35. return fmt.Sprintf("unmarshal failed, %s", msg)
  36. }
  37. // An UnmarshalError wraps an error that occurred while unmarshaling a
  38. // Smithy document into a Go type. This is different from
  39. // UnmarshalTypeError in that it wraps the underlying error that occurred.
  40. type UnmarshalError struct {
  41. Err error
  42. Value string
  43. Type reflect.Type
  44. }
  45. // Unwrap returns the underlying unmarshaling error
  46. func (e *UnmarshalError) Unwrap() error {
  47. return e.Err
  48. }
  49. // Error returns the string representation of the error.
  50. // Satisfying the error interface.
  51. func (e *UnmarshalError) Error() string {
  52. return fmt.Sprintf("unmarshal failed, cannot unmarshal %q into %s, %v",
  53. e.Value, e.Type.String(), e.Err)
  54. }
  55. // An InvalidMarshalError is an error type representing an error
  56. // occurring when marshaling a Go value type.
  57. type InvalidMarshalError struct {
  58. Message string
  59. }
  60. // Error returns the string representation of the error.
  61. // Satisfying the error interface.
  62. func (e *InvalidMarshalError) Error() string {
  63. return fmt.Sprintf("marshal failed, %s", e.Message)
  64. }