~james-page/ubuntu/wily/juju-core/mir-fixes

« back to all changes in this revision

Viewing changes to src/github.com/joyent/gocommon/errors/errors.go

  • Committer: Package Import Robot
  • Author(s): James Page
  • Date: 2014-04-07 18:24:59 UTC
  • mfrom: (1.1.22)
  • Revision ID: package-import@ubuntu.com-20140407182459-1b6zvm5ygm4ki7yp
Tags: 1.18.0-0ubuntu1
* New upstream release (LP: #1287147), including fixes for:
  - maas/lxc: LXC permission denied issue (LP: #1299588).
  - core: mega-watcher for machines does not include container
    addresses (LP: #1301464).

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
//
 
2
// gocommon - Go library to interact with the JoyentCloud
 
3
// This package provides an Error implementation which knows about types of error, and which has support
 
4
// for error causes.
 
5
//
 
6
// Copyright (c) 2013 Joyent Inc.
 
7
//
 
8
// Written by Daniele Stroppa <daniele.stroppa@joyent.com>
 
9
//
 
10
 
 
11
package errors
 
12
 
 
13
import "fmt"
 
14
 
 
15
type Code string
 
16
 
 
17
const (
 
18
        // Public available error types.
 
19
        // These errors are provided because they are specifically required by business logic in the callers.
 
20
        BadRequestError         = Code("BadRequest")
 
21
        InternalErrorError      = Code("InternalError")
 
22
        InvalidArgumentError    = Code("InvalidArgument")
 
23
        InvalidCredentialsError = Code("InvalidCredentials")
 
24
        InvalidHeaderError      = Code("InvalidHeader")
 
25
        InvalidVersionError     = Code("InvalidVersion")
 
26
        MissingParameterError   = Code("MissinParameter")
 
27
        NotAuthorizedError      = Code("NotAuthorized")
 
28
        RequestThrottledError   = Code("RequestThrottled")
 
29
        RequestTooLargeError    = Code("RequestTooLarge")
 
30
        RequestMovedError       = Code("RequestMoved")
 
31
        ResourceNotFoundError   = Code("ResourceNotFound")
 
32
        UnknownErrorError       = Code("UnkownError")
 
33
)
 
34
 
 
35
// Error instances store an optional error cause.
 
36
type Error interface {
 
37
        error
 
38
        Cause() error
 
39
}
 
40
 
 
41
type gojoyentError struct {
 
42
        error
 
43
        errcode Code
 
44
        cause   error
 
45
}
 
46
 
 
47
// Type checks.
 
48
var _ Error = (*gojoyentError)(nil)
 
49
 
 
50
// Code returns the error code.
 
51
func (err *gojoyentError) code() Code {
 
52
        if err.errcode != UnknownErrorError {
 
53
                return err.errcode
 
54
        }
 
55
        if e, ok := err.cause.(*gojoyentError); ok {
 
56
                return e.code()
 
57
        }
 
58
        return UnknownErrorError
 
59
}
 
60
 
 
61
// Cause returns the error cause.
 
62
func (err *gojoyentError) Cause() error {
 
63
        return err.cause
 
64
}
 
65
 
 
66
// CausedBy returns true if this error or its cause are of the specified error code.
 
67
func (err *gojoyentError) causedBy(code Code) bool {
 
68
        if err.code() == code {
 
69
                return true
 
70
        }
 
71
        if cause, ok := err.cause.(*gojoyentError); ok {
 
72
                return cause.code() == code
 
73
        }
 
74
        return false
 
75
}
 
76
 
 
77
// Error fulfills the error interface, taking account of any caused by error.
 
78
func (err *gojoyentError) Error() string {
 
79
        if err.cause != nil {
 
80
                return fmt.Sprintf("%v\ncaused by: %v", err.error, err.cause)
 
81
        }
 
82
        return err.error.Error()
 
83
}
 
84
 
 
85
func IsBadRequest(err error) bool {
 
86
        if e, ok := err.(*gojoyentError); ok {
 
87
                return e.causedBy(BadRequestError)
 
88
        }
 
89
        return false
 
90
}
 
91
 
 
92
func IsInternalError(err error) bool {
 
93
        if e, ok := err.(*gojoyentError); ok {
 
94
                return e.causedBy(InternalErrorError)
 
95
        }
 
96
        return false
 
97
}
 
98
 
 
99
func IsInvalidArgument(err error) bool {
 
100
        if e, ok := err.(*gojoyentError); ok {
 
101
                return e.causedBy(InvalidArgumentError)
 
102
        }
 
103
        return false
 
104
}
 
105
 
 
106
func IsInvalidCredentials(err error) bool {
 
107
        if e, ok := err.(*gojoyentError); ok {
 
108
                return e.causedBy(InvalidCredentialsError)
 
109
        }
 
110
        return false
 
111
}
 
112
 
 
113
func IsInvalidHeader(err error) bool {
 
114
        if e, ok := err.(*gojoyentError); ok {
 
115
                return e.causedBy(InvalidHeaderError)
 
116
        }
 
117
        return false
 
118
}
 
119
 
 
120
func IsInvalidVersion(err error) bool {
 
121
        if e, ok := err.(*gojoyentError); ok {
 
122
                return e.causedBy(InvalidVersionError)
 
123
        }
 
124
        return false
 
125
}
 
126
 
 
127
func IsMissingParameter(err error) bool {
 
128
        if e, ok := err.(*gojoyentError); ok {
 
129
                return e.causedBy(MissingParameterError)
 
130
        }
 
131
        return false
 
132
}
 
133
 
 
134
func IsNotAuthorized(err error) bool {
 
135
        if e, ok := err.(*gojoyentError); ok {
 
136
                return e.causedBy(NotAuthorizedError)
 
137
        }
 
138
        return false
 
139
}
 
140
 
 
141
func IsRequestThrottled(err error) bool {
 
142
        if e, ok := err.(*gojoyentError); ok {
 
143
                return e.causedBy(RequestThrottledError)
 
144
        }
 
145
        return false
 
146
}
 
147
 
 
148
func IsRequestTooLarge(err error) bool {
 
149
        if e, ok := err.(*gojoyentError); ok {
 
150
                return e.causedBy(RequestTooLargeError)
 
151
        }
 
152
        return false
 
153
}
 
154
 
 
155
func IsRequestMoved(err error) bool {
 
156
        if e, ok := err.(*gojoyentError); ok {
 
157
                return e.causedBy(RequestMovedError)
 
158
        }
 
159
        return false
 
160
}
 
161
 
 
162
func IsResourceNotFound(err error) bool {
 
163
        if e, ok := err.(*gojoyentError); ok {
 
164
                return e.causedBy(ResourceNotFoundError)
 
165
        }
 
166
        return false
 
167
}
 
168
 
 
169
func IsUnknownError(err error) bool {
 
170
        if e, ok := err.(*gojoyentError); ok {
 
171
                return e.causedBy(UnknownErrorError)
 
172
        }
 
173
        return false
 
174
}
 
175
 
 
176
// New creates a new Error instance with the specified cause.
 
177
func makeErrorf(code Code, cause error, format string, args ...interface{}) Error {
 
178
        return &gojoyentError{
 
179
                errcode: code,
 
180
                error:   fmt.Errorf(format, args...),
 
181
                cause:   cause,
 
182
        }
 
183
}
 
184
 
 
185
// New creates a new UnknownError Error instance with the specified cause.
 
186
func Newf(cause error, format string, args ...interface{}) Error {
 
187
        return makeErrorf(UnknownErrorError, cause, format, args...)
 
188
}
 
189
 
 
190
// New creates a new BadRequest Error instance with the specified cause.
 
191
func NewBadRequestf(cause error, context interface{}, format string, args ...interface{}) Error {
 
192
        if format == "" {
 
193
                format = fmt.Sprintf("Bad Request: %s", context)
 
194
        }
 
195
        return makeErrorf(BadRequestError, cause, format, args...)
 
196
}
 
197
 
 
198
// New creates a new InternalError Error instance with the specified cause.
 
199
func NewInternalErrorf(cause error, context interface{}, format string, args ...interface{}) Error {
 
200
        if format == "" {
 
201
                format = fmt.Sprintf("Internal Error: %s", context)
 
202
        }
 
203
        return makeErrorf(InternalErrorError, cause, format, args...)
 
204
}
 
205
 
 
206
// New creates a new InvalidArgument Error instance with the specified cause.
 
207
func NewInvalidArgumentf(cause error, context interface{}, format string, args ...interface{}) Error {
 
208
        if format == "" {
 
209
                format = fmt.Sprintf("Invalid Argument: %s", context)
 
210
        }
 
211
        return makeErrorf(InvalidArgumentError, cause, format, args...)
 
212
}
 
213
 
 
214
// New creates a new InvalidCredentials Error instance with the specified cause.
 
215
func NewInvalidCredentialsf(cause error, context interface{}, format string, args ...interface{}) Error {
 
216
        if format == "" {
 
217
                format = fmt.Sprintf("Invalid Credentials: %s", context)
 
218
        }
 
219
        return makeErrorf(InvalidCredentialsError, cause, format, args...)
 
220
}
 
221
 
 
222
// New creates a new InvalidHeader Error instance with the specified cause.
 
223
func NewInvalidHeaderf(cause error, context interface{}, format string, args ...interface{}) Error {
 
224
        if format == "" {
 
225
                format = fmt.Sprintf("Invalid Header: %s", context)
 
226
        }
 
227
        return makeErrorf(InvalidHeaderError, cause, format, args...)
 
228
}
 
229
 
 
230
// New creates a new InvalidVersion Error instance with the specified cause.
 
231
func NewInvalidVersionf(cause error, context interface{}, format string, args ...interface{}) Error {
 
232
        if format == "" {
 
233
                format = fmt.Sprintf("Invalid Version: %s", context)
 
234
        }
 
235
        return makeErrorf(InvalidVersionError, cause, format, args...)
 
236
}
 
237
 
 
238
// New creates a new MissingParameter Error instance with the specified cause.
 
239
func NewMissingParameterf(cause error, context interface{}, format string, args ...interface{}) Error {
 
240
        if format == "" {
 
241
                format = fmt.Sprintf("Missing Parameter: %s", context)
 
242
        }
 
243
        return makeErrorf(MissingParameterError, cause, format, args...)
 
244
}
 
245
 
 
246
// New creates a new NotAuthorized Error instance with the specified cause.
 
247
func NewNotAuthorizedf(cause error, context interface{}, format string, args ...interface{}) Error {
 
248
        if format == "" {
 
249
                format = fmt.Sprintf("Not Authorized: %s", context)
 
250
        }
 
251
        return makeErrorf(NotAuthorizedError, cause, format, args...)
 
252
}
 
253
 
 
254
// New creates a new RequestThrottled Error instance with the specified cause.
 
255
func NewRequestThrottledf(cause error, context interface{}, format string, args ...interface{}) Error {
 
256
        if format == "" {
 
257
                format = fmt.Sprintf("Request Throttled: %s", context)
 
258
        }
 
259
        return makeErrorf(RequestThrottledError, cause, format, args...)
 
260
}
 
261
 
 
262
// New creates a new RequestTooLarge Error instance with the specified cause.
 
263
func NewRequestTooLargef(cause error, context interface{}, format string, args ...interface{}) Error {
 
264
        if format == "" {
 
265
                format = fmt.Sprintf("Request Too Large: %s", context)
 
266
        }
 
267
        return makeErrorf(RequestTooLargeError, cause, format, args...)
 
268
}
 
269
 
 
270
// New creates a new RequestMoved Error instance with the specified cause.
 
271
func NewRequestMovedf(cause error, context interface{}, format string, args ...interface{}) Error {
 
272
        if format == "" {
 
273
                format = fmt.Sprintf("Request Moved: %s", context)
 
274
        }
 
275
        return makeErrorf(RequestMovedError, cause, format, args...)
 
276
}
 
277
 
 
278
// New creates a new ResourceNotFound Error instance with the specified cause.
 
279
func NewResourceNotFoundf(cause error, context interface{}, format string, args ...interface{}) Error {
 
280
        if format == "" {
 
281
                format = fmt.Sprintf("Resource Not Found: %s", context)
 
282
        }
 
283
        return makeErrorf(ResourceNotFoundError, cause, format, args...)
 
284
}
 
285
 
 
286
// New creates a new UnknownError Error instance with the specified cause.
 
287
func NewUnknownErrorf(cause error, context interface{}, format string, args ...interface{}) Error {
 
288
        if format == "" {
 
289
                format = fmt.Sprintf("Unknown Error: %s", context)
 
290
        }
 
291
        return makeErrorf(UnknownErrorError, cause, format, args...)
 
292
}