errors.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. package smithy
  2. import "fmt"
  3. // APIError provides the generic API and protocol agnostic error type all SDK
  4. // generated exception types will implement.
  5. type APIError interface {
  6. error
  7. // ErrorCode returns the error code for the API exception.
  8. ErrorCode() string
  9. // ErrorMessage returns the error message for the API exception.
  10. ErrorMessage() string
  11. // ErrorFault returns the fault for the API exception.
  12. ErrorFault() ErrorFault
  13. }
  14. // GenericAPIError provides a generic concrete API error type that SDKs can use
  15. // to deserialize error responses into. Should be used for unmodeled or untyped
  16. // errors.
  17. type GenericAPIError struct {
  18. Code string
  19. Message string
  20. Fault ErrorFault
  21. }
  22. // ErrorCode returns the error code for the API exception.
  23. func (e *GenericAPIError) ErrorCode() string { return e.Code }
  24. // ErrorMessage returns the error message for the API exception.
  25. func (e *GenericAPIError) ErrorMessage() string { return e.Message }
  26. // ErrorFault returns the fault for the API exception.
  27. func (e *GenericAPIError) ErrorFault() ErrorFault { return e.Fault }
  28. func (e *GenericAPIError) Error() string {
  29. return fmt.Sprintf("api error %s: %s", e.Code, e.Message)
  30. }
  31. var _ APIError = (*GenericAPIError)(nil)
  32. // OperationError decorates an underlying error which occurred while invoking
  33. // an operation with names of the operation and API.
  34. type OperationError struct {
  35. ServiceID string
  36. OperationName string
  37. Err error
  38. }
  39. // Service returns the name of the API service the error occurred with.
  40. func (e *OperationError) Service() string { return e.ServiceID }
  41. // Operation returns the name of the API operation the error occurred with.
  42. func (e *OperationError) Operation() string { return e.OperationName }
  43. // Unwrap returns the nested error if any, or nil.
  44. func (e *OperationError) Unwrap() error { return e.Err }
  45. func (e *OperationError) Error() string {
  46. return fmt.Sprintf("operation error %s: %s, %v", e.ServiceID, e.OperationName, e.Err)
  47. }
  48. // DeserializationError provides a wrapper for an error that occurs during
  49. // deserialization.
  50. type DeserializationError struct {
  51. Err error // original error
  52. Snapshot []byte
  53. }
  54. // Error returns a formatted error for DeserializationError
  55. func (e *DeserializationError) Error() string {
  56. const msg = "deserialization failed"
  57. if e.Err == nil {
  58. return msg
  59. }
  60. return fmt.Sprintf("%s, %v", msg, e.Err)
  61. }
  62. // Unwrap returns the underlying Error in DeserializationError
  63. func (e *DeserializationError) Unwrap() error { return e.Err }
  64. // ErrorFault provides the type for a Smithy API error fault.
  65. type ErrorFault int
  66. // ErrorFault enumeration values
  67. const (
  68. FaultUnknown ErrorFault = iota
  69. FaultServer
  70. FaultClient
  71. )
  72. func (f ErrorFault) String() string {
  73. switch f {
  74. case FaultServer:
  75. return "server"
  76. case FaultClient:
  77. return "client"
  78. default:
  79. return "unknown"
  80. }
  81. }
  82. // SerializationError represents an error that occurred while attempting to serialize a request
  83. type SerializationError struct {
  84. Err error // original error
  85. }
  86. // Error returns a formatted error for SerializationError
  87. func (e *SerializationError) Error() string {
  88. const msg = "serialization failed"
  89. if e.Err == nil {
  90. return msg
  91. }
  92. return fmt.Sprintf("%s: %v", msg, e.Err)
  93. }
  94. // Unwrap returns the underlying Error in SerializationError
  95. func (e *SerializationError) Unwrap() error { return e.Err }
  96. // CanceledError is the error that will be returned by an API request that was
  97. // canceled. API operations given a Context may return this error when
  98. // canceled.
  99. type CanceledError struct {
  100. Err error
  101. }
  102. // CanceledError returns true to satisfy interfaces checking for canceled errors.
  103. func (*CanceledError) CanceledError() bool { return true }
  104. // Unwrap returns the underlying error, if there was one.
  105. func (e *CanceledError) Unwrap() error {
  106. return e.Err
  107. }
  108. func (e *CanceledError) Error() string {
  109. return fmt.Sprintf("canceled, %v", e.Err)
  110. }