github.com/cloudbase/juju-core@v0.0.0-20140504232958-a7271ac7912f/errors/errors.go (about) 1 // Copyright 2013 Canonical Ltd. 2 // Licensed under the AGPLv3, see LICENCE file for details. 3 4 package errors 5 6 import ( 7 "fmt" 8 ) 9 10 // errorWrapper defines a way to encapsulate an error inside another error. 11 type errorWrapper struct { 12 // Err is the underlying error. 13 Err error 14 Msg string 15 } 16 17 // Error implements the error interface. 18 func (e *errorWrapper) Error() string { 19 if e.Msg != "" || e.Err == nil { 20 if e.Err != nil { 21 return fmt.Sprintf("%s: %v", e.Msg, e.Err.Error()) 22 } 23 return e.Msg 24 } 25 return e.Err.Error() 26 } 27 28 // notFoundError records an error when something has not been found. 29 type notFoundError struct { 30 *errorWrapper 31 } 32 33 // IsNotFoundError is satisfied by errors created by this package representing 34 // resources that can't be found. 35 func IsNotFoundError(err error) bool { 36 if _, ok := err.(notFoundError); ok { 37 return true 38 } 39 return false 40 } 41 42 // NotFoundf returns a error which satisfies IsNotFoundError(). 43 // The message for the error is made up from the given 44 // arguments formatted as with fmt.Sprintf, with the 45 // string " not found" appended. 46 func NotFoundf(format string, args ...interface{}) error { 47 return notFoundError{ 48 &errorWrapper{ 49 Msg: fmt.Sprintf(format+" not found", args...), 50 }, 51 } 52 } 53 54 // NewNotFoundError returns a new error wrapping err that satisfies 55 // IsNotFoundError(). 56 func NewNotFoundError(err error, msg string) error { 57 return notFoundError{&errorWrapper{Err: err, Msg: msg}} 58 } 59 60 // unauthorizedError represents the error that an operation is unauthorized. 61 // Use IsUnauthorized() to determine if the error was related to authorization 62 // failure. 63 type unauthorizedError struct { 64 *errorWrapper 65 } 66 67 // IsUnauthorizedError is satisfied by errors created by this package 68 // representing authorization failures. 69 func IsUnauthorizedError(err error) bool { 70 _, ok := err.(unauthorizedError) 71 return ok 72 } 73 74 // Unauthorizedf returns an error which satisfies IsUnauthorizedError(). 75 func Unauthorizedf(format string, args ...interface{}) error { 76 return unauthorizedError{ 77 &errorWrapper{ 78 Msg: fmt.Sprintf(format, args...), 79 }, 80 } 81 } 82 83 // NewUnauthorizedError returns an error which wraps err and satisfies 84 // IsUnauthorized(). 85 func NewUnauthorizedError(err error, msg string) error { 86 return unauthorizedError{&errorWrapper{Err: err, Msg: msg}} 87 } 88 89 type notImplementedError struct { 90 what string 91 } 92 93 // NewNotImplementedError returns an error signifying that 94 // something is not implemented. 95 func NewNotImplementedError(what string) error { 96 return ¬ImplementedError{what: what} 97 } 98 99 func (e *notImplementedError) Error() string { 100 return e.what + " not implemented" 101 } 102 103 // IsNotImplementedError reports whether the error 104 // was created with NewNotImplementedError. 105 func IsNotImplementedError(err error) bool { 106 _, ok := err.(*notImplementedError) 107 return ok 108 }