| 1 |
// Copyright 2020-2025 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 nats |
| 15 |
|
| 16 |
import ( |
| 17 |
"errors" |
| 18 |
"fmt" |
| 19 |
) |
| 20 |
|
| 21 |
var ( |
| 22 |
// API errors |
| 23 |
|
| 24 |
// ErrJetStreamNotEnabled is an error returned when JetStream is not enabled for an account. |
| 25 |
// |
| 26 |
// Note: This error will not be returned in clustered mode, even if each |
| 27 |
// server in the cluster does not have JetStream enabled. In clustered mode, |
| 28 |
// requests will time out instead. |
| 29 |
ErrJetStreamNotEnabled JetStreamError = &jsError{apiErr: &APIError{ErrorCode: JSErrCodeJetStreamNotEnabled, Description: "jetstream not enabled", Code: 503}} |
| 30 |
|
| 31 |
// ErrJetStreamNotEnabledForAccount is an error returned when JetStream is not enabled for an account. |
| 32 |
ErrJetStreamNotEnabledForAccount JetStreamError = &jsError{apiErr: &APIError{ErrorCode: JSErrCodeJetStreamNotEnabledForAccount, Description: "jetstream not enabled for account", Code: 503}} |
| 33 |
|
| 34 |
// ErrStreamNotFound is an error returned when stream with given name does not exist. |
| 35 |
ErrStreamNotFound JetStreamError = &jsError{apiErr: &APIError{ErrorCode: JSErrCodeStreamNotFound, Description: "stream not found", Code: 404}} |
| 36 |
|
| 37 |
// ErrStreamNameAlreadyInUse is returned when a stream with given name already exists and has a different configuration. |
| 38 |
ErrStreamNameAlreadyInUse JetStreamError = &jsError{apiErr: &APIError{ErrorCode: JSErrCodeStreamNameInUse, Description: "stream name already in use", Code: 400}} |
| 39 |
|
| 40 |
// ErrStreamSubjectTransformNotSupported is returned when the connected nats-server version does not support setting |
| 41 |
// the stream subject transform. If this error is returned when executing AddStream(), the stream with invalid |
| 42 |
// configuration was already created in the server. |
| 43 |
ErrStreamSubjectTransformNotSupported JetStreamError = &jsError{message: "stream subject transformation not supported by nats-server"} |
| 44 |
|
| 45 |
// ErrStreamSourceSubjectTransformNotSupported is returned when the connected nats-server version does not support setting |
| 46 |
// the stream source subject transform. If this error is returned when executing AddStream(), the stream with invalid |
| 47 |
// configuration was already created in the server. |
| 48 |
ErrStreamSourceSubjectTransformNotSupported JetStreamError = &jsError{message: "stream subject transformation not supported by nats-server"} |
| 49 |
|
| 50 |
// ErrStreamSourceNotSupported is returned when the connected nats-server version does not support setting |
| 51 |
// the stream sources. If this error is returned when executing AddStream(), the stream with invalid |
| 52 |
// configuration was already created in the server. |
| 53 |
ErrStreamSourceNotSupported JetStreamError = &jsError{message: "stream sourcing is not supported by nats-server"} |
| 54 |
|
| 55 |
// ErrStreamSourceMultipleSubjectTransformsNotSupported is returned when the connected nats-server version does not support setting |
| 56 |
// the stream sources. If this error is returned when executing AddStream(), the stream with invalid |
| 57 |
// configuration was already created in the server. |
| 58 |
ErrStreamSourceMultipleSubjectTransformsNotSupported JetStreamError = &jsError{message: "stream sourcing with multiple subject transforms not supported by nats-server"} |
| 59 |
|
| 60 |
// ErrConsumerNotFound is an error returned when consumer with given name does not exist. |
| 61 |
ErrConsumerNotFound JetStreamError = &jsError{apiErr: &APIError{ErrorCode: JSErrCodeConsumerNotFound, Description: "consumer not found", Code: 404}} |
| 62 |
|
| 63 |
// ErrConsumerCreationResponseEmpty is an error returned when the response from the server |
| 64 |
// when creating a consumer is empty. This means that the state of the consumer is unknown and |
| 65 |
// the consumer may not have been created successfully. |
| 66 |
ErrConsumerCreationResponseEmpty JetStreamError = &jsError{message: "consumer creation response is empty"} |
| 67 |
|
| 68 |
// ErrMsgNotFound is returned when message with provided sequence number does npt exist. |
| 69 |
ErrMsgNotFound JetStreamError = &jsError{apiErr: &APIError{ErrorCode: JSErrCodeMessageNotFound, Description: "message not found", Code: 404}} |
| 70 |
|
| 71 |
// ErrBadRequest is returned when invalid request is sent to JetStream API. |
| 72 |
ErrBadRequest JetStreamError = &jsError{apiErr: &APIError{ErrorCode: JSErrCodeBadRequest, Description: "bad request", Code: 400}} |
| 73 |
|
| 74 |
// ErrDuplicateFilterSubjects is returned when both FilterSubject and FilterSubjects are specified when creating consumer. |
| 75 |
ErrDuplicateFilterSubjects JetStreamError = &jsError{apiErr: &APIError{ErrorCode: JSErrCodeDuplicateFilterSubjects, Description: "consumer cannot have both FilterSubject and FilterSubjects specified", Code: 500}} |
| 76 |
|
| 77 |
// ErrDuplicateFilterSubjects is returned when filter subjects overlap when creating consumer. |
| 78 |
ErrOverlappingFilterSubjects JetStreamError = &jsError{apiErr: &APIError{ErrorCode: JSErrCodeOverlappingFilterSubjects, Description: "consumer subject filters cannot overlap", Code: 500}} |
| 79 |
|
| 80 |
// ErrEmptyFilter is returned when a filter in FilterSubjects is empty. |
| 81 |
ErrEmptyFilter JetStreamError = &jsError{apiErr: &APIError{ErrorCode: JSErrCodeConsumerEmptyFilter, Description: "consumer filter in FilterSubjects cannot be empty", Code: 500}} |
| 82 |
|
| 83 |
// Client errors |
| 84 |
|
| 85 |
// ErrConsumerNameAlreadyInUse is an error returned when consumer with given name already exists. |
| 86 |
ErrConsumerNameAlreadyInUse JetStreamError = &jsError{message: "consumer name already in use"} |
| 87 |
|
| 88 |
// ErrConsumerNotActive is an error returned when consumer is not active. |
| 89 |
ErrConsumerNotActive JetStreamError = &jsError{message: "consumer not active"} |
| 90 |
|
| 91 |
// ErrInvalidJSAck is returned when JetStream ack from message publish is invalid. |
| 92 |
ErrInvalidJSAck JetStreamError = &jsError{message: "invalid jetstream publish response"} |
| 93 |
|
| 94 |
// ErrStreamConfigRequired is returned when empty stream configuration is supplied to add/update stream. |
| 95 |
ErrStreamConfigRequired JetStreamError = &jsError{message: "stream configuration is required"} |
| 96 |
|
| 97 |
// ErrStreamNameRequired is returned when the provided stream name is empty. |
| 98 |
ErrStreamNameRequired JetStreamError = &jsError{message: "stream name is required"} |
| 99 |
|
| 100 |
// ErrConsumerNameRequired is returned when the provided consumer durable name is empty. |
| 101 |
ErrConsumerNameRequired JetStreamError = &jsError{message: "consumer name is required"} |
| 102 |
|
| 103 |
// ErrConsumerMultipleFilterSubjectsNotSupported is returned when the connected nats-server version does not support setting |
| 104 |
// multiple filter subjects with filter_subjects field. If this error is returned when executing AddConsumer(), the consumer with invalid |
| 105 |
// configuration was already created in the server. |
| 106 |
ErrConsumerMultipleFilterSubjectsNotSupported JetStreamError = &jsError{message: "multiple consumer filter subjects not supported by nats-server"} |
| 107 |
|
| 108 |
// ErrConsumerConfigRequired is returned when empty consumer consuguration is supplied to add/update consumer. |
| 109 |
ErrConsumerConfigRequired JetStreamError = &jsError{message: "consumer configuration is required"} |
| 110 |
|
| 111 |
// ErrPullSubscribeToPushConsumer is returned when attempting to use PullSubscribe on push consumer. |
| 112 |
ErrPullSubscribeToPushConsumer JetStreamError = &jsError{message: "cannot pull subscribe to push based consumer"} |
| 113 |
|
| 114 |
// ErrPullSubscribeRequired is returned when attempting to use subscribe methods not suitable for pull consumers for pull consumers. |
| 115 |
ErrPullSubscribeRequired JetStreamError = &jsError{message: "must use pull subscribe to bind to pull based consumer"} |
| 116 |
|
| 117 |
// ErrMsgAlreadyAckd is returned when attempting to acknowledge message more than once. |
| 118 |
ErrMsgAlreadyAckd JetStreamError = &jsError{message: "message was already acknowledged"} |
| 119 |
|
| 120 |
// ErrNoStreamResponse is returned when there is no response from stream (e.g. no responders error). |
| 121 |
ErrNoStreamResponse JetStreamError = &jsError{message: "no response from stream"} |
| 122 |
|
| 123 |
// ErrNotJSMessage is returned when attempting to get metadata from non JetStream message . |
| 124 |
ErrNotJSMessage JetStreamError = &jsError{message: "not a jetstream message"} |
| 125 |
|
| 126 |
// ErrInvalidStreamName is returned when the provided stream name is invalid (contains '.' or ' '). |
| 127 |
ErrInvalidStreamName JetStreamError = &jsError{message: "invalid stream name"} |
| 128 |
|
| 129 |
// ErrInvalidConsumerName is returned when the provided consumer name is invalid (contains '.' or ' '). |
| 130 |
ErrInvalidConsumerName JetStreamError = &jsError{message: "invalid consumer name"} |
| 131 |
|
| 132 |
// ErrInvalidFilterSubject is returned when the provided filter subject is invalid. |
| 133 |
ErrInvalidFilterSubject JetStreamError = &jsError{message: "invalid filter subject"} |
| 134 |
|
| 135 |
// ErrNoMatchingStream is returned when stream lookup by subject is unsuccessful. |
| 136 |
ErrNoMatchingStream JetStreamError = &jsError{message: "no stream matches subject"} |
| 137 |
|
| 138 |
// ErrSubjectMismatch is returned when the provided subject does not match consumer's filter subject. |
| 139 |
ErrSubjectMismatch JetStreamError = &jsError{message: "subject does not match consumer"} |
| 140 |
|
| 141 |
// ErrContextAndTimeout is returned when attempting to use both context and timeout. |
| 142 |
ErrContextAndTimeout JetStreamError = &jsError{message: "context and timeout can not both be set"} |
| 143 |
|
| 144 |
// ErrCantAckIfConsumerAckNone is returned when attempting to ack a message for consumer with AckNone policy set. |
| 145 |
ErrCantAckIfConsumerAckNone JetStreamError = &jsError{message: "cannot acknowledge a message for a consumer with AckNone policy"} |
| 146 |
|
| 147 |
// ErrConsumerDeleted is returned when attempting to send pull request to a consumer which does not exist |
| 148 |
ErrConsumerDeleted JetStreamError = &jsError{message: "consumer deleted"} |
| 149 |
|
| 150 |
// ErrConsumerLeadershipChanged is returned when pending requests are no longer valid after leadership has changed |
| 151 |
ErrConsumerLeadershipChanged JetStreamError = &jsError{message: "Leadership Changed"} |
| 152 |
|
| 153 |
// ErrConsumerInfoOnOrderedReset is returned when attempting to fetch consumer info for an ordered consumer that is currently being recreated. |
| 154 |
ErrConsumerInfoOnOrderedReset JetStreamError = &jsError{message: "cannot fetch consumer info; ordered consumer is being reset"} |
| 155 |
|
| 156 |
// ErrNoHeartbeat is returned when no heartbeat is received from server when sending requests with pull consumer. |
| 157 |
ErrNoHeartbeat JetStreamError = &jsError{message: "no heartbeat received"} |
| 158 |
|
| 159 |
// ErrSubscriptionClosed is returned when attempting to send pull request to a closed subscription |
| 160 |
ErrSubscriptionClosed JetStreamError = &jsError{message: "subscription closed"} |
| 161 |
|
| 162 |
// ErrJetStreamPublisherClosed is returned for each unfinished ack future when JetStream.Cleanup is called. |
| 163 |
ErrJetStreamPublisherClosed JetStreamError = &jsError{message: "jetstream context closed"} |
| 164 |
|
| 165 |
// Deprecated: ErrInvalidDurableName is no longer returned and will be removed in future releases. |
| 166 |
// Use ErrInvalidConsumerName instead. |
| 167 |
ErrInvalidDurableName = errors.New("nats: invalid durable name") |
| 168 |
|
| 169 |
// ErrAsyncPublishTimeout is returned when waiting for ack on async publish |
| 170 |
ErrAsyncPublishTimeout JetStreamError = &jsError{message: "timeout waiting for ack"} |
| 171 |
|
| 172 |
// ErrTooManyStalledMsgs is returned when too many outstanding async |
| 173 |
// messages are waiting for ack. |
| 174 |
ErrTooManyStalledMsgs JetStreamError = &jsError{message: "stalled with too many outstanding async published messages"} |
| 175 |
|
| 176 |
// ErrFetchDisconnected is returned when the connection to the server is lost |
| 177 |
// while waiting for messages to be delivered on PullSubscribe. |
| 178 |
ErrFetchDisconnected = &jsError{message: "disconnected during fetch"} |
| 179 |
) |
| 180 |
|
| 181 |
// Error code represents JetStream error codes returned by the API |
| 182 |
type ErrorCode uint16 |
| 183 |
|
| 184 |
const ( |
| 185 |
JSErrCodeJetStreamNotEnabledForAccount ErrorCode = 10039 |
| 186 |
JSErrCodeJetStreamNotEnabled ErrorCode = 10076 |
| 187 |
JSErrCodeInsufficientResourcesErr ErrorCode = 10023 |
| 188 |
JSErrCodeJetStreamNotAvailable ErrorCode = 10008 |
| 189 |
|
| 190 |
JSErrCodeStreamNotFound ErrorCode = 10059 |
| 191 |
JSErrCodeStreamNameInUse ErrorCode = 10058 |
| 192 |
|
| 193 |
JSErrCodeConsumerNotFound ErrorCode = 10014 |
| 194 |
JSErrCodeConsumerNameExists ErrorCode = 10013 |
| 195 |
JSErrCodeConsumerAlreadyExists ErrorCode = 10105 |
| 196 |
JSErrCodeDuplicateFilterSubjects ErrorCode = 10136 |
| 197 |
JSErrCodeOverlappingFilterSubjects ErrorCode = 10138 |
| 198 |
JSErrCodeConsumerEmptyFilter ErrorCode = 10139 |
| 199 |
|
| 200 |
JSErrCodeMessageNotFound ErrorCode = 10037 |
| 201 |
|
| 202 |
JSErrCodeBadRequest ErrorCode = 10003 |
| 203 |
JSStreamInvalidConfig ErrorCode = 10052 |
| 204 |
|
| 205 |
JSErrCodeStreamWrongLastSequence ErrorCode = 10071 |
| 206 |
|
| 207 |
// JSErrCodeStreamWrongLastSequenceConstant is returned instead of |
| 208 |
// JSErrCodeStreamWrongLastSequence for CAS conflicts on replicated (R>1) |
| 209 |
// streams. The two are equivalent "wrong last sequence" responses. |
| 210 |
JSErrCodeStreamWrongLastSequenceConstant ErrorCode = 10164 |
| 211 |
) |
| 212 |
|
| 213 |
// APIError is included in all API responses if there was an error. |
| 214 |
type APIError struct { |
| 215 |
Code int `json:"code"` |
| 216 |
ErrorCode ErrorCode `json:"err_code"` |
| 217 |
Description string `json:"description,omitempty"` |
| 218 |
} |
| 219 |
|
| 220 |
// Error prints the JetStream API error code and description |
| 221 |
func (e *APIError) Error() string { |
| 222 |
return fmt.Sprintf("nats: %s", e.Description) |
| 223 |
} |
| 224 |
|
| 225 |
// APIError implements the JetStreamError interface. |
| 226 |
func (e *APIError) APIError() *APIError { |
| 227 |
return e |
| 228 |
} |
| 229 |
|
| 230 |
// Is matches against an APIError. |
| 231 |
func (e *APIError) Is(err error) bool { |
| 232 |
if e == nil { |
| 233 |
return false |
| 234 |
} |
| 235 |
// Extract internal APIError to match against. |
| 236 |
var aerr *APIError |
| 237 |
ok := errors.As(err, &aerr) |
| 238 |
if !ok { |
| 239 |
return ok |
| 240 |
} |
| 241 |
return e.ErrorCode == aerr.ErrorCode |
| 242 |
} |
| 243 |
|
| 244 |
// JetStreamError is an error result that happens when using JetStream. |
| 245 |
// In case of client-side error, `APIError()` returns nil |
| 246 |
type JetStreamError interface { |
| 247 |
APIError() *APIError |
| 248 |
error |
| 249 |
} |
| 250 |
|
| 251 |
type jsError struct { |
| 252 |
apiErr *APIError |
| 253 |
message string |
| 254 |
} |
| 255 |
|
| 256 |
func (err *jsError) APIError() *APIError { |
| 257 |
return err.apiErr |
| 258 |
} |
| 259 |
|
| 260 |
func (err *jsError) Error() string { |
| 261 |
if err.apiErr != nil && err.apiErr.Description != "" { |
| 262 |
return err.apiErr.Error() |
| 263 |
} |
| 264 |
return fmt.Sprintf("nats: %s", err.message) |
| 265 |
} |
| 266 |
|
| 267 |
func (err *jsError) Unwrap() error { |
| 268 |
// Allow matching to embedded APIError in case there is one. |
| 269 |
if err.apiErr == nil { |
| 270 |
return nil |
| 271 |
} |
| 272 |
return err.apiErr |
| 273 |
} |