From ee9d10f40b8f16aacddd57f8142df7004422a532 Mon Sep 17 00:00:00 2001 From: "R.I.Pienaar" Date: Wed, 26 May 2021 08:03:12 +0200 Subject: [PATCH] restore old error constants for backwards compat Signed-off-by: R.I.Pienaar --- doc/adr/0007-error-codes.md | 14 ++-- server/errors.json | 40 +++++++--- server/errors_gen.go | 10 ++- server/jetstream.go | 16 ++-- server/jetstream_api.go | 106 +++++++++++++-------------- server/jetstream_cluster.go | 10 +-- server/jetstream_errors.go | 6 +- server/jetstream_errors_generated.go | 30 +++++++- server/jetstream_errors_test.go | 24 +++--- server/stream.go | 2 +- 10 files changed, 156 insertions(+), 102 deletions(-) diff --git a/doc/adr/0007-error-codes.md b/doc/adr/0007-error-codes.md index 40cf48c2..e640c069 100644 --- a/doc/adr/0007-error-codes.md +++ b/doc/adr/0007-error-codes.md @@ -77,7 +77,8 @@ if err != nil { ``` If we have to do string interpolation of the error body, here the `JSStreamRestoreErrF` has the body -`"restore failed: {err}"`, the `NewT()` will simply use `strings.Replaces()` to create a new `ApiError` with the full string: +`"restore failed: {err}"`, the `NewT()` will simply use `strings.Replaces()` to create a new `ApiError` with the full string, +note this is a new instance of ApiError so normal compare of `err == ApiErrors[x]` won't match: ```go err = doRestore() @@ -86,9 +87,9 @@ if err != nil { } ``` -If we had to handle an error that may be an `ApiError` or a traditional go error we can use the `ErrOrNew` function, -this will look at the result from `lookupConsumer()`, if it's an `ApiError` that error will be set else a `JSConsumerNotFoundErr` -will be created. Essentially the `lookupConsumer()` would return a `JSStreamNotFoundErr` if the stream does not exist else +If we had to handle an error that may be an `ApiError` or a traditional go error we can use the `ErrOr` function, +this will look at the result from `lookupConsumer()`, if it's an `ApiError` that error will be set else `JSConsumerNotFoundErr` be +returned. Essentially the `lookupConsumer()` would return a `JSStreamNotFoundErr` if the stream does not exist else a `JSConsumerNotFoundErr` or go error on I/O failure for example. ```go @@ -96,10 +97,13 @@ var resp = JSApiConsumerCreateResponse{ApiResponse: ApiResponse{Type: JSApiStrea _, err = lookupConsumer(stream, consumer) if err != nil { - resp.Error = ApiErrors[JSConsumerNotFoundErr].ErrOrNew(err) + resp.Error = ApiErrors[JSConsumerNotFoundErr].ErrOr(err) } ``` +While the `ErrOr` function returns the `ApiErrors` pointer exactly - meaning `err == ApiErrors[x]`, the counterpart +`ErrOrNewT` will create a new instance. + ### Testing Errors Should you need to determine if a error is of a specific kind (error code) this can be done using the `IsNatsErr()` function: diff --git a/server/errors.json b/server/errors.json index c621f833..719f88dd 100644 --- a/server/errors.json +++ b/server/errors.json @@ -63,7 +63,8 @@ "constant": "JSInsufficientResourcesErr", "code": 503, "error_code": 10023, - "description": "insufficient resources" + "description": "insufficient resources", + "deprecates": "ErrJetStreamResourcesExceeded" }, { "constant": "JSMirrorMaxMessageSizeTooBigErr", @@ -187,9 +188,17 @@ { "constant": "JSNotEnabledErr", "code": 503, + "error_code": 10076, + "description": "JetStream not enabled", + "help": "This error indicates that JetStream is not enabled at a global level", + "deprectes": "ErrJetStreamNotEnabled" + }, + { + "constant": "JSNotEnabledForAccountErr", + "code": 503, "error_code": 10039, "description": "JetStream not enabled for account", - "help": "This error indicates that JetStream is not enabled either at a global level or at global and account level" + "help": "This error indicates that JetStream is not enabled for an account account level" }, { "constant": "JSSequenceNotFoundErrF", @@ -225,7 +234,8 @@ "constant": "JSStorageResourcesExceededErr", "code": 500, "error_code": 10047, - "description": "insufficient storage resources available" + "description": "insufficient storage resources available", + "deprecates": "ErrStorageResourcesExceeded" }, { "constant": "JSStreamMismatchErr", @@ -256,7 +266,8 @@ "constant": "JSStreamNameExistErr", "code": 400, "error_code": 10058, - "description": "stream name already in use" + "description": "stream name already in use", + "deprecates": "ErrJetStreamStreamAlreadyUsed" }, { "constant": "JSClusterTagsErr", @@ -313,19 +324,22 @@ "constant": "JSClusterNotAssignedErr", "code": 500, "error_code": 10007, - "description": "JetStream cluster not assigned to this server" + "description": "JetStream cluster not assigned to this server", + "deprecates": "ErrJetStreamNotAssigned" }, { "constant": "JSClusterNotLeaderErr", "code": 500, "error_code": 10009, - "description": "JetStream cluster can not handle request" + "description": "JetStream cluster can not handle request", + "deprecates": "ErrJetStreamNotLeader" }, { "constant": "JSConsumerNameExistErr", "code": 400, "error_code": 10013, - "description": "consumer name already in use" + "description": "consumer name already in use", + "deprecates": "ErrJetStreamConsumerAlreadyUsed" }, { "constant": "JSMirrorWithSourcesErr", @@ -337,7 +351,8 @@ "constant": "JSStreamNotFoundErr", "code": 404, "error_code": 10059, - "description": "stream not found" + "description": "stream not found", + "deprecates": "ErrJetStreamStreamNotFound" }, { "constant": "JSClusterRequiredErr", @@ -380,7 +395,8 @@ "constant": "JSClusterNotActiveErr", "code": 500, "error_code": 10006, - "description": "JetStream not in clustered mode" + "description": "JetStream not in clustered mode", + "deprecates": "ErrJetStreamNotClustered" }, { "constant": "JSConsumerDurableNameNotMatchSubjectErr", @@ -392,7 +408,8 @@ "constant": "JSMemoryResourcesExceededErr", "code": 500, "error_code": 10028, - "description": "insufficient memory resources available" + "description": "insufficient memory resources available", + "deprecates": "ErrMemoryResourcesExceeded" }, { "constant": "JSMirrorWithSubjectFiltersErr", @@ -442,7 +459,8 @@ "constant": "JSStreamReplicasNotSupportedErr", "code": 500, "error_code": 10074, - "description": "replicas > 1 not supported in non-clustered mode" + "description": "replicas > 1 not supported in non-clustered mode", + "deprecates": "ErrReplicasNotSupported" }, { "constant": "JSStreamMsgDeleteFailedF", diff --git a/server/errors_gen.go b/server/errors_gen.go index d88ccaad..2113ea29 100644 --- a/server/errors_gen.go +++ b/server/errors_gen.go @@ -33,7 +33,14 @@ var ( ApiErrors = map[ErrorIdentifier]*ApiError{ {{- range $i, $error := . }} {{ .Constant }}: {Code: {{ .Code }},ErrCode: {{ .ErrCode }},Description: {{ .Description | printf "%q" }},{{- if .Help }}Help: {{ .Help | printf "%q" }},{{- end }}{{- if .URL }}URL: {{ .URL | printf "%q" }},{{- end }} },{{- end }} - } + } + +{{- range $i, $error := . }} +{{- if .Deprecates }} +// {{ .Deprecates }} Deprecated by {{ .Constant }} ApiError, use IsNatsError() for comparisons +{{ .Deprecates }} = ApiErrors[{{ .Constant }}] +{{- end }} +{{- end }} ) ` @@ -52,6 +59,7 @@ type Errors struct { Comment string `json:"comment"` Help string `json:"help"` URL string `json:"url"` + Deprecates string `json:"deprecates"` } func goFmt(file string) error { diff --git a/server/jetstream.go b/server/jetstream.go index b5de3888..78f2d564 100644 --- a/server/jetstream.go +++ b/server/jetstream.go @@ -775,7 +775,7 @@ func (s *Server) JetStreamNumAccounts() int { func (s *Server) JetStreamReservedResources() (int64, int64, error) { js := s.getJetStream() if js == nil { - return -1, -1, ApiErrors[JSNotEnabledErr] + return -1, -1, ApiErrors[JSNotEnabledForAccountErr] } js.mu.RLock() defer js.mu.RUnlock() @@ -1107,7 +1107,7 @@ func (a *Account) lookupStream(name string) (*stream, error) { a.mu.RUnlock() if jsa == nil { - return nil, ApiErrors[JSNotEnabledErr] + return nil, ApiErrors[JSNotEnabledForAccountErr] } jsa.mu.Lock() defer jsa.mu.Unlock() @@ -1134,7 +1134,7 @@ func (a *Account) UpdateJetStreamLimits(limits *JetStreamAccountLimits) error { return ApiErrors[JSNotEnabledErr] } if jsa == nil { - return ApiErrors[JSNotEnabledErr] + return ApiErrors[JSNotEnabledForAccountErr] } if limits == nil { @@ -1223,7 +1223,7 @@ func (a *Account) DisableJetStream() error { js := s.getJetStream() if js == nil { - return ApiErrors[JSNotEnabledErr] + return ApiErrors[JSNotEnabledForAccountErr] } // Remove service imports. @@ -1248,7 +1248,7 @@ func (a *Account) removeJetStream() error { js := s.getJetStream() if js == nil { - return ApiErrors[JSNotEnabledErr] + return ApiErrors[JSNotEnabledForAccountErr] } return js.disableJetStream(js.lookupAccount(a)) @@ -1257,7 +1257,7 @@ func (a *Account) removeJetStream() error { // Disable JetStream for the account. func (js *jetStream) disableJetStream(jsa *jsAccount) error { if jsa == nil || jsa.account == nil { - return ApiErrors[JSNotEnabledErr] + return ApiErrors[JSNotEnabledForAccountErr] } js.mu.Lock() @@ -1664,7 +1664,7 @@ func (a *Account) checkForJetStream() (*Server, *jsAccount, error) { a.mu.RUnlock() if s == nil || jsa == nil { - return nil, nil, ApiErrors[JSNotEnabledErr] + return nil, nil, ApiErrors[JSNotEnabledForAccountErr] } return s, jsa, nil @@ -1875,7 +1875,7 @@ func (t *streamTemplate) delete() error { t.mu.Unlock() if jsa == nil { - return ApiErrors[JSNotEnabledErr] + return ApiErrors[JSNotEnabledForAccountErr] } jsa.mu.Lock() diff --git a/server/jetstream_api.go b/server/jetstream_api.go index 42d396db..978fc933 100644 --- a/server/jetstream_api.go +++ b/server/jetstream_api.go @@ -867,7 +867,7 @@ func (s *Server) jsAccountInfoRequest(sub *subscription, c *client, subject, rep if !doErr { return } - resp.Error = ApiErrors[JSNotEnabledErr] + resp.Error = ApiErrors[JSNotEnabledForAccountErr] } else { stats := acc.JetStreamUsage() resp.JetStreamAccountStats = &stats @@ -905,7 +905,7 @@ func (s *Server) jsTemplateCreateRequest(sub *subscription, c *client, subject, var resp = JSApiStreamTemplateCreateResponse{ApiResponse: ApiResponse{Type: JSApiStreamTemplateCreateResponseType}} if !acc.JetStreamEnabled() { - resp.Error = ApiErrors[JSNotEnabledErr] + resp.Error = ApiErrors[JSNotEnabledForAccountErr] s.sendAPIErrResponse(ci, acc, subject, reply, string(msg), s.jsonResponse(&resp)) return } @@ -932,7 +932,7 @@ func (s *Server) jsTemplateCreateRequest(sub *subscription, c *client, subject, t, err := acc.addStreamTemplate(&cfg) if err != nil { - resp.Error = ApiErrors[JSStreamTemplateCreateErrF].ErrOrNew(err) + resp.Error = ApiErrors[JSStreamTemplateCreateErrF].ErrOr(err) s.sendAPIErrResponse(ci, acc, subject, reply, string(msg), s.jsonResponse(&resp)) return } @@ -960,7 +960,7 @@ func (s *Server) jsTemplateNamesRequest(sub *subscription, c *client, subject, r var resp = JSApiStreamTemplateNamesResponse{ApiResponse: ApiResponse{Type: JSApiStreamTemplateNamesResponseType}} if !acc.JetStreamEnabled() { - resp.Error = ApiErrors[JSNotEnabledErr] + resp.Error = ApiErrors[JSNotEnabledForAccountErr] s.sendAPIErrResponse(ci, acc, subject, reply, string(msg), s.jsonResponse(&resp)) return } @@ -1024,7 +1024,7 @@ func (s *Server) jsTemplateInfoRequest(sub *subscription, c *client, subject, re var resp = JSApiStreamTemplateInfoResponse{ApiResponse: ApiResponse{Type: JSApiStreamTemplateInfoResponseType}} if !acc.JetStreamEnabled() { - resp.Error = ApiErrors[JSNotEnabledErr] + resp.Error = ApiErrors[JSNotEnabledForAccountErr] s.sendAPIErrResponse(ci, acc, subject, reply, string(msg), s.jsonResponse(&resp)) return } @@ -1065,7 +1065,7 @@ func (s *Server) jsTemplateDeleteRequest(sub *subscription, c *client, subject, var resp = JSApiStreamTemplateDeleteResponse{ApiResponse: ApiResponse{Type: JSApiStreamTemplateDeleteResponseType}} if !acc.JetStreamEnabled() { - resp.Error = ApiErrors[JSNotEnabledErr] + resp.Error = ApiErrors[JSNotEnabledForAccountErr] s.sendAPIErrResponse(ci, acc, subject, reply, string(msg), s.jsonResponse(&resp)) return } @@ -1077,7 +1077,7 @@ func (s *Server) jsTemplateDeleteRequest(sub *subscription, c *client, subject, name := templateNameFromSubject(subject) err = acc.deleteStreamTemplate(name) if err != nil { - resp.Error = ApiErrors[JSStreamTemplateDeleteErrF].ErrOrNew(err) + resp.Error = ApiErrors[JSStreamTemplateDeleteErrF].ErrOr(err) s.sendAPIErrResponse(ci, acc, subject, reply, string(msg), s.jsonResponse(&resp)) return } @@ -1126,7 +1126,7 @@ func (s *Server) jsStreamCreateRequest(sub *subscription, c *client, subject, re if hasJS, doErr := acc.checkJetStream(); !hasJS { if doErr { - resp.Error = ApiErrors[JSNotEnabledErr] + resp.Error = ApiErrors[JSNotEnabledForAccountErr] s.sendAPIErrResponse(ci, acc, subject, reply, string(msg), s.jsonResponse(&resp)) } return @@ -1315,7 +1315,7 @@ func (s *Server) jsStreamUpdateRequest(sub *subscription, c *client, subject, re if hasJS, doErr := acc.checkJetStream(); !hasJS { if doErr { - resp.Error = ApiErrors[JSNotEnabledErr] + resp.Error = ApiErrors[JSNotEnabledForAccountErr] s.sendAPIErrResponse(ci, acc, subject, reply, string(msg), s.jsonResponse(&resp)) } return @@ -1348,13 +1348,13 @@ func (s *Server) jsStreamUpdateRequest(sub *subscription, c *client, subject, re mset, err := acc.lookupStream(streamName) if err != nil { - resp.Error = ApiErrors[JSStreamNotFoundErr].ErrOrNew(err) + resp.Error = ApiErrors[JSStreamNotFoundErr].ErrOr(err) s.sendAPIErrResponse(ci, acc, subject, reply, string(msg), s.jsonResponse(&resp)) return } if err := mset.update(&cfg); err != nil { - resp.Error = ApiErrors[JSStreamUpdateErrF].ErrOrNew(err) + resp.Error = ApiErrors[JSStreamUpdateErrF].ErrOr(err) s.sendAPIErrResponse(ci, acc, subject, reply, string(msg), s.jsonResponse(&resp)) return } @@ -1397,7 +1397,7 @@ func (s *Server) jsStreamNamesRequest(sub *subscription, c *client, subject, rep if hasJS, doErr := acc.checkJetStream(); !hasJS { if doErr { - resp.Error = ApiErrors[JSNotEnabledErr] + resp.Error = ApiErrors[JSNotEnabledForAccountErr] s.sendAPIErrResponse(ci, acc, subject, reply, string(msg), s.jsonResponse(&resp)) } return @@ -1524,7 +1524,7 @@ func (s *Server) jsStreamListRequest(sub *subscription, c *client, subject, repl if hasJS, doErr := acc.checkJetStream(); !hasJS { if doErr { - resp.Error = ApiErrors[JSNotEnabledErr] + resp.Error = ApiErrors[JSNotEnabledForAccountErr] s.sendAPIErrResponse(ci, acc, subject, reply, string(msg), s.jsonResponse(&resp)) } return @@ -1610,7 +1610,7 @@ func (s *Server) jsStreamInfoRequest(sub *subscription, c *client, subject, repl // We can't find the stream, so mimic what would be the errors below. if hasJS, doErr := acc.checkJetStream(); !hasJS { if doErr { - resp.Error = ApiErrors[JSNotEnabledErr] + resp.Error = ApiErrors[JSNotEnabledForAccountErr] s.sendAPIErrResponse(ci, acc, subject, reply, string(msg), s.jsonResponse(&resp)) } return @@ -1644,7 +1644,7 @@ func (s *Server) jsStreamInfoRequest(sub *subscription, c *client, subject, repl if hasJS, doErr := acc.checkJetStream(); !hasJS { if doErr { - resp.Error = ApiErrors[JSNotEnabledErr] + resp.Error = ApiErrors[JSNotEnabledForAccountErr] s.sendAPIErrResponse(ci, acc, subject, reply, string(msg), s.jsonResponse(&resp)) } return @@ -1663,7 +1663,7 @@ func (s *Server) jsStreamInfoRequest(sub *subscription, c *client, subject, repl mset, err := acc.lookupStream(streamName) if err != nil { - resp.Error = ApiErrors[JSStreamNotFoundErr].ErrOrNew(err) + resp.Error = ApiErrors[JSStreamNotFoundErr].ErrOr(err) s.sendAPIErrResponse(ci, acc, subject, reply, string(msg), s.jsonResponse(&resp)) return } @@ -1739,7 +1739,7 @@ func (s *Server) jsStreamLeaderStepDownRequest(sub *subscription, c *client, sub if hasJS, doErr := acc.checkJetStream(); !hasJS { if doErr { - resp.Error = ApiErrors[JSNotEnabledErr] + resp.Error = ApiErrors[JSNotEnabledForAccountErr] s.sendAPIErrResponse(ci, acc, subject, reply, string(msg), s.jsonResponse(&resp)) } return @@ -1764,7 +1764,7 @@ func (s *Server) jsStreamLeaderStepDownRequest(sub *subscription, c *client, sub mset, err := acc.lookupStream(name) if err != nil { - resp.Error = ApiErrors[JSStreamNotFoundErr].ErrOrNew(err) + resp.Error = ApiErrors[JSStreamNotFoundErr].ErrOr(err) s.sendAPIErrResponse(ci, acc, subject, reply, string(msg), s.jsonResponse(&resp)) return } @@ -1848,7 +1848,7 @@ func (s *Server) jsConsumerLeaderStepDownRequest(sub *subscription, c *client, s if hasJS, doErr := acc.checkJetStream(); !hasJS { if doErr { - resp.Error = ApiErrors[JSNotEnabledErr] + resp.Error = ApiErrors[JSNotEnabledForAccountErr] s.sendAPIErrResponse(ci, acc, subject, reply, string(msg), s.jsonResponse(&resp)) } return @@ -1924,7 +1924,7 @@ func (s *Server) jsStreamRemovePeerRequest(sub *subscription, c *client, subject if hasJS, doErr := acc.checkJetStream(); !hasJS { if doErr { - resp.Error = ApiErrors[JSNotEnabledErr] + resp.Error = ApiErrors[JSNotEnabledForAccountErr] s.sendAPIErrResponse(ci, acc, subject, reply, string(msg), s.jsonResponse(&resp)) } return @@ -2117,7 +2117,7 @@ func (s *Server) jsLeaderStepDownRequest(sub *subscription, c *client, subject, // Call actual stepdown. err = cc.meta.StepDown(preferredLeader) if err != nil { - resp.Error = ApiErrors[JSRaftGeneralErrF].ErrOrNew(err) + resp.Error = ApiErrors[JSRaftGeneralErrF].ErrOr(err) } else { resp.Success = true } @@ -2175,7 +2175,7 @@ func (s *Server) jsStreamDeleteRequest(sub *subscription, c *client, subject, re if hasJS, doErr := acc.checkJetStream(); !hasJS { if doErr { - resp.Error = ApiErrors[JSNotEnabledErr] + resp.Error = ApiErrors[JSNotEnabledForAccountErr] s.sendAPIErrResponse(ci, acc, subject, reply, string(msg), s.jsonResponse(&resp)) } return @@ -2196,13 +2196,13 @@ func (s *Server) jsStreamDeleteRequest(sub *subscription, c *client, subject, re mset, err := acc.lookupStream(stream) if err != nil { - resp.Error = ApiErrors[JSStreamNotFoundErr].ErrOrNew(err) + resp.Error = ApiErrors[JSStreamNotFoundErr].ErrOr(err) s.sendAPIErrResponse(ci, acc, subject, reply, string(msg), s.jsonResponse(&resp)) return } if err := mset.delete(); err != nil { - resp.Error = ApiErrors[JSStreamDeleteErrF].ErrOrNew(err) + resp.Error = ApiErrors[JSStreamDeleteErrF].ErrOr(err) s.sendAPIErrResponse(ci, acc, subject, reply, string(msg), s.jsonResponse(&resp)) return } @@ -2247,7 +2247,7 @@ func (s *Server) jsMsgDeleteRequest(sub *subscription, c *client, subject, reply // We can't find the stream, so mimic what would be the errors below. if hasJS, doErr := acc.checkJetStream(); !hasJS { if doErr { - resp.Error = ApiErrors[JSNotEnabledErr] + resp.Error = ApiErrors[JSNotEnabledForAccountErr] s.sendAPIErrResponse(ci, acc, subject, reply, string(msg), s.jsonResponse(&resp)) } return @@ -2275,7 +2275,7 @@ func (s *Server) jsMsgDeleteRequest(sub *subscription, c *client, subject, reply if hasJS, doErr := acc.checkJetStream(); !hasJS { if doErr { - resp.Error = ApiErrors[JSNotEnabledErr] + resp.Error = ApiErrors[JSNotEnabledForAccountErr] s.sendAPIErrResponse(ci, acc, subject, reply, string(msg), s.jsonResponse(&resp)) } return @@ -2294,7 +2294,7 @@ func (s *Server) jsMsgDeleteRequest(sub *subscription, c *client, subject, reply mset, err := acc.lookupStream(stream) if err != nil { - resp.Error = ApiErrors[JSStreamNotFoundErr].ErrOrNew(err) + resp.Error = ApiErrors[JSStreamNotFoundErr].ErrOr(err) s.sendAPIErrResponse(ci, acc, subject, reply, string(msg), s.jsonResponse(&resp)) return } @@ -2311,7 +2311,7 @@ func (s *Server) jsMsgDeleteRequest(sub *subscription, c *client, subject, reply removed, err = mset.eraseMsg(req.Seq) } if err != nil { - resp.Error = ApiErrors[JSStreamMsgDeleteFailedF].ErrOrNew(err) + resp.Error = ApiErrors[JSStreamMsgDeleteFailedF].ErrOr(err) } else if !removed { resp.Error = ApiErrors[JSSequenceNotFoundErrF].NewT("{seq}", req.Seq) } else { @@ -2356,7 +2356,7 @@ func (s *Server) jsMsgGetRequest(sub *subscription, c *client, subject, reply st // We can't find the stream, so mimic what would be the errors below. if hasJS, doErr := acc.checkJetStream(); !hasJS { if doErr { - resp.Error = ApiErrors[JSNotEnabledErr] + resp.Error = ApiErrors[JSNotEnabledForAccountErr] s.sendAPIErrResponse(ci, acc, subject, reply, string(msg), s.jsonResponse(&resp)) } return @@ -2384,7 +2384,7 @@ func (s *Server) jsMsgGetRequest(sub *subscription, c *client, subject, reply st if hasJS, doErr := acc.checkJetStream(); !hasJS { if doErr { - resp.Error = ApiErrors[JSNotEnabledErr] + resp.Error = ApiErrors[JSNotEnabledForAccountErr] s.sendAPIErrResponse(ci, acc, subject, reply, string(msg), s.jsonResponse(&resp)) } return @@ -2455,7 +2455,7 @@ func (s *Server) jsStreamPurgeRequest(sub *subscription, c *client, subject, rep // We can't find the stream, so mimic what would be the errors below. if hasJS, doErr := acc.checkJetStream(); !hasJS { if doErr { - resp.Error = ApiErrors[JSNotEnabledErr] + resp.Error = ApiErrors[JSNotEnabledForAccountErr] s.sendAPIErrResponse(ci, acc, subject, reply, string(msg), s.jsonResponse(&resp)) } return @@ -2491,7 +2491,7 @@ func (s *Server) jsStreamPurgeRequest(sub *subscription, c *client, subject, rep if hasJS, doErr := acc.checkJetStream(); !hasJS { if doErr { - resp.Error = ApiErrors[JSNotEnabledErr] + resp.Error = ApiErrors[JSNotEnabledForAccountErr] s.sendAPIErrResponse(ci, acc, subject, reply, string(msg), s.jsonResponse(&resp)) } return @@ -2504,7 +2504,7 @@ func (s *Server) jsStreamPurgeRequest(sub *subscription, c *client, subject, rep } mset, err := acc.lookupStream(stream) if err != nil { - resp.Error = ApiErrors[JSStreamNotFoundErr].ErrOrNew(err) + resp.Error = ApiErrors[JSStreamNotFoundErr].ErrOr(err) s.sendAPIErrResponse(ci, acc, subject, reply, string(msg), s.jsonResponse(&resp)) return } @@ -2516,7 +2516,7 @@ func (s *Server) jsStreamPurgeRequest(sub *subscription, c *client, subject, rep purged, err := mset.purge() if err != nil { - resp.Error = ApiErrors[JSStreamGeneralErrorF].ErrOrNew(err) + resp.Error = ApiErrors[JSStreamGeneralErrorF].ErrOr(err) } else { resp.Purged = purged resp.Success = true @@ -2537,7 +2537,7 @@ func (s *Server) jsStreamRestoreRequest(sub *subscription, c *client, subject, r var resp = JSApiStreamRestoreResponse{ApiResponse: ApiResponse{Type: JSApiStreamRestoreResponseType}} if !acc.JetStreamEnabled() { - resp.Error = ApiErrors[JSNotEnabledErr] + resp.Error = ApiErrors[JSNotEnabledForAccountErr] s.sendAPIErrResponse(ci, acc, subject, reply, string(msg), s.jsonResponse(&resp)) return } @@ -2573,7 +2573,7 @@ func (s *Server) jsStreamRestoreRequest(sub *subscription, c *client, subject, r if hasJS, doErr := acc.checkJetStream(); !hasJS { if doErr { - resp.Error = ApiErrors[JSNotEnabledErr] + resp.Error = ApiErrors[JSNotEnabledForAccountErr] s.sendAPIErrResponse(ci, acc, subject, reply, string(msg), s.jsonResponse(&resp)) } return @@ -2802,7 +2802,7 @@ func (s *Server) jsStreamSnapshotRequest(sub *subscription, c *client, subject, var resp = JSApiStreamSnapshotResponse{ApiResponse: ApiResponse{Type: JSApiStreamSnapshotResponseType}} if !acc.JetStreamEnabled() { - resp.Error = ApiErrors[JSNotEnabledErr] + resp.Error = ApiErrors[JSNotEnabledForAccountErr] s.sendAPIErrResponse(ci, acc, subject, reply, smsg, s.jsonResponse(&resp)) return } @@ -2814,14 +2814,14 @@ func (s *Server) jsStreamSnapshotRequest(sub *subscription, c *client, subject, mset, err := acc.lookupStream(stream) if err != nil { - resp.Error = ApiErrors[JSStreamNotFoundErr].ErrOrNew(err) + resp.Error = ApiErrors[JSStreamNotFoundErr].ErrOr(err) s.sendAPIErrResponse(ci, acc, subject, reply, smsg, s.jsonResponse(&resp)) return } if hasJS, doErr := acc.checkJetStream(); !hasJS { if doErr { - resp.Error = ApiErrors[JSNotEnabledErr] + resp.Error = ApiErrors[JSNotEnabledForAccountErr] s.sendAPIErrResponse(ci, acc, subject, reply, smsg, s.jsonResponse(&resp)) } return @@ -2853,7 +2853,7 @@ func (s *Server) jsStreamSnapshotRequest(sub *subscription, c *client, subject, sr, err := mset.snapshot(0, req.CheckMsgs, !req.NoConsumers) if err != nil { s.Warnf("Snapshot of stream '%s > %s' failed: %v", mset.jsa.account.Name, mset.name(), err) - resp.Error = ApiErrors[JSStreamSnapshotErrF].ErrOrNew(err) + resp.Error = ApiErrors[JSStreamSnapshotErrF].ErrOr(err) s.sendAPIErrResponse(ci, acc, subject, reply, smsg, s.jsonResponse(&resp)) return } @@ -3049,7 +3049,7 @@ func (s *Server) jsConsumerCreate(sub *subscription, c *client, subject, reply s if hasJS, doErr := acc.checkJetStream(); !hasJS { if doErr { - resp.Error = ApiErrors[JSNotEnabledErr] + resp.Error = ApiErrors[JSNotEnabledForAccountErr] s.sendAPIErrResponse(ci, acc, subject, reply, string(msg), s.jsonResponse(&resp)) } return @@ -3098,14 +3098,14 @@ func (s *Server) jsConsumerCreate(sub *subscription, c *client, subject, reply s stream, err := acc.lookupStream(req.Stream) if err != nil { - resp.Error = ApiErrors[JSStreamNotFoundErr].ErrOrNew(err) + resp.Error = ApiErrors[JSStreamNotFoundErr].ErrOr(err) s.sendAPIErrResponse(ci, acc, subject, reply, string(msg), s.jsonResponse(&resp)) return } o, err := stream.addConsumer(&req.Config) if err != nil { - resp.Error = ApiErrors[JSConsumerCreateErrF].ErrOrNew(err) + resp.Error = ApiErrors[JSConsumerCreateErrF].ErrOr(err) s.sendAPIErrResponse(ci, acc, subject, reply, string(msg), s.jsonResponse(&resp)) return } @@ -3148,7 +3148,7 @@ func (s *Server) jsConsumerNamesRequest(sub *subscription, c *client, subject, r if hasJS, doErr := acc.checkJetStream(); !hasJS { if doErr { - resp.Error = ApiErrors[JSNotEnabledErr] + resp.Error = ApiErrors[JSNotEnabledForAccountErr] s.sendAPIErrResponse(ci, acc, subject, reply, string(msg), s.jsonResponse(&resp)) } return @@ -3205,7 +3205,7 @@ func (s *Server) jsConsumerNamesRequest(sub *subscription, c *client, subject, r } else { mset, err := acc.lookupStream(streamName) if err != nil { - resp.Error = ApiErrors[JSStreamNotFoundErr].ErrOrNew(err) + resp.Error = ApiErrors[JSStreamNotFoundErr].ErrOr(err) s.sendAPIErrResponse(ci, acc, subject, reply, string(msg), s.jsonResponse(&resp)) return } @@ -3269,7 +3269,7 @@ func (s *Server) jsConsumerListRequest(sub *subscription, c *client, subject, re if hasJS, doErr := acc.checkJetStream(); !hasJS { if doErr { - resp.Error = ApiErrors[JSNotEnabledErr] + resp.Error = ApiErrors[JSNotEnabledForAccountErr] s.sendAPIErrResponse(ci, acc, subject, reply, string(msg), s.jsonResponse(&resp)) } return @@ -3299,7 +3299,7 @@ func (s *Server) jsConsumerListRequest(sub *subscription, c *client, subject, re mset, err := acc.lookupStream(streamName) if err != nil { - resp.Error = ApiErrors[JSStreamNotFoundErr].ErrOrNew(err) + resp.Error = ApiErrors[JSStreamNotFoundErr].ErrOr(err) s.sendAPIErrResponse(ci, acc, subject, reply, string(msg), s.jsonResponse(&resp)) return } @@ -3362,7 +3362,7 @@ func (s *Server) jsConsumerInfoRequest(sub *subscription, c *client, subject, re // We can't find the consumer, so mimic what would be the errors below. if hasJS, doErr := acc.checkJetStream(); !hasJS { if doErr { - resp.Error = ApiErrors[JSNotEnabledErr] + resp.Error = ApiErrors[JSNotEnabledForAccountErr] s.sendAPIErrResponse(ci, acc, subject, reply, string(msg), s.jsonResponse(&resp)) } return @@ -3410,7 +3410,7 @@ func (s *Server) jsConsumerInfoRequest(sub *subscription, c *client, subject, re } if !acc.JetStreamEnabled() { - resp.Error = ApiErrors[JSNotEnabledErr] + resp.Error = ApiErrors[JSNotEnabledForAccountErr] s.sendAPIErrResponse(ci, acc, subject, reply, string(msg), s.jsonResponse(&resp)) return } @@ -3422,7 +3422,7 @@ func (s *Server) jsConsumerInfoRequest(sub *subscription, c *client, subject, re mset, err := acc.lookupStream(streamName) if err != nil { - resp.Error = ApiErrors[JSStreamNotFoundErr].ErrOrNew(err) + resp.Error = ApiErrors[JSStreamNotFoundErr].ErrOr(err) s.sendAPIErrResponse(ci, acc, subject, reply, string(msg), s.jsonResponse(&resp)) return } @@ -3469,7 +3469,7 @@ func (s *Server) jsConsumerDeleteRequest(sub *subscription, c *client, subject, if hasJS, doErr := acc.checkJetStream(); !hasJS { if doErr { - resp.Error = ApiErrors[JSNotEnabledErr] + resp.Error = ApiErrors[JSNotEnabledForAccountErr] s.sendAPIErrResponse(ci, acc, subject, reply, string(msg), s.jsonResponse(&resp)) } return @@ -3489,7 +3489,7 @@ func (s *Server) jsConsumerDeleteRequest(sub *subscription, c *client, subject, mset, err := acc.lookupStream(stream) if err != nil { - resp.Error = ApiErrors[JSStreamNotFoundErr].ErrOrNew(err) + resp.Error = ApiErrors[JSStreamNotFoundErr].ErrOr(err) s.sendAPIErrResponse(ci, acc, subject, reply, string(msg), s.jsonResponse(&resp)) return } @@ -3501,7 +3501,7 @@ func (s *Server) jsConsumerDeleteRequest(sub *subscription, c *client, subject, return } if err := obs.delete(); err != nil { - resp.Error = ApiErrors[JSStreamGeneralErrorF].ErrOrNew(err) + resp.Error = ApiErrors[JSStreamGeneralErrorF].ErrOr(err) s.sendAPIErrResponse(ci, acc, subject, reply, string(msg), s.jsonResponse(&resp)) return } diff --git a/server/jetstream_cluster.go b/server/jetstream_cluster.go index 0392f2d8..f985625e 100644 --- a/server/jetstream_cluster.go +++ b/server/jetstream_cluster.go @@ -248,7 +248,7 @@ func (s *Server) JetStreamStepdownStream(account, stream string) error { func (s *Server) JetStreamSnapshotStream(account, stream string) error { js, cc := s.getJetStreamCluster() if js == nil { - return ApiErrors[JSNotEnabledErr] + return ApiErrors[JSNotEnabledForAccountErr] } if cc == nil { return ApiErrors[JSClusterNotActiveErr] @@ -267,7 +267,7 @@ func (s *Server) JetStreamSnapshotStream(account, stream string) error { mset.mu.RLock() if !mset.node.Leader() { mset.mu.RUnlock() - return ApiErrors[JSNotEnabledErr] + return ApiErrors[JSNotEnabledForAccountErr] } n := mset.node mset.mu.RUnlock() @@ -435,7 +435,7 @@ func (s *Server) enableJetStreamClustering() error { } js := s.getJetStream() if js == nil { - return ApiErrors[JSNotEnabledErr] + return ApiErrors[JSNotEnabledForAccountErr] } // Already set. if js.cluster != nil { @@ -3319,7 +3319,7 @@ func (s *Server) jsClusteredStreamRequest(ci *ClientInfo, acc *Account, subject, acc.mu.RUnlock() if jsa == nil { - resp.Error = ApiErrors[JSNotEnabledErr] + resp.Error = ApiErrors[JSNotEnabledForAccountErr] s.sendAPIErrResponse(ci, acc, subject, reply, string(rmsg), s.jsonResponse(&resp)) return } @@ -3421,7 +3421,7 @@ func (s *Server) jsClusteredStreamUpdateRequest(ci *ClientInfo, acc *Account, su newCfg = ncfg } } else { - resp.Error = ApiErrors[JSNotEnabledErr] + resp.Error = ApiErrors[JSNotEnabledForAccountErr] s.sendAPIErrResponse(ci, acc, subject, reply, string(rmsg), s.jsonResponse(&resp)) return } diff --git a/server/jetstream_errors.go b/server/jetstream_errors.go index a0568677..02439891 100644 --- a/server/jetstream_errors.go +++ b/server/jetstream_errors.go @@ -55,13 +55,13 @@ func (e *ApiError) ErrOrNewT(err error, replacements ...interface{}) *ApiError { return e.NewT(replacements...) } -// ErrOrNew returns err if it's an ApiError else creates a new error -func (e *ApiError) ErrOrNew(err error) *ApiError { +// ErrOr returns err if it's an ApiError else creates a new error +func (e *ApiError) ErrOr(err error) *ApiError { if ae, ok := err.(*ApiError); ok { return ae } - return e.NewT() + return e } // NewT creates a new error using strings.Replacer on the Description field, arguments must be an even number like NewT("{err}", err) diff --git a/server/jetstream_errors_generated.go b/server/jetstream_errors_generated.go index 892cbb6c..c901918f 100644 --- a/server/jetstream_errors_generated.go +++ b/server/jetstream_errors_generated.go @@ -108,8 +108,11 @@ const ( // JSNotEmptyRequestErr expected an empty request payload JSNotEmptyRequestErr ErrorIdentifier = 10038 - // JSNotEnabledErr JetStream not enabled for account - JSNotEnabledErr ErrorIdentifier = 10039 + // JSNotEnabledErr JetStream not enabled + JSNotEnabledErr ErrorIdentifier = 10076 + + // JSNotEnabledForAccountErr JetStream not enabled for account + JSNotEnabledForAccountErr ErrorIdentifier = 10039 // JSPeerRemapErr peer remap failed JSPeerRemapErr ErrorIdentifier = 10075 @@ -263,7 +266,8 @@ var ( JSNoAccountErr: {Code: 503, ErrCode: 10035, Description: "account not found"}, JSNoMessageFoundErr: {Code: 404, ErrCode: 10037, Description: "no message found"}, JSNotEmptyRequestErr: {Code: 400, ErrCode: 10038, Description: "expected an empty request payload"}, - JSNotEnabledErr: {Code: 503, ErrCode: 10039, Description: "JetStream not enabled for account", Help: "This error indicates that JetStream is not enabled either at a global level or at global and account level"}, + JSNotEnabledErr: {Code: 503, ErrCode: 10076, Description: "JetStream not enabled", Help: "This error indicates that JetStream is not enabled at a global level"}, + JSNotEnabledForAccountErr: {Code: 503, ErrCode: 10039, Description: "JetStream not enabled for account", Help: "This error indicates that JetStream is not enabled for an account account level"}, JSPeerRemapErr: {Code: 503, ErrCode: 10075, Description: "peer remap failed"}, JSRaftGeneralErrF: {Code: 500, ErrCode: 10041, Description: "{err}"}, JSRestoreSubscribeFailedErrF: {Code: 500, ErrCode: 10042, Description: "JetStream unable to subscribe to restore snapshot {subject}: {err}"}, @@ -303,4 +307,24 @@ var ( JSTempStorageFailedErr: {Code: 500, ErrCode: 10072, Description: "JetStream unable to open temp storage for restore"}, JSTemplateNameNotMatchSubjectErr: {Code: 400, ErrCode: 10073, Description: "template name in subject does not match request"}, } + // ErrJetStreamNotClustered Deprecated by JSClusterNotActiveErr ApiError, use IsNatsError() for comparisons + ErrJetStreamNotClustered = ApiErrors[JSClusterNotActiveErr] + // ErrJetStreamNotAssigned Deprecated by JSClusterNotAssignedErr ApiError, use IsNatsError() for comparisons + ErrJetStreamNotAssigned = ApiErrors[JSClusterNotAssignedErr] + // ErrJetStreamNotLeader Deprecated by JSClusterNotLeaderErr ApiError, use IsNatsError() for comparisons + ErrJetStreamNotLeader = ApiErrors[JSClusterNotLeaderErr] + // ErrJetStreamConsumerAlreadyUsed Deprecated by JSConsumerNameExistErr ApiError, use IsNatsError() for comparisons + ErrJetStreamConsumerAlreadyUsed = ApiErrors[JSConsumerNameExistErr] + // ErrJetStreamResourcesExceeded Deprecated by JSInsufficientResourcesErr ApiError, use IsNatsError() for comparisons + ErrJetStreamResourcesExceeded = ApiErrors[JSInsufficientResourcesErr] + // ErrMemoryResourcesExceeded Deprecated by JSMemoryResourcesExceededErr ApiError, use IsNatsError() for comparisons + ErrMemoryResourcesExceeded = ApiErrors[JSMemoryResourcesExceededErr] + // ErrStorageResourcesExceeded Deprecated by JSStorageResourcesExceededErr ApiError, use IsNatsError() for comparisons + ErrStorageResourcesExceeded = ApiErrors[JSStorageResourcesExceededErr] + // ErrJetStreamStreamAlreadyUsed Deprecated by JSStreamNameExistErr ApiError, use IsNatsError() for comparisons + ErrJetStreamStreamAlreadyUsed = ApiErrors[JSStreamNameExistErr] + // ErrJetStreamStreamNotFound Deprecated by JSStreamNotFoundErr ApiError, use IsNatsError() for comparisons + ErrJetStreamStreamNotFound = ApiErrors[JSStreamNotFoundErr] + // ErrReplicasNotSupported Deprecated by JSStreamReplicasNotSupportedErr ApiError, use IsNatsError() for comparisons + ErrReplicasNotSupported = ApiErrors[JSStreamReplicasNotSupportedErr] ) diff --git a/server/jetstream_errors_test.go b/server/jetstream_errors_test.go index 99cd9143..8171de92 100644 --- a/server/jetstream_errors_test.go +++ b/server/jetstream_errors_test.go @@ -8,23 +8,23 @@ import ( ) func TestIsNatsErr(t *testing.T) { - if !IsNatsErr(ApiErrors[JSNotEnabledErr], JSNotEnabledErr) { + if !IsNatsErr(ApiErrors[JSNotEnabledForAccountErr], JSNotEnabledForAccountErr) { t.Fatalf("Expected error match") } - if IsNatsErr(ApiErrors[JSNotEnabledErr], JSClusterNotActiveErr) { + if IsNatsErr(ApiErrors[JSNotEnabledForAccountErr], JSClusterNotActiveErr) { t.Fatalf("Expected error mismatch") } - if IsNatsErr(ApiErrors[JSNotEnabledErr], JSClusterNotActiveErr, JSClusterNotAvailErr) { + if IsNatsErr(ApiErrors[JSNotEnabledForAccountErr], JSClusterNotActiveErr, JSClusterNotAvailErr) { t.Fatalf("Expected error mismatch") } - if !IsNatsErr(ApiErrors[JSNotEnabledErr], JSClusterNotActiveErr, JSNotEnabledErr) { + if !IsNatsErr(ApiErrors[JSNotEnabledForAccountErr], JSClusterNotActiveErr, JSNotEnabledForAccountErr) { t.Fatalf("Expected error match") } - if !IsNatsErr(&ApiError{ErrCode: 10039}, 1, JSClusterNotActiveErr, JSNotEnabledErr) { + if !IsNatsErr(&ApiError{ErrCode: 10039}, 1, JSClusterNotActiveErr, JSNotEnabledForAccountErr) { t.Fatalf("Expected error match") } @@ -59,8 +59,8 @@ func TestApiError_NewF(t *testing.T) { } func TestApiError_ErrOrNewF(t *testing.T) { - if ne := ApiErrors[JSStreamRestoreErrF].ErrOrNewT(ApiErrors[JSNotEnabledErr], "{err}", errors.New("failed error")); !IsNatsErr(ne, JSNotEnabledErr) { - t.Fatalf("Expected JSNotEnabledErr got %s", ne) + if ne := ApiErrors[JSStreamRestoreErrF].ErrOrNewT(ApiErrors[JSNotEnabledForAccountErr], "{err}", errors.New("failed error")); !IsNatsErr(ne, JSNotEnabledForAccountErr) { + t.Fatalf("Expected JSNotEnabledForAccountErr got %s", ne) } if ne := ApiErrors[JSStreamRestoreErrF].ErrOrNewT(nil, "{err}", errors.New("failed error")); !IsNatsErr(ne, JSStreamRestoreErrF) { @@ -72,16 +72,16 @@ func TestApiError_ErrOrNewF(t *testing.T) { } } -func TestApiError_ErrOrNew(t *testing.T) { - if ne := ApiErrors[JSPeerRemapErr].ErrOrNew(ApiErrors[JSNotEnabledErr]); !IsNatsErr(ne, JSNotEnabledErr) { - t.Fatalf("Expected JSNotEnabledErr got %s", ne) +func TestApiError_ErrOr(t *testing.T) { + if ne := ApiErrors[JSPeerRemapErr].ErrOr(ApiErrors[JSNotEnabledForAccountErr]); !IsNatsErr(ne, JSNotEnabledForAccountErr) { + t.Fatalf("Expected JSNotEnabledForAccountErr got %s", ne) } - if ne := ApiErrors[JSPeerRemapErr].ErrOrNew(nil); !IsNatsErr(ne, JSPeerRemapErr) { + if ne := ApiErrors[JSPeerRemapErr].ErrOr(nil); !IsNatsErr(ne, JSPeerRemapErr) { t.Fatalf("Expected JSPeerRemapErr got %s", ne) } - if ne := ApiErrors[JSPeerRemapErr].ErrOrNew(errors.New("other error")); !IsNatsErr(ne, JSPeerRemapErr) { + if ne := ApiErrors[JSPeerRemapErr].ErrOr(errors.New("other error")); !IsNatsErr(ne, JSPeerRemapErr) { t.Fatalf("Expected JSPeerRemapErr got %s", ne) } } diff --git a/server/stream.go b/server/stream.go index 2f026ad2..c257ea49 100644 --- a/server/stream.go +++ b/server/stream.go @@ -2949,7 +2949,7 @@ func (mset *stream) stop(deleteFlag, advisory bool) error { mset.mu.RUnlock() if jsa == nil { - return ApiErrors[JSNotEnabledErr] + return ApiErrors[JSNotEnabledForAccountErr] } // Remove from our account map.