error.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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:", err.Code(), err.Message())
  17. //
  18. // Prints out full error message, including original error if there was one.
  19. // log.Println("Error:", err.Error())
  20. //
  21. // // Get original error
  22. // if origErr := err.Err(); 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. // A RequestFailure is an interface to extract request failure information from
  41. // an Error such as the request ID of the failed request returned by a service.
  42. // RequestFailures may not always have a requestID value if the request failed
  43. // prior to reaching the service such as a connection error.
  44. //
  45. // Example:
  46. //
  47. // output, err := s3manage.Upload(svc, input, opts)
  48. // if err != nil {
  49. // if reqerr, ok := err.(RequestFailure); ok {
  50. // log.Printf("Request failed", reqerr.Code(), reqerr.Message(), reqerr.RequestID())
  51. // } else {
  52. // log.Printf("Error:", err.Error()
  53. // }
  54. // }
  55. //
  56. // Combined with awserr.Error:
  57. //
  58. // output, err := s3manage.Upload(svc, input, opts)
  59. // if err != nil {
  60. // if awsErr, ok := err.(awserr.Error); ok {
  61. // // Generic AWS Error with Code, Message, and original error (if any)
  62. // fmt.Println(awsErr.Code(), awsErr.Message(), awsErr.OrigErr())
  63. //
  64. // if reqErr, ok := err.(awserr.RequestFailure); ok {
  65. // // A service error occurred
  66. // fmt.Println(reqErr.StatusCode(), reqErr.RequestID())
  67. // }
  68. // } else {
  69. // fmt.Println(err.Error())
  70. // }
  71. // }
  72. //
  73. type RequestFailure interface {
  74. Error
  75. // The status code of the HTTP response.
  76. StatusCode() int
  77. // The request ID returned by the service for a request failure. This will
  78. // be empty if no request ID is available such as the request failed due
  79. // to a connection error.
  80. RequestID() string
  81. }
  82. // New returns an Error object described by the code, message, and origErr.
  83. //
  84. // If origErr satisfies the Error interface it will not be wrapped within a new
  85. // Error object and will instead be returned.
  86. func New(code, message string, origErr error) Error {
  87. if e, ok := origErr.(Error); ok && e != nil {
  88. return e
  89. }
  90. return newBaseError(code, message, origErr)
  91. }
  92. // NewRequestFailure returns a new request error wrapper for the given Error
  93. // provided.
  94. func NewRequestFailure(err Error, statusCode int, reqID string) RequestFailure {
  95. return newRequestError(err, statusCode, reqID)
  96. }