|
| 1 | +package errors |
| 2 | + |
| 3 | +import "fmt" |
| 4 | + |
| 5 | +// New returns an error that formats as the given text. |
| 6 | +func New(text string) error { |
| 7 | + return &errorString{text} |
| 8 | +} |
| 9 | + |
| 10 | +// errorString is a trivial implementation of error. |
| 11 | +type errorString struct { |
| 12 | + s string |
| 13 | +} |
| 14 | + |
| 15 | +func (e *errorString) Error() string { |
| 16 | + return e.s |
| 17 | +} |
| 18 | + |
| 19 | +type errBadRequest struct { |
| 20 | + message string |
| 21 | +} |
| 22 | + |
| 23 | +func (err *errBadRequest) Error() string { |
| 24 | + if err == nil { |
| 25 | + return "nil" |
| 26 | + } |
| 27 | + return err.message |
| 28 | +} |
| 29 | + |
| 30 | +// NewBadRequestError create a new not found error |
| 31 | +func NewBadRequestError(message string) error { |
| 32 | + return &errBadRequest{message} |
| 33 | +} |
| 34 | + |
| 35 | +// IsBadRequestError judges error is errBadRequest |
| 36 | +func IsBadRequestError(err error) bool { |
| 37 | + if _, ok := err.(*errBadRequest); ok { |
| 38 | + return true |
| 39 | + } |
| 40 | + return false |
| 41 | +} |
| 42 | + |
| 43 | +type errNotFound struct { |
| 44 | + resource string |
| 45 | +} |
| 46 | + |
| 47 | +func (err *errNotFound) Error() string { |
| 48 | + if err == nil { |
| 49 | + return "nil" |
| 50 | + } |
| 51 | + return fmt.Sprintf("resource '%s' is not found", err.resource) |
| 52 | +} |
| 53 | + |
| 54 | +// NewNotFoundError create a new not found error |
| 55 | +func NewNotFoundError(resource string) error { |
| 56 | + return &errNotFound{resource} |
| 57 | +} |
| 58 | + |
| 59 | +// IsNotFoundError judges error is errNotFound |
| 60 | +func IsNotFoundError(err error) bool { |
| 61 | + if _, ok := err.(*errNotFound); ok { |
| 62 | + return true |
| 63 | + } |
| 64 | + return false |
| 65 | +} |
| 66 | + |
| 67 | +type errConflict struct { |
| 68 | + resource string |
| 69 | +} |
| 70 | + |
| 71 | +func (err *errConflict) Error() string { |
| 72 | + if err == nil { |
| 73 | + return "nil" |
| 74 | + } |
| 75 | + return fmt.Sprintf("resource '%s' is conflict with the exists one", err.resource) |
| 76 | +} |
| 77 | + |
| 78 | +// NewConflictError create a new conflict error |
| 79 | +func NewConflictError(resource string) error { |
| 80 | + return &errConflict{resource} |
| 81 | +} |
| 82 | + |
| 83 | +// IsConflictError judges error is errConflict |
| 84 | +func IsConflictError(err error) bool { |
| 85 | + if _, ok := err.(*errConflict); ok { |
| 86 | + return true |
| 87 | + } |
| 88 | + return false |
| 89 | +} |
| 90 | + |
| 91 | +// not ready will returen StatusNotAcceptable http status if user post or put a request |
| 92 | +type errNotReady struct { |
| 93 | + resource string |
| 94 | +} |
| 95 | + |
| 96 | +func (err *errNotReady) Error() string { |
| 97 | + if err == nil { |
| 98 | + return "nil" |
| 99 | + } |
| 100 | + return fmt.Sprintf("resource '%s' is not ready for process", err.resource) |
| 101 | +} |
| 102 | + |
| 103 | +// NewNotReadyError create a new not ready error |
| 104 | +func NewNotReadyError(resource string) error { |
| 105 | + return &errNotReady{resource} |
| 106 | +} |
| 107 | + |
| 108 | +// IsNotReadyError judges error is errNotReady |
| 109 | +func IsNotReadyError(err error) bool { |
| 110 | + if _, ok := err.(*errNotReady); ok { |
| 111 | + return true |
| 112 | + } |
| 113 | + return false |
| 114 | +} |
| 115 | + |
| 116 | +type errTaskIsRunning struct { |
| 117 | + message string |
| 118 | +} |
| 119 | + |
| 120 | +func (err *errTaskIsRunning) Error() string { |
| 121 | + if err == nil { |
| 122 | + return "nil" |
| 123 | + } |
| 124 | + return err.message |
| 125 | +} |
| 126 | + |
| 127 | +// NewTaskIsRunningError create a new task is running error |
| 128 | +func NewTaskIsRunningError(message string) error { |
| 129 | + return &errTaskIsRunning{message} |
| 130 | +} |
| 131 | + |
| 132 | +// IsTaskIsRunningError judges error is errTaskIsRunning |
| 133 | +func IsTaskIsRunningError(err error) bool { |
| 134 | + if _, ok := err.(*errTaskIsRunning); ok { |
| 135 | + return true |
| 136 | + } |
| 137 | + return false |
| 138 | +} |
| 139 | + |
| 140 | +// NewClientError return a ClientError with the msg |
| 141 | +func NewClientError(msg string) *ClientError { |
| 142 | + return &ClientError{message: msg} |
| 143 | +} |
| 144 | + |
| 145 | +// ClientError is an error caused by request client |
| 146 | +type ClientError struct { |
| 147 | + message string |
| 148 | +} |
| 149 | + |
| 150 | +func (err *ClientError) Error() string { |
| 151 | + return err.message |
| 152 | +} |
| 153 | + |
| 154 | +// IsClientError judeges error is IsClientError |
| 155 | +func IsClientError(err error) bool { |
| 156 | + if _, ok := err.(*ClientError); ok { |
| 157 | + return true |
| 158 | + } |
| 159 | + return false |
| 160 | +} |
| 161 | + |
| 162 | +// NewServerError return a ServerError with the msg |
| 163 | +func NewServerError(msg string) *ServerError { |
| 164 | + return &ServerError{message: msg} |
| 165 | +} |
| 166 | + |
| 167 | +// ServerError is an error caused by request client |
| 168 | +type ServerError struct { |
| 169 | + message string |
| 170 | +} |
| 171 | + |
| 172 | +func (err *ServerError) Error() string { |
| 173 | + return err.message |
| 174 | +} |
| 175 | + |
| 176 | +// invalid region |
| 177 | +type errInvalidRegion struct { |
| 178 | + region string |
| 179 | +} |
| 180 | + |
| 181 | +func (err *errInvalidRegion) Error() string { |
| 182 | + if err == nil { |
| 183 | + return "nil" |
| 184 | + } |
| 185 | + return fmt.Sprintf("invalid region '%s'", err.region) |
| 186 | +} |
| 187 | + |
| 188 | +// NewInvalidRegionError create a new not found error |
| 189 | +func NewInvalidRegionError(region string) error { |
| 190 | + return &errInvalidRegion{region} |
| 191 | +} |
| 192 | + |
| 193 | +// IsInvalidRegionError judges error is errInvalidRegion |
| 194 | +func IsInvalidRegionError(err error) bool { |
| 195 | + if _, ok := err.(*errInvalidRegion); ok { |
| 196 | + return true |
| 197 | + } |
| 198 | + return false |
| 199 | +} |
| 200 | + |
| 201 | +type errForbidden struct { |
| 202 | + message string |
| 203 | +} |
| 204 | + |
| 205 | +func (err *errForbidden) Error() string { |
| 206 | + if err == nil { |
| 207 | + return "nil" |
| 208 | + } |
| 209 | + return err.message |
| 210 | +} |
| 211 | + |
| 212 | +// NewForbiddenError create a new not found error |
| 213 | +func NewForbiddenError(message string) error { |
| 214 | + return &errForbidden{message} |
| 215 | +} |
| 216 | + |
| 217 | +// IsForbiddenError judges error is errForbidden |
| 218 | +func IsForbiddenError(err error) bool { |
| 219 | + if _, ok := err.(*errForbidden); ok { |
| 220 | + return true |
| 221 | + } |
| 222 | + return false |
| 223 | +} |
0 commit comments