error.go 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. // Package apierr represents API error types.
  2. package apierr
  3. import "fmt"
  4. // A BaseError wraps the code and message which defines an error. It also
  5. // can be used to wrap an original error object.
  6. //
  7. // Should be used as the root for errors satisfying the awserr.Error. Also
  8. // for any error which does not fit into a specific error wrapper type.
  9. type BaseError struct {
  10. // Classification of error
  11. code string
  12. // Detailed information about error
  13. message string
  14. // Optional original error this error is based off of. Allows building
  15. // chained errors.
  16. origErr error
  17. }
  18. // New returns an error object for the code, message, and err.
  19. //
  20. // code is a short no whitespace phrase depicting the classification of
  21. // the error that is being created.
  22. //
  23. // message is the free flow string containing detailed information about the error.
  24. //
  25. // origErr is the error object which will be nested under the new error to be returned.
  26. func New(code, message string, origErr error) *BaseError {
  27. return &BaseError{
  28. code: code,
  29. message: message,
  30. origErr: origErr,
  31. }
  32. }
  33. // Error returns the string representation of the error.
  34. //
  35. // See ErrorWithExtra for formatting.
  36. //
  37. // Satisfies the error interface.
  38. func (b *BaseError) Error() string {
  39. return b.ErrorWithExtra("")
  40. }
  41. // String returns the string representation of the error.
  42. // Alias for Error to satisfy the stringer interface.
  43. func (b *BaseError) String() string {
  44. return b.Error()
  45. }
  46. // Code returns the short phrase depicting the classification of the error.
  47. func (b *BaseError) Code() string {
  48. return b.code
  49. }
  50. // Message returns the error details message.
  51. func (b *BaseError) Message() string {
  52. return b.message
  53. }
  54. // OrigErr returns the original error if one was set. Nil is returned if no error
  55. // was set.
  56. func (b *BaseError) OrigErr() error {
  57. return b.origErr
  58. }
  59. // ErrorWithExtra is a helper method to add an extra string to the stratified
  60. // error message. The extra message will be added on the next line below the
  61. // error message like the following:
  62. //
  63. // <error code>: <error message>
  64. // <extra message>
  65. //
  66. // If there is a original error the error will be included on a new line.
  67. //
  68. // <error code>: <error message>
  69. // <extra message>
  70. // caused by: <original error>
  71. func (b *BaseError) ErrorWithExtra(extra string) string {
  72. msg := fmt.Sprintf("%s: %s", b.code, b.message)
  73. if extra != "" {
  74. msg = fmt.Sprintf("%s\n\t%s", msg, extra)
  75. }
  76. if b.origErr != nil {
  77. msg = fmt.Sprintf("%s\ncaused by: %s", msg, b.origErr.Error())
  78. }
  79. return msg
  80. }
  81. // A RequestError wraps a request or service error.
  82. //
  83. // Composed of BaseError for code, message, and original error.
  84. type RequestError struct {
  85. *BaseError
  86. statusCode int
  87. requestID string
  88. }
  89. // NewRequestError returns a wrapped error with additional information for request
  90. // status code, and service requestID.
  91. //
  92. // Should be used to wrap all request which involve service requests. Even if
  93. // the request failed without a service response, but had an HTTP status code
  94. // that may be meaningful.
  95. //
  96. // Also wraps original errors via the BaseError.
  97. func NewRequestError(base *BaseError, statusCode int, requestID string) *RequestError {
  98. return &RequestError{
  99. BaseError: base,
  100. statusCode: statusCode,
  101. requestID: requestID,
  102. }
  103. }
  104. // Error returns the string representation of the error.
  105. // Satisfies the error interface.
  106. func (r *RequestError) Error() string {
  107. return r.ErrorWithExtra(fmt.Sprintf("status code: %d, request id: [%s]",
  108. r.statusCode, r.requestID))
  109. }
  110. // String returns the string representation of the error.
  111. // Alias for Error to satisfy the stringer interface.
  112. func (r *RequestError) String() string {
  113. return r.Error()
  114. }
  115. // StatusCode returns the wrapped status code for the error
  116. func (r *RequestError) StatusCode() int {
  117. return r.statusCode
  118. }
  119. // RequestID returns the wrapped requestID
  120. func (r *RequestError) RequestID() string {
  121. return r.requestID
  122. }