error.go 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. // Package awserr represents API error interface accessors for the SDK.
  2. package awserr
  3. // An Error wraps lower level errors with code, message and an original error.
  4. // The underlying concrete error type may also satisfy other interfaces which
  5. // can be to used to obtain more specific information about the error.
  6. //
  7. // Calling Error() or String() will always include the full information about
  8. // an error based on its underlying type.
  9. //
  10. // Example:
  11. //
  12. // output, err := s3manage.Upload(svc, input, opts)
  13. // if err != nil {
  14. // if awsErr, ok := err.(awserr.Error); ok {
  15. // // Get error details
  16. // log.Println("Error:", awsErr.Code(), awsErr.Message())
  17. //
  18. // // Prints out full error message, including original error if there was one.
  19. // log.Println("Error:", awsErr.Error())
  20. //
  21. // // Get original error
  22. // if origErr := awsErr.OrigErr(); origErr != nil {
  23. // // operate on original error.
  24. // }
  25. // } else {
  26. // fmt.Println(err.Error())
  27. // }
  28. // }
  29. //
  30. type Error interface {
  31. // Satisfy the generic error interface.
  32. error
  33. // Returns the short phrase depicting the classification of the error.
  34. Code() string
  35. // Returns the error details message.
  36. Message() string
  37. // Returns the original error if one was set. Nil is returned if not set.
  38. OrigErr() error
  39. }
  40. // BatchError is a batch of errors which also wraps lower level errors with
  41. // code, message, and original errors. Calling Error() will include all errors
  42. // that occurred in the batch.
  43. //
  44. // Deprecated: Replaced with BatchedErrors. Only defined for backwards
  45. // compatibility.
  46. type BatchError interface {
  47. // Satisfy the generic error interface.
  48. error
  49. // Returns the short phrase depicting the classification of the error.
  50. Code() string
  51. // Returns the error details message.
  52. Message() string
  53. // Returns the original error if one was set. Nil is returned if not set.
  54. OrigErrs() []error
  55. }
  56. // BatchedErrors is a batch of errors which also wraps lower level errors with
  57. // code, message, and original errors. Calling Error() will include all errors
  58. // that occurred in the batch.
  59. //
  60. // Replaces BatchError
  61. type BatchedErrors interface {
  62. // Satisfy the base Error interface.
  63. Error
  64. // Returns the original error if one was set. Nil is returned if not set.
  65. OrigErrs() []error
  66. }
  67. // New returns an Error object described by the code, message, and origErr.
  68. //
  69. // If origErr satisfies the Error interface it will not be wrapped within a new
  70. // Error object and will instead be returned.
  71. func New(code, message string, origErr error) Error {
  72. var errs []error
  73. if origErr != nil {
  74. errs = append(errs, origErr)
  75. }
  76. return newBaseError(code, message, errs)
  77. }
  78. // NewBatchError returns an BatchedErrors with a collection of errors as an
  79. // array of errors.
  80. func NewBatchError(code, message string, errs []error) BatchedErrors {
  81. return newBaseError(code, message, errs)
  82. }
  83. // A RequestFailure is an interface to extract request failure information from
  84. // an Error such as the request ID of the failed request returned by a service.
  85. // RequestFailures may not always have a requestID value if the request failed
  86. // prior to reaching the service such as a connection error.
  87. //
  88. // Example:
  89. //
  90. // output, err := s3manage.Upload(svc, input, opts)
  91. // if err != nil {
  92. // if reqerr, ok := err.(RequestFailure); ok {
  93. // log.Println("Request failed", reqerr.Code(), reqerr.Message(), reqerr.RequestID())
  94. // } else {
  95. // log.Println("Error:", err.Error())
  96. // }
  97. // }
  98. //
  99. // Combined with awserr.Error:
  100. //
  101. // output, err := s3manage.Upload(svc, input, opts)
  102. // if err != nil {
  103. // if awsErr, ok := err.(awserr.Error); ok {
  104. // // Generic AWS Error with Code, Message, and original error (if any)
  105. // fmt.Println(awsErr.Code(), awsErr.Message(), awsErr.OrigErr())
  106. //
  107. // if reqErr, ok := err.(awserr.RequestFailure); ok {
  108. // // A service error occurred
  109. // fmt.Println(reqErr.StatusCode(), reqErr.RequestID())
  110. // }
  111. // } else {
  112. // fmt.Println(err.Error())
  113. // }
  114. // }
  115. //
  116. type RequestFailure interface {
  117. Error
  118. // The status code of the HTTP response.
  119. StatusCode() int
  120. // The request ID returned by the service for a request failure. This will
  121. // be empty if no request ID is available such as the request failed due
  122. // to a connection error.
  123. RequestID() string
  124. }
  125. // NewRequestFailure returns a wrapped error with additional information for
  126. // request status code, and service requestID.
  127. //
  128. // Should be used to wrap all request which involve service requests. Even if
  129. // the request failed without a service response, but had an HTTP status code
  130. // that may be meaningful.
  131. func NewRequestFailure(err Error, statusCode int, reqID string) RequestFailure {
  132. return newRequestError(err, statusCode, reqID)
  133. }
  134. // UnmarshalError provides the interface for the SDK failing to unmarshal data.
  135. type UnmarshalError interface {
  136. awsError
  137. Bytes() []byte
  138. }
  139. // NewUnmarshalError returns an initialized UnmarshalError error wrapper adding
  140. // the bytes that fail to unmarshal to the error.
  141. func NewUnmarshalError(err error, msg string, bytes []byte) UnmarshalError {
  142. return &unmarshalError{
  143. awsError: New("UnmarshalError", msg, err),
  144. bytes: bytes,
  145. }
  146. }