| 1 |
// Copyright 2022-2026 The NATS Authors |
| 2 |
// Licensed under the Apache License, Version 2.0 (the "License"); |
| 3 |
// you may not use this file except in compliance with the License. |
| 4 |
// You may obtain a copy of the License at |
| 5 |
// |
| 6 |
// http://www.apache.org/licenses/LICENSE-2.0 |
| 7 |
// |
| 8 |
// Unless required by applicable law or agreed to in writing, software |
| 9 |
// distributed under the License is distributed on an "AS IS" BASIS, |
| 10 |
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 11 |
// See the License for the specific language governing permissions and |
| 12 |
// limitations under the License. |
| 13 |
|
| 14 |
package jetstream |
| 15 |
|
| 16 |
import ( |
| 17 |
"errors" |
| 18 |
"fmt" |
| 19 |
) |
| 20 |
|
| 21 |
type ( |
| 22 |
// JetStreamError is an error result that happens when using JetStream. |
| 23 |
// In case of client-side error, [APIError] returns nil. |
| 24 |
JetStreamError interface { |
| 25 |
APIError() *APIError |
| 26 |
error |
| 27 |
} |
| 28 |
|
| 29 |
jsError struct { |
| 30 |
apiErr *APIError |
| 31 |
message string |
| 32 |
} |
| 33 |
|
| 34 |
// APIError is included in all API responses if there was an error. |
| 35 |
APIError struct { |
| 36 |
Code int `json:"code"` |
| 37 |
ErrorCode ErrorCode `json:"err_code"` |
| 38 |
Description string `json:"description,omitempty"` |
| 39 |
} |
| 40 |
|
| 41 |
// ErrorCode represents error_code returned in response from JetStream API. |
| 42 |
ErrorCode uint16 |
| 43 |
) |
| 44 |
|
| 45 |
const ( |
| 46 |
JSErrCodeBadRequest ErrorCode = 10003 |
| 47 |
JSErrCodeConsumerCreate ErrorCode = 10012 |
| 48 |
JSErrCodeConsumerNameExists ErrorCode = 10013 |
| 49 |
JSErrCodeConsumerNotFound ErrorCode = 10014 |
| 50 |
JSErrCodeMaximumConsumersLimit ErrorCode = 10026 |
| 51 |
|
| 52 |
JSErrCodeMessageNotFound ErrorCode = 10037 |
| 53 |
JSErrCodeJetStreamNotEnabledForAccount ErrorCode = 10039 |
| 54 |
|
| 55 |
JSErrCodeStreamNameInUse ErrorCode = 10058 |
| 56 |
JSErrCodeStreamNotFound ErrorCode = 10059 |
| 57 |
|
| 58 |
JSErrCodeStreamWrongLastSequence ErrorCode = 10071 |
| 59 |
JSErrCodeJetStreamNotEnabled ErrorCode = 10076 |
| 60 |
|
| 61 |
// JSErrCodeStreamWrongLastSequenceConstant is returned instead of |
| 62 |
// JSErrCodeStreamWrongLastSequence for CAS conflicts on replicated (R>1) |
| 63 |
// streams. The two are equivalent "wrong last sequence" responses. |
| 64 |
JSErrCodeStreamWrongLastSequenceConstant ErrorCode = 10164 |
| 65 |
|
| 66 |
JSErrCodeConsumerAlreadyExists ErrorCode = 10105 |
| 67 |
|
| 68 |
JSErrCodeDuplicateFilterSubjects ErrorCode = 10136 |
| 69 |
JSErrCodeOverlappingFilterSubjects ErrorCode = 10138 |
| 70 |
JSErrCodeConsumerEmptyFilter ErrorCode = 10139 |
| 71 |
JSErrCodeConsumerExists ErrorCode = 10148 |
| 72 |
JSErrCodeConsumerDoesNotExist ErrorCode = 10149 |
| 73 |
|
| 74 |
JSErrCodeMirrorWithMsgSchedules ErrorCode = 10186 |
| 75 |
JSErrCodeSourceWithMsgSchedules ErrorCode = 10187 |
| 76 |
JSErrCodeMessageSchedulesDisabled ErrorCode = 10188 |
| 77 |
JSErrCodeSchedulePatternInvalid ErrorCode = 10189 |
| 78 |
JSErrCodeScheduleTargetInvalid ErrorCode = 10190 |
| 79 |
JSErrCodeScheduleTTLInvalid ErrorCode = 10191 |
| 80 |
JSErrCodeScheduleRollupInvalid ErrorCode = 10192 |
| 81 |
JSErrCodeScheduleSourceInvalid ErrorCode = 10203 |
| 82 |
|
| 83 |
JSErrCodeConsumerInvalidReset ErrorCode = 10204 |
| 84 |
) |
| 85 |
|
| 86 |
var ( |
| 87 |
// JetStream API errors |
| 88 |
|
| 89 |
// ErrJetStreamNotEnabled is an error returned when JetStream is not |
| 90 |
// enabled. |
| 91 |
// |
| 92 |
// Note: This error will not be returned in clustered mode, even if each |
| 93 |
// server in the cluster does not have JetStream enabled. In clustered mode, |
| 94 |
// requests will time out instead. |
| 95 |
ErrJetStreamNotEnabled JetStreamError = &jsError{apiErr: &APIError{ErrorCode: JSErrCodeJetStreamNotEnabled, Description: "jetstream not enabled", Code: 503}} |
| 96 |
|
| 97 |
// ErrJetStreamNotEnabledForAccount is an error returned when JetStream is |
| 98 |
// not enabled for an account. |
| 99 |
ErrJetStreamNotEnabledForAccount JetStreamError = &jsError{apiErr: &APIError{ErrorCode: JSErrCodeJetStreamNotEnabledForAccount, Description: "jetstream not enabled for account", Code: 503}} |
| 100 |
|
| 101 |
// ErrStreamNotFound is an error returned when stream with given name does |
| 102 |
// not exist. |
| 103 |
ErrStreamNotFound JetStreamError = &jsError{apiErr: &APIError{ErrorCode: JSErrCodeStreamNotFound, Description: "stream not found", Code: 404}} |
| 104 |
|
| 105 |
// ErrStreamNameAlreadyInUse is returned when a stream with given name |
| 106 |
// already exists and has a different configuration. |
| 107 |
ErrStreamNameAlreadyInUse JetStreamError = &jsError{apiErr: &APIError{ErrorCode: JSErrCodeStreamNameInUse, Description: "stream name already in use", Code: 400}} |
| 108 |
|
| 109 |
// ErrStreamSubjectTransformNotSupported is returned when the connected |
| 110 |
// nats-server version does not support setting the stream subject |
| 111 |
// transform. If this error is returned when executing CreateStream(), the |
| 112 |
// stream with invalid configuration was already created in the server. |
| 113 |
ErrStreamSubjectTransformNotSupported JetStreamError = &jsError{message: "stream subject transformation not supported by nats-server"} |
| 114 |
|
| 115 |
// ErrStreamSourceSubjectTransformNotSupported is returned when the |
| 116 |
// connected nats-server version does not support setting the stream source |
| 117 |
// subject transform. If this error is returned when executing |
| 118 |
// CreateStream(), the stream with invalid configuration was already created |
| 119 |
// in the server. |
| 120 |
ErrStreamSourceSubjectTransformNotSupported JetStreamError = &jsError{message: "stream subject transformation not supported by nats-server"} |
| 121 |
|
| 122 |
// ErrStreamSourceNotSupported is returned when the connected nats-server |
| 123 |
// version does not support setting the stream sources. If this error is |
| 124 |
// returned when executing CreateStream(), the stream with invalid |
| 125 |
// configuration was already created in the server. |
| 126 |
ErrStreamSourceNotSupported JetStreamError = &jsError{message: "stream sourcing is not supported by nats-server"} |
| 127 |
|
| 128 |
// ErrStreamSourceMultipleFilterSubjectsNotSupported is returned when the |
| 129 |
// connected nats-server version does not support setting the stream |
| 130 |
// sources. If this error is returned when executing CreateStream(), the |
| 131 |
// stream with invalid configuration was already created in the server. |
| 132 |
ErrStreamSourceMultipleFilterSubjectsNotSupported JetStreamError = &jsError{message: "stream sourcing with multiple subject filters not supported by nats-server"} |
| 133 |
|
| 134 |
// ErrConsumerNotFound is an error returned when consumer with given name |
| 135 |
// does not exist. |
| 136 |
ErrConsumerNotFound JetStreamError = &jsError{apiErr: &APIError{ErrorCode: JSErrCodeConsumerNotFound, Description: "consumer not found", Code: 404}} |
| 137 |
|
| 138 |
// ErrConsumerCreationResponseEmpty is an error returned when the response from the server |
| 139 |
// when creating a consumer is empty. This means that the state of the consumer is unknown and |
| 140 |
// the consumer may not have been created successfully. |
| 141 |
ErrConsumerCreationResponseEmpty JetStreamError = &jsError{message: "consumer creation response is empty"} |
| 142 |
|
| 143 |
// ErrInvalidJetStreamResponse is returned when the response from the server |
| 144 |
// to a JetStream API call (stream CRUD, message get, etc.) does not contain |
| 145 |
// the expected data payload. The operation may or may not have succeeded on |
| 146 |
// the server side. |
| 147 |
ErrInvalidJetStreamResponse JetStreamError = &jsError{message: "invalid jetstream api response"} |
| 148 |
|
| 149 |
// ErrConsumerExists is returned when attempting to create a consumer with |
| 150 |
// CreateConsumer but a consumer with given name already exists. |
| 151 |
ErrConsumerExists JetStreamError = &jsError{apiErr: &APIError{ErrorCode: JSErrCodeConsumerExists, Description: "consumer already exists", Code: 400}} |
| 152 |
|
| 153 |
// ErrConsumerNameExists is returned when attempting to update a consumer |
| 154 |
// with UpdateConsumer but a consumer with given name does not exist. |
| 155 |
ErrConsumerDoesNotExist JetStreamError = &jsError{apiErr: &APIError{ErrorCode: JSErrCodeConsumerDoesNotExist, Description: "consumer does not exist", Code: 400}} |
| 156 |
|
| 157 |
// ErrConsumerResetResponseEmpty is returned when the response from the |
| 158 |
// server to a consumer reset request is missing the ConsumerInfo |
| 159 |
// payload. The reset may or may not have taken effect. |
| 160 |
ErrConsumerResetResponseEmpty JetStreamError = &jsError{message: "consumer reset response is empty"} |
| 161 |
|
| 162 |
// ErrConsumerInvalidReset is returned when ResetConsumerToSequence is |
| 163 |
// called with a sequence that violates the consumer's DeliverPolicy |
| 164 |
// constraints (e.g. seq below OptStartSeq, or non-zero seq with a |
| 165 |
// DeliverPolicy other than all/by-start-sequence/by-start-time). |
| 166 |
ErrConsumerInvalidReset JetStreamError = &jsError{apiErr: &APIError{ErrorCode: JSErrCodeConsumerInvalidReset, Description: "invalid reset", Code: 400}} |
| 167 |
|
| 168 |
// ErrMsgNotFound is returned when message with provided sequence number |
| 169 |
// does not exist. |
| 170 |
ErrMsgNotFound JetStreamError = &jsError{apiErr: &APIError{ErrorCode: JSErrCodeMessageNotFound, Description: "message not found", Code: 404}} |
| 171 |
|
| 172 |
// ErrBadRequest is returned when invalid request is sent to JetStream API. |
| 173 |
ErrBadRequest JetStreamError = &jsError{apiErr: &APIError{ErrorCode: JSErrCodeBadRequest, Description: "bad request", Code: 400}} |
| 174 |
|
| 175 |
// ErrConsumerCreate is returned when nats-server reports error when |
| 176 |
// creating consumer (e.g. illegal update). |
| 177 |
ErrConsumerCreate JetStreamError = &jsError{apiErr: &APIError{ErrorCode: JSErrCodeConsumerCreate, Description: "could not create consumer", Code: 500}} |
| 178 |
|
| 179 |
// ErrMaximumConsumersLimit is returned when user limit of allowed |
| 180 |
// consumers for stream is reached |
| 181 |
ErrMaximumConsumersLimit JetStreamError = &jsError{apiErr: &APIError{ErrorCode: JSErrCodeMaximumConsumersLimit, Description: "maximum consumers limit reached", Code: 400}} |
| 182 |
|
| 183 |
// ErrDuplicateFilterSubjects is returned when both FilterSubject and |
| 184 |
// FilterSubjects are specified when creating consumer. |
| 185 |
ErrDuplicateFilterSubjects JetStreamError = &jsError{apiErr: &APIError{ErrorCode: JSErrCodeDuplicateFilterSubjects, Description: "consumer cannot have both FilterSubject and FilterSubjects specified", Code: 500}} |
| 186 |
|
| 187 |
// ErrOverlappingFilterSubjects is returned when filter subjects overlap when |
| 188 |
// creating consumer. |
| 189 |
ErrOverlappingFilterSubjects JetStreamError = &jsError{apiErr: &APIError{ErrorCode: JSErrCodeOverlappingFilterSubjects, Description: "consumer subject filters cannot overlap", Code: 500}} |
| 190 |
|
| 191 |
// ErrEmptyFilter is returned when a filter in FilterSubjects is empty. |
| 192 |
ErrEmptyFilter JetStreamError = &jsError{apiErr: &APIError{ErrorCode: JSErrCodeConsumerEmptyFilter, Description: "consumer filter in FilterSubjects cannot be empty", Code: 500}} |
| 193 |
|
| 194 |
// ErrScheduleTargetInvalid is returned when publishing a scheduled |
| 195 |
// message without a valid target subject. |
| 196 |
ErrScheduleTargetInvalid JetStreamError = &jsError{apiErr: &APIError{ErrorCode: JSErrCodeScheduleTargetInvalid, Description: "message schedules target is invalid", Code: 400}} |
| 197 |
|
| 198 |
// ErrSchedulePatternInvalid is returned when the schedule expression |
| 199 |
// is not a valid @at, @every, or cron pattern. |
| 200 |
ErrSchedulePatternInvalid JetStreamError = &jsError{apiErr: &APIError{ErrorCode: JSErrCodeSchedulePatternInvalid, Description: "message schedules pattern is invalid", Code: 400}} |
| 201 |
|
| 202 |
// ErrMessageSchedulesDisabled is returned when publishing a scheduled |
| 203 |
// message to a stream that does not have AllowMsgSchedules enabled. |
| 204 |
ErrMessageSchedulesDisabled JetStreamError = &jsError{apiErr: &APIError{ErrorCode: JSErrCodeMessageSchedulesDisabled, Description: "message schedules is disabled", Code: 400}} |
| 205 |
|
| 206 |
// ErrScheduleSourceInvalid is returned when the schedule source |
| 207 |
// subject is invalid. |
| 208 |
ErrScheduleSourceInvalid JetStreamError = &jsError{apiErr: &APIError{ErrorCode: JSErrCodeScheduleSourceInvalid, Description: "message schedules source is invalid", Code: 400}} |
| 209 |
|
| 210 |
// ErrScheduleTTLInvalid is returned when the schedule TTL value |
| 211 |
// is not a valid duration or "never". |
| 212 |
ErrScheduleTTLInvalid JetStreamError = &jsError{apiErr: &APIError{ErrorCode: JSErrCodeScheduleTTLInvalid, Description: "message schedules invalid per-message TTL", Code: 400}} |
| 213 |
|
| 214 |
// ErrScheduleRollupInvalid is returned when a scheduled message |
| 215 |
// has an invalid rollup configuration. |
| 216 |
ErrScheduleRollupInvalid JetStreamError = &jsError{apiErr: &APIError{ErrorCode: JSErrCodeScheduleRollupInvalid, Description: "message schedules invalid rollup", Code: 400}} |
| 217 |
|
| 218 |
// ErrMirrorWithMsgSchedules is returned when attempting to enable |
| 219 |
// message scheduling on a mirror stream. |
| 220 |
ErrMirrorWithMsgSchedules JetStreamError = &jsError{apiErr: &APIError{ErrorCode: JSErrCodeMirrorWithMsgSchedules, Description: "stream mirrors can not also schedule messages", Code: 400}} |
| 221 |
|
| 222 |
// ErrSourceWithMsgSchedules is returned when attempting to enable |
| 223 |
// message scheduling on a stream with sources. |
| 224 |
ErrSourceWithMsgSchedules JetStreamError = &jsError{apiErr: &APIError{ErrorCode: JSErrCodeSourceWithMsgSchedules, Description: "stream source can not also schedule messages", Code: 400}} |
| 225 |
|
| 226 |
// Client errors |
| 227 |
|
| 228 |
// ErrConsumerMultipleFilterSubjectsNotSupported is returned when the |
| 229 |
// connected nats-server version does not support setting multiple filter |
| 230 |
// subjects with filter_subjects field. If this error is returned when |
| 231 |
// executing AddConsumer(), the consumer with invalid configuration was |
| 232 |
// already created in the server. |
| 233 |
ErrConsumerMultipleFilterSubjectsNotSupported JetStreamError = &jsError{message: "multiple consumer filter subjects not supported by nats-server"} |
| 234 |
|
| 235 |
// ErrConsumerNameAlreadyInUse is an error returned when attempting to create |
| 236 |
// a consumer with a name that is already in use. |
| 237 |
ErrConsumerNameAlreadyInUse JetStreamError = &jsError{message: "consumer name already in use"} |
| 238 |
|
| 239 |
// ErrNotPullConsumer is returned when attempting to fetch or create pull |
| 240 |
// consumer and the returned consumer is a push consumer. |
| 241 |
ErrNotPullConsumer JetStreamError = &jsError{message: "consumer is not a pull consumer"} |
| 242 |
|
| 243 |
// ErrNotPushConsumer is returned when attempting to fetch or create push |
| 244 |
// consumer and the returned consumer is a pull consumer. |
| 245 |
ErrNotPushConsumer JetStreamError = &jsError{message: "consumer is not a push consumer"} |
| 246 |
|
| 247 |
// ErrConsumerAlreadyConsuming is returned when attempting to consume from |
| 248 |
// the same push consumer more than once. |
| 249 |
ErrConsumerAlreadyConsuming JetStreamError = &jsError{message: "consumer is already consuming"} |
| 250 |
|
| 251 |
// ErrInvalidJSAck is returned when JetStream ack from message publish is |
| 252 |
// invalid. |
| 253 |
ErrInvalidJSAck JetStreamError = &jsError{message: "invalid jetstream publish response"} |
| 254 |
|
| 255 |
// ErrStreamNameRequired is returned when the provided stream name is empty. |
| 256 |
ErrStreamNameRequired JetStreamError = &jsError{message: "stream name is required"} |
| 257 |
|
| 258 |
// ErrMsgAlreadyAckd is returned when attempting to acknowledge message more |
| 259 |
// than once. |
| 260 |
ErrMsgAlreadyAckd JetStreamError = &jsError{message: "message was already acknowledged"} |
| 261 |
|
| 262 |
// ErrNoStreamResponse is returned when there is no response from stream |
| 263 |
// (e.g. no responders error). |
| 264 |
ErrNoStreamResponse JetStreamError = &jsError{message: "no response from stream"} |
| 265 |
|
| 266 |
// ErrNotJSMessage is returned when attempting to get metadata from non |
| 267 |
// JetStream message. |
| 268 |
ErrNotJSMessage JetStreamError = &jsError{message: "not a jetstream message"} |
| 269 |
|
| 270 |
// ErrInvalidStreamName is returned when the provided stream name is invalid |
| 271 |
// (contains '.'). |
| 272 |
ErrInvalidStreamName JetStreamError = &jsError{message: "invalid stream name"} |
| 273 |
|
| 274 |
// ErrInvalidSubject is returned when the provided subject name is invalid. |
| 275 |
ErrInvalidSubject JetStreamError = &jsError{message: "invalid subject name"} |
| 276 |
|
| 277 |
// ErrInvalidConsumerName is returned when the provided consumer name is |
| 278 |
// invalid (contains '.'). |
| 279 |
ErrInvalidConsumerName JetStreamError = &jsError{message: "invalid consumer name"} |
| 280 |
|
| 281 |
// ErrNoMessages is returned when no messages are currently available for a |
| 282 |
// consumer. |
| 283 |
ErrNoMessages JetStreamError = &jsError{message: "no messages"} |
| 284 |
|
| 285 |
// ErrPinIDMismatch is returned when Pin ID sent in the request does not match |
| 286 |
// the currently pinned consumer subscriber ID on the server. |
| 287 |
ErrPinIDMismatch JetStreamError = &jsError{message: "pin ID mismatch"} |
| 288 |
|
| 289 |
// ErrMaxBytesExceeded is returned when a message would exceed MaxBytes set |
| 290 |
// on a pull request. |
| 291 |
ErrMaxBytesExceeded JetStreamError = &jsError{message: "message size exceeds max bytes"} |
| 292 |
|
| 293 |
// ErrBatchCompleted is returned when a fetch request sent the whole batch, |
| 294 |
// but there are still bytes left. This is applicable only when MaxBytes is |
| 295 |
// set on a pull request. |
| 296 |
ErrBatchCompleted JetStreamError = &jsError{message: "batch completed"} |
| 297 |
|
| 298 |
// ErrConsumerDeleted is returned when attempting to send pull request to a |
| 299 |
// consumer which does not exist. |
| 300 |
ErrConsumerDeleted JetStreamError = &jsError{message: "consumer deleted"} |
| 301 |
|
| 302 |
// ErrConsumerLeadershipChanged is returned when pending requests are no |
| 303 |
// longer valid after leadership has changed. |
| 304 |
ErrConsumerLeadershipChanged JetStreamError = &jsError{message: "leadership change"} |
| 305 |
|
| 306 |
// ErrHandlerRequired is returned when no handler func is provided in |
| 307 |
// Stream(). |
| 308 |
ErrHandlerRequired JetStreamError = &jsError{message: "handler cannot be empty"} |
| 309 |
|
| 310 |
// ErrEndOfData is returned when iterating over paged API from JetStream |
| 311 |
// reaches end of data. |
| 312 |
ErrEndOfData JetStreamError = &jsError{message: "end of data reached"} |
| 313 |
|
| 314 |
// ErrNoHeartbeat is received when no message is received in IdleHeartbeat |
| 315 |
// time (if set). |
| 316 |
ErrNoHeartbeat JetStreamError = &jsError{message: "no heartbeat received"} |
| 317 |
|
| 318 |
// ErrConsumerHasActiveSubscription is returned when a consumer is already |
| 319 |
// subscribed to a stream. |
| 320 |
ErrConsumerHasActiveSubscription JetStreamError = &jsError{message: "consumer has active subscription"} |
| 321 |
|
| 322 |
// ErrMsgNotBound is returned when given message is not bound to any |
| 323 |
// subscription. |
| 324 |
ErrMsgNotBound JetStreamError = &jsError{message: "message is not bound to subscription/connection"} |
| 325 |
|
| 326 |
// ErrMsgNoReply is returned when attempting to reply to a message without a |
| 327 |
// reply subject. |
| 328 |
ErrMsgNoReply JetStreamError = &jsError{message: "message does not have a reply"} |
| 329 |
|
| 330 |
// ErrMsgDeleteUnsuccessful is returned when an attempt to delete a message |
| 331 |
// is unsuccessful. |
| 332 |
ErrMsgDeleteUnsuccessful JetStreamError = &jsError{message: "message deletion unsuccessful"} |
| 333 |
|
| 334 |
// ErrAsyncPublishReplySubjectSet is returned when reply subject is set on |
| 335 |
// async message publish. |
| 336 |
ErrAsyncPublishReplySubjectSet JetStreamError = &jsError{message: "reply subject should be empty"} |
| 337 |
|
| 338 |
// ErrTooManyStalledMsgs is returned when too many outstanding async |
| 339 |
// messages are waiting for ack. |
| 340 |
ErrTooManyStalledMsgs JetStreamError = &jsError{message: "stalled with too many outstanding async published messages"} |
| 341 |
|
| 342 |
// ErrInvalidOption is returned when there is a collision between options. |
| 343 |
ErrInvalidOption JetStreamError = &jsError{message: "invalid jetstream option"} |
| 344 |
|
| 345 |
// ErrMsgIteratorClosed is returned when attempting to get message from a |
| 346 |
// closed iterator. |
| 347 |
ErrMsgIteratorClosed JetStreamError = &jsError{message: "messages iterator closed"} |
| 348 |
|
| 349 |
// ErrConnectionClosed is returned when JetStream operations fail due to |
| 350 |
// underlying connection being closed. |
| 351 |
ErrConnectionClosed JetStreamError = &jsError{message: "connection closed"} |
| 352 |
|
| 353 |
// ErrServerShutdown is returned when pull request fails due to server |
| 354 |
// shutdown. |
| 355 |
ErrServerShutdown JetStreamError = &jsError{message: "server shutdown"} |
| 356 |
|
| 357 |
// ErrOrderedConsumerReset indicates that the ordered consumer was |
| 358 |
// automatically reset and recreated to preserve message ordering. |
| 359 |
ErrOrderedConsumerReset JetStreamError = &jsError{message: "recreating ordered consumer"} |
| 360 |
|
| 361 |
// ErrOrderConsumerUsedAsFetch is returned when ordered consumer was already |
| 362 |
// used to process messages using Fetch (or FetchBytes). |
| 363 |
ErrOrderConsumerUsedAsFetch JetStreamError = &jsError{message: "ordered consumer initialized as fetch"} |
| 364 |
|
| 365 |
// ErrOrderConsumerUsedAsConsume is returned when ordered consumer was |
| 366 |
// already used to process messages using Consume or Messages. |
| 367 |
ErrOrderConsumerUsedAsConsume JetStreamError = &jsError{message: "ordered consumer initialized as consume"} |
| 368 |
|
| 369 |
// ErrOrderedConsumerConcurrentRequests is returned when attempting to run |
| 370 |
// concurrent operations on ordered consumers. |
| 371 |
ErrOrderedConsumerConcurrentRequests JetStreamError = &jsError{message: "cannot run concurrent processing using ordered consumer"} |
| 372 |
|
| 373 |
// ErrOrderedConsumerNotCreated is returned when trying to get consumer info |
| 374 |
// of an ordered consumer which was not yet created. |
| 375 |
ErrOrderedConsumerNotCreated JetStreamError = &jsError{message: "consumer instance not yet created"} |
| 376 |
|
| 377 |
// ErrJetStreamPublisherClosed is returned for each unfinished ack future when JetStream.Cleanup is called. |
| 378 |
ErrJetStreamPublisherClosed JetStreamError = &jsError{message: "jetstream context closed"} |
| 379 |
|
| 380 |
// ErrAsyncPublishTimeout is returned when waiting for ack on async publish |
| 381 |
ErrAsyncPublishTimeout JetStreamError = &jsError{message: "timeout waiting for ack"} |
| 382 |
|
| 383 |
// KeyValue Errors |
| 384 |
|
| 385 |
// ErrKeyExists is returned when attempting to create a key that already |
| 386 |
// exists. |
| 387 |
// |
| 388 |
// Note: ErrKeyExists matches errors by code 10071, which CAS conflicts |
| 389 |
// from Update/Delete/Purge also carry on non-replicated streams; |
| 390 |
// replicated (R>1) streams report code 10164 instead and will not match. |
| 391 |
// Do not use ErrKeyExists to detect revision conflicts - use |
| 392 |
// ErrKeyRevisionMismatch. |
| 393 |
ErrKeyExists JetStreamError = &jsError{apiErr: &APIError{ErrorCode: JSErrCodeStreamWrongLastSequence, Code: 400}, message: "key exists"} |
| 394 |
|
| 395 |
// ErrKeyRevisionMismatch is returned by Update, and by Delete/Purge when |
| 396 |
// the LastRevision option is used, if the provided revision does not |
| 397 |
// match the key's current revision (an optimistic-concurrency conflict). |
| 398 |
// Replicated (R>1) streams report this as error code 10164 instead of |
| 399 |
// 10071; both map to this error. |
| 400 |
ErrKeyRevisionMismatch JetStreamError = &jsError{message: "key revision mismatch"} |
| 401 |
|
| 402 |
// ErrKeyValueConfigRequired is returned when attempting to create a bucket |
| 403 |
// without a config. |
| 404 |
ErrKeyValueConfigRequired JetStreamError = &jsError{message: "config required"} |
| 405 |
|
| 406 |
// ErrInvalidBucketName is returned when attempting to create a bucket with |
| 407 |
// an invalid name. |
| 408 |
ErrInvalidBucketName JetStreamError = &jsError{message: "invalid bucket name"} |
| 409 |
|
| 410 |
// ErrInvalidKey is returned when attempting to create a key with an invalid |
| 411 |
// name. |
| 412 |
ErrInvalidKey JetStreamError = &jsError{message: "invalid key"} |
| 413 |
|
| 414 |
// ErrBucketExists is returned when attempting to create a bucket that |
| 415 |
// already exists and has a different configuration. |
| 416 |
ErrBucketExists JetStreamError = &jsError{message: "bucket name already in use"} |
| 417 |
|
| 418 |
// ErrBucketNotFound is returned when attempting to access a bucket that |
| 419 |
// does not exist. |
| 420 |
ErrBucketNotFound JetStreamError = &jsError{message: "bucket not found"} |
| 421 |
|
| 422 |
// ErrBadBucket is returned when attempting to access a bucket that is not a |
| 423 |
// key-value store. |
| 424 |
ErrBadBucket JetStreamError = &jsError{message: "bucket not valid key-value store"} |
| 425 |
|
| 426 |
// ErrKeyNotFound is returned when attempting to access a key that does not |
| 427 |
// exist. |
| 428 |
ErrKeyNotFound JetStreamError = &jsError{message: "key not found"} |
| 429 |
|
| 430 |
// ErrKeyDeleted is returned when attempting to access a key that was |
| 431 |
// deleted. |
| 432 |
ErrKeyDeleted JetStreamError = &jsError{message: "key was deleted"} |
| 433 |
|
| 434 |
// ErrHistoryTooLarge is returned when provided history limit is larger than |
| 435 |
// 64. |
| 436 |
ErrHistoryTooLarge JetStreamError = &jsError{message: "history limited to a max of 64"} |
| 437 |
|
| 438 |
// ErrNoKeysFound is returned when no keys are found. |
| 439 |
ErrNoKeysFound JetStreamError = &jsError{message: "no keys found"} |
| 440 |
|
| 441 |
// ErrTTLOnDeleteNotSupported is returned when attempting to set a TTL |
| 442 |
// on a delete operation. |
| 443 |
ErrTTLOnDeleteNotSupported JetStreamError = &jsError{message: "TTL is not supported on delete"} |
| 444 |
|
| 445 |
// ErrLimitMarkerTTLNotSupported is returned when the connected jetstream API |
| 446 |
// does not support setting the LimitMarkerTTL. |
| 447 |
ErrLimitMarkerTTLNotSupported JetStreamError = &jsError{message: "limit marker TTLs not supported by server"} |
| 448 |
|
| 449 |
// ErrObjectConfigRequired is returned when attempting to create an object |
| 450 |
// without a config. |
| 451 |
ErrObjectConfigRequired JetStreamError = &jsError{message: "object-store config required"} |
| 452 |
|
| 453 |
// ErrBadObjectMeta is returned when the meta information of an object is |
| 454 |
// invalid. |
| 455 |
ErrBadObjectMeta JetStreamError = &jsError{message: "object-store meta information invalid"} |
| 456 |
|
| 457 |
// ErrObjectNotFound is returned when an object is not found. |
| 458 |
ErrObjectNotFound JetStreamError = &jsError{message: "object not found"} |
| 459 |
|
| 460 |
// ErrInvalidStoreName is returned when the name of an object-store is |
| 461 |
// invalid. |
| 462 |
ErrInvalidStoreName JetStreamError = &jsError{message: "invalid object-store name"} |
| 463 |
|
| 464 |
// ErrDigestMismatch is returned when the digests of an object do not match. |
| 465 |
ErrDigestMismatch JetStreamError = &jsError{message: "received a corrupt object, digests do not match"} |
| 466 |
|
| 467 |
// ErrInvalidDigestFormat is returned when the digest hash of an object has |
| 468 |
// an invalid format. |
| 469 |
ErrInvalidDigestFormat JetStreamError = &jsError{message: "object digest hash has invalid format"} |
| 470 |
|
| 471 |
// ErrNoObjectsFound is returned when no objects are found. |
| 472 |
ErrNoObjectsFound JetStreamError = &jsError{message: "no objects found"} |
| 473 |
|
| 474 |
// ErrObjectAlreadyExists is returned when an object with the same name |
| 475 |
// already exists. |
| 476 |
ErrObjectAlreadyExists JetStreamError = &jsError{message: "an object already exists with that name"} |
| 477 |
|
| 478 |
// ErrNameRequired is returned when a name is required. |
| 479 |
ErrNameRequired JetStreamError = &jsError{message: "name is required"} |
| 480 |
|
| 481 |
// ErrLinkNotAllowed is returned when a link cannot be set when putting the |
| 482 |
// object in a bucket. |
| 483 |
ErrLinkNotAllowed JetStreamError = &jsError{message: "link cannot be set when putting the object in bucket"} |
| 484 |
|
| 485 |
// ErrObjectRequired is returned when an object is required. |
| 486 |
ErrObjectRequired = &jsError{message: "object required"} |
| 487 |
|
| 488 |
// ErrNoLinkToDeleted is returned when it is not allowed to link to a |
| 489 |
// deleted object. |
| 490 |
ErrNoLinkToDeleted JetStreamError = &jsError{message: "not allowed to link to a deleted object"} |
| 491 |
|
| 492 |
// ErrNoLinkToLink is returned when it is not allowed to link to another |
| 493 |
// link. |
| 494 |
ErrNoLinkToLink JetStreamError = &jsError{message: "not allowed to link to another link"} |
| 495 |
|
| 496 |
// ErrCantGetBucket is returned when an invalid Get is attempted on an |
| 497 |
// object that is a link to a bucket. |
| 498 |
ErrCantGetBucket JetStreamError = &jsError{message: "invalid Get, object is a link to a bucket"} |
| 499 |
|
| 500 |
// ErrBucketRequired is returned when a bucket is required. |
| 501 |
ErrBucketRequired JetStreamError = &jsError{message: "bucket required"} |
| 502 |
|
| 503 |
// ErrBucketMalformed is returned when a bucket is malformed. |
| 504 |
ErrBucketMalformed JetStreamError = &jsError{message: "bucket malformed"} |
| 505 |
|
| 506 |
// ErrUpdateMetaDeleted is returned when the meta information of a deleted |
| 507 |
// object cannot be updated. |
| 508 |
ErrUpdateMetaDeleted JetStreamError = &jsError{message: "cannot update meta for a deleted object"} |
| 509 |
) |
| 510 |
|
| 511 |
// Error prints the JetStream API error code and description. |
| 512 |
func (e *APIError) Error() string { |
| 513 |
return fmt.Sprintf("nats: API error: code=%d err_code=%d description=%s", e.Code, e.ErrorCode, e.Description) |
| 514 |
} |
| 515 |
|
| 516 |
// APIError implements the JetStreamError interface. |
| 517 |
func (e *APIError) APIError() *APIError { |
| 518 |
return e |
| 519 |
} |
| 520 |
|
| 521 |
// Is matches against an APIError. |
| 522 |
func (e *APIError) Is(err error) bool { |
| 523 |
if e == nil { |
| 524 |
return false |
| 525 |
} |
| 526 |
// Extract internal APIError to match against. |
| 527 |
var aerr *APIError |
| 528 |
ok := errors.As(err, &aerr) |
| 529 |
if !ok { |
| 530 |
return ok |
| 531 |
} |
| 532 |
return e.ErrorCode == aerr.ErrorCode |
| 533 |
} |
| 534 |
|
| 535 |
func (err *jsError) APIError() *APIError { |
| 536 |
return err.apiErr |
| 537 |
} |
| 538 |
|
| 539 |
func (err *jsError) Error() string { |
| 540 |
if err.apiErr != nil && err.apiErr.Description != "" { |
| 541 |
return err.apiErr.Error() |
| 542 |
} |
| 543 |
return fmt.Sprintf("nats: %s", err.message) |
| 544 |
} |
| 545 |
|
| 546 |
func (err *jsError) Unwrap() error { |
| 547 |
// Allow matching to embedded APIError in case there is one. |
| 548 |
if err.apiErr == nil { |
| 549 |
return nil |
| 550 |
} |
| 551 |
return err.apiErr |
| 552 |
} |