~ubuntu-branches/debian/stretch/golang-github-aws-aws-sdk-go/stretch

« back to all changes in this revision

Viewing changes to aws/awserr/types.go

  • Committer: Package Import Robot
  • Author(s): Dmitry Smirnov
  • Date: 2015-09-29 12:34:07 UTC
  • Revision ID: package-import@ubuntu.com-20150929123407-7xmll3gdhvb9zh2l
Tags: upstream-0.9.9+dfsg
ImportĀ upstreamĀ versionĀ 0.9.9+dfsg

Show diffs side-by-side

added added

removed removed

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