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
6
// Copyright (c) 2013 Joyent Inc.
8
// Written by Daniele Stroppa <daniele.stroppa@joyent.com>
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")
35
// Error instances store an optional error cause.
36
type Error interface {
41
type gojoyentError struct {
48
var _ Error = (*gojoyentError)(nil)
50
// Code returns the error code.
51
func (err *gojoyentError) code() Code {
52
if err.errcode != UnknownErrorError {
55
if e, ok := err.cause.(*gojoyentError); ok {
58
return UnknownErrorError
61
// Cause returns the error cause.
62
func (err *gojoyentError) Cause() error {
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 {
71
if cause, ok := err.cause.(*gojoyentError); ok {
72
return cause.code() == code
77
// Error fulfills the error interface, taking account of any caused by error.
78
func (err *gojoyentError) Error() string {
80
return fmt.Sprintf("%v\ncaused by: %v", err.error, err.cause)
82
return err.error.Error()
85
func IsBadRequest(err error) bool {
86
if e, ok := err.(*gojoyentError); ok {
87
return e.causedBy(BadRequestError)
92
func IsInternalError(err error) bool {
93
if e, ok := err.(*gojoyentError); ok {
94
return e.causedBy(InternalErrorError)
99
func IsInvalidArgument(err error) bool {
100
if e, ok := err.(*gojoyentError); ok {
101
return e.causedBy(InvalidArgumentError)
106
func IsInvalidCredentials(err error) bool {
107
if e, ok := err.(*gojoyentError); ok {
108
return e.causedBy(InvalidCredentialsError)
113
func IsInvalidHeader(err error) bool {
114
if e, ok := err.(*gojoyentError); ok {
115
return e.causedBy(InvalidHeaderError)
120
func IsInvalidVersion(err error) bool {
121
if e, ok := err.(*gojoyentError); ok {
122
return e.causedBy(InvalidVersionError)
127
func IsMissingParameter(err error) bool {
128
if e, ok := err.(*gojoyentError); ok {
129
return e.causedBy(MissingParameterError)
134
func IsNotAuthorized(err error) bool {
135
if e, ok := err.(*gojoyentError); ok {
136
return e.causedBy(NotAuthorizedError)
141
func IsRequestThrottled(err error) bool {
142
if e, ok := err.(*gojoyentError); ok {
143
return e.causedBy(RequestThrottledError)
148
func IsRequestTooLarge(err error) bool {
149
if e, ok := err.(*gojoyentError); ok {
150
return e.causedBy(RequestTooLargeError)
155
func IsRequestMoved(err error) bool {
156
if e, ok := err.(*gojoyentError); ok {
157
return e.causedBy(RequestMovedError)
162
func IsResourceNotFound(err error) bool {
163
if e, ok := err.(*gojoyentError); ok {
164
return e.causedBy(ResourceNotFoundError)
169
func IsUnknownError(err error) bool {
170
if e, ok := err.(*gojoyentError); ok {
171
return e.causedBy(UnknownErrorError)
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{
180
error: fmt.Errorf(format, args...),
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...)
190
// New creates a new BadRequest Error instance with the specified cause.
191
func NewBadRequestf(cause error, context interface{}, format string, args ...interface{}) Error {
193
format = fmt.Sprintf("Bad Request: %s", context)
195
return makeErrorf(BadRequestError, cause, format, args...)
198
// New creates a new InternalError Error instance with the specified cause.
199
func NewInternalErrorf(cause error, context interface{}, format string, args ...interface{}) Error {
201
format = fmt.Sprintf("Internal Error: %s", context)
203
return makeErrorf(InternalErrorError, cause, format, args...)
206
// New creates a new InvalidArgument Error instance with the specified cause.
207
func NewInvalidArgumentf(cause error, context interface{}, format string, args ...interface{}) Error {
209
format = fmt.Sprintf("Invalid Argument: %s", context)
211
return makeErrorf(InvalidArgumentError, cause, format, args...)
214
// New creates a new InvalidCredentials Error instance with the specified cause.
215
func NewInvalidCredentialsf(cause error, context interface{}, format string, args ...interface{}) Error {
217
format = fmt.Sprintf("Invalid Credentials: %s", context)
219
return makeErrorf(InvalidCredentialsError, cause, format, args...)
222
// New creates a new InvalidHeader Error instance with the specified cause.
223
func NewInvalidHeaderf(cause error, context interface{}, format string, args ...interface{}) Error {
225
format = fmt.Sprintf("Invalid Header: %s", context)
227
return makeErrorf(InvalidHeaderError, cause, format, args...)
230
// New creates a new InvalidVersion Error instance with the specified cause.
231
func NewInvalidVersionf(cause error, context interface{}, format string, args ...interface{}) Error {
233
format = fmt.Sprintf("Invalid Version: %s", context)
235
return makeErrorf(InvalidVersionError, cause, format, args...)
238
// New creates a new MissingParameter Error instance with the specified cause.
239
func NewMissingParameterf(cause error, context interface{}, format string, args ...interface{}) Error {
241
format = fmt.Sprintf("Missing Parameter: %s", context)
243
return makeErrorf(MissingParameterError, cause, format, args...)
246
// New creates a new NotAuthorized Error instance with the specified cause.
247
func NewNotAuthorizedf(cause error, context interface{}, format string, args ...interface{}) Error {
249
format = fmt.Sprintf("Not Authorized: %s", context)
251
return makeErrorf(NotAuthorizedError, cause, format, args...)
254
// New creates a new RequestThrottled Error instance with the specified cause.
255
func NewRequestThrottledf(cause error, context interface{}, format string, args ...interface{}) Error {
257
format = fmt.Sprintf("Request Throttled: %s", context)
259
return makeErrorf(RequestThrottledError, cause, format, args...)
262
// New creates a new RequestTooLarge Error instance with the specified cause.
263
func NewRequestTooLargef(cause error, context interface{}, format string, args ...interface{}) Error {
265
format = fmt.Sprintf("Request Too Large: %s", context)
267
return makeErrorf(RequestTooLargeError, cause, format, args...)
270
// New creates a new RequestMoved Error instance with the specified cause.
271
func NewRequestMovedf(cause error, context interface{}, format string, args ...interface{}) Error {
273
format = fmt.Sprintf("Request Moved: %s", context)
275
return makeErrorf(RequestMovedError, cause, format, args...)
278
// New creates a new ResourceNotFound Error instance with the specified cause.
279
func NewResourceNotFoundf(cause error, context interface{}, format string, args ...interface{}) Error {
281
format = fmt.Sprintf("Resource Not Found: %s", context)
283
return makeErrorf(ResourceNotFoundError, cause, format, args...)
286
// New creates a new UnknownError Error instance with the specified cause.
287
func NewUnknownErrorf(cause error, context interface{}, format string, args ...interface{}) Error {
289
format = fmt.Sprintf("Unknown Error: %s", context)
291
return makeErrorf(UnknownErrorError, cause, format, args...)