David K. Bainbridge | 528b318 | 2017-01-23 08:51:59 -0800 | [diff] [blame^] | 1 | // Copyright 2016 Canonical Ltd. |
| 2 | // Licensed under the LGPLv3, see LICENCE file for details. |
| 3 | |
| 4 | package gomaasapi |
| 5 | |
| 6 | import ( |
| 7 | "fmt" |
| 8 | |
| 9 | "github.com/juju/errors" |
| 10 | ) |
| 11 | |
| 12 | // NoMatchError is returned when the requested action cannot be performed |
| 13 | // due to being unable to service due to no entities available that match the |
| 14 | // request. |
| 15 | type NoMatchError struct { |
| 16 | errors.Err |
| 17 | } |
| 18 | |
| 19 | // NewNoMatchError constructs a new NoMatchError and sets the location. |
| 20 | func NewNoMatchError(message string) error { |
| 21 | err := &NoMatchError{Err: errors.NewErr(message)} |
| 22 | err.SetLocation(1) |
| 23 | return err |
| 24 | } |
| 25 | |
| 26 | // IsNoMatchError returns true if err is a NoMatchError. |
| 27 | func IsNoMatchError(err error) bool { |
| 28 | _, ok := errors.Cause(err).(*NoMatchError) |
| 29 | return ok |
| 30 | } |
| 31 | |
| 32 | // UnexpectedError is an error for a condition that hasn't been determined. |
| 33 | type UnexpectedError struct { |
| 34 | errors.Err |
| 35 | } |
| 36 | |
| 37 | // NewUnexpectedError constructs a new UnexpectedError and sets the location. |
| 38 | func NewUnexpectedError(err error) error { |
| 39 | uerr := &UnexpectedError{Err: errors.NewErr("unexpected: %v", err)} |
| 40 | uerr.SetLocation(1) |
| 41 | return errors.Wrap(err, uerr) |
| 42 | } |
| 43 | |
| 44 | // IsUnexpectedError returns true if err is an UnexpectedError. |
| 45 | func IsUnexpectedError(err error) bool { |
| 46 | _, ok := errors.Cause(err).(*UnexpectedError) |
| 47 | return ok |
| 48 | } |
| 49 | |
| 50 | // UnsupportedVersionError refers to calls made to an unsupported api version. |
| 51 | type UnsupportedVersionError struct { |
| 52 | errors.Err |
| 53 | } |
| 54 | |
| 55 | // NewUnsupportedVersionError constructs a new UnsupportedVersionError and sets the location. |
| 56 | func NewUnsupportedVersionError(format string, args ...interface{}) error { |
| 57 | err := &UnsupportedVersionError{Err: errors.NewErr(format, args...)} |
| 58 | err.SetLocation(1) |
| 59 | return err |
| 60 | } |
| 61 | |
| 62 | // IsUnsupportedVersionError returns true if err is an UnsupportedVersionError. |
| 63 | func IsUnsupportedVersionError(err error) bool { |
| 64 | _, ok := errors.Cause(err).(*UnsupportedVersionError) |
| 65 | return ok |
| 66 | } |
| 67 | |
| 68 | // DeserializationError types are returned when the returned JSON data from |
| 69 | // the controller doesn't match the code's expectations. |
| 70 | type DeserializationError struct { |
| 71 | errors.Err |
| 72 | } |
| 73 | |
| 74 | // NewDeserializationError constructs a new DeserializationError and sets the location. |
| 75 | func NewDeserializationError(format string, args ...interface{}) error { |
| 76 | err := &DeserializationError{Err: errors.NewErr(format, args...)} |
| 77 | err.SetLocation(1) |
| 78 | return err |
| 79 | } |
| 80 | |
| 81 | // WrapWithDeserializationError constructs a new DeserializationError with the |
| 82 | // specified message, and sets the location and returns a new error with the |
| 83 | // full error stack set including the error passed in. |
| 84 | func WrapWithDeserializationError(err error, format string, args ...interface{}) error { |
| 85 | message := fmt.Sprintf(format, args...) |
| 86 | // We want the deserialization error message to include the error text of the |
| 87 | // previous error, but wrap it in the new type. |
| 88 | derr := &DeserializationError{Err: errors.NewErr(message + ": " + err.Error())} |
| 89 | derr.SetLocation(1) |
| 90 | wrapped := errors.Wrap(err, derr) |
| 91 | // We want the location of the wrapped error to be the caller of this function, |
| 92 | // not the line above. |
| 93 | if errType, ok := wrapped.(*errors.Err); ok { |
| 94 | // We know it is because that is what Wrap returns. |
| 95 | errType.SetLocation(1) |
| 96 | } |
| 97 | return wrapped |
| 98 | } |
| 99 | |
| 100 | // IsDeserializationError returns true if err is a DeserializationError. |
| 101 | func IsDeserializationError(err error) bool { |
| 102 | _, ok := errors.Cause(err).(*DeserializationError) |
| 103 | return ok |
| 104 | } |
| 105 | |
| 106 | // BadRequestError is returned when the requested action cannot be performed |
| 107 | // due to bad or incorrect parameters passed to the server. |
| 108 | type BadRequestError struct { |
| 109 | errors.Err |
| 110 | } |
| 111 | |
| 112 | // NewBadRequestError constructs a new BadRequestError and sets the location. |
| 113 | func NewBadRequestError(message string) error { |
| 114 | err := &BadRequestError{Err: errors.NewErr(message)} |
| 115 | err.SetLocation(1) |
| 116 | return err |
| 117 | } |
| 118 | |
| 119 | // IsBadRequestError returns true if err is a NoMatchError. |
| 120 | func IsBadRequestError(err error) bool { |
| 121 | _, ok := errors.Cause(err).(*BadRequestError) |
| 122 | return ok |
| 123 | } |
| 124 | |
| 125 | // PermissionError is returned when the user does not have permission to do the |
| 126 | // requested action. |
| 127 | type PermissionError struct { |
| 128 | errors.Err |
| 129 | } |
| 130 | |
| 131 | // NewPermissionError constructs a new PermissionError and sets the location. |
| 132 | func NewPermissionError(message string) error { |
| 133 | err := &PermissionError{Err: errors.NewErr(message)} |
| 134 | err.SetLocation(1) |
| 135 | return err |
| 136 | } |
| 137 | |
| 138 | // IsPermissionError returns true if err is a NoMatchError. |
| 139 | func IsPermissionError(err error) bool { |
| 140 | _, ok := errors.Cause(err).(*PermissionError) |
| 141 | return ok |
| 142 | } |
| 143 | |
| 144 | // CannotCompleteError is returned when the requested action is unable to |
| 145 | // complete for some server side reason. |
| 146 | type CannotCompleteError struct { |
| 147 | errors.Err |
| 148 | } |
| 149 | |
| 150 | // NewCannotCompleteError constructs a new CannotCompleteError and sets the location. |
| 151 | func NewCannotCompleteError(message string) error { |
| 152 | err := &CannotCompleteError{Err: errors.NewErr(message)} |
| 153 | err.SetLocation(1) |
| 154 | return err |
| 155 | } |
| 156 | |
| 157 | // IsCannotCompleteError returns true if err is a NoMatchError. |
| 158 | func IsCannotCompleteError(err error) bool { |
| 159 | _, ok := errors.Cause(err).(*CannotCompleteError) |
| 160 | return ok |
| 161 | } |