- Notifications
You must be signed in to change notification settings - Fork 1.5k
/
Copy patherrors.go
396 lines (305 loc) · 16.1 KB
/
errors.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
// Copyright 2012-2024 The NATS Authors
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package server
import (
"errors"
"fmt"
)
var (
// ErrConnectionClosed represents an error condition on a closed connection.
ErrConnectionClosed=errors.New("connection closed")
// ErrAuthentication represents an error condition on failed authentication.
ErrAuthentication=errors.New("authentication error")
// ErrAuthTimeout represents an error condition on failed authorization due to timeout.
ErrAuthTimeout=errors.New("authentication timeout")
// ErrAuthExpired represents an expired authorization due to timeout.
ErrAuthExpired=errors.New("authentication expired")
// ErrMaxPayload represents an error condition when the payload is too big.
ErrMaxPayload=errors.New("maximum payload exceeded")
// ErrMaxControlLine represents an error condition when the control line is too big.
ErrMaxControlLine=errors.New("maximum control line exceeded")
// ErrReservedPublishSubject represents an error condition when sending to a reserved subject, e.g. _SYS.>
ErrReservedPublishSubject=errors.New("reserved internal subject")
// ErrBadPublishSubject represents an error condition for an invalid publish subject.
ErrBadPublishSubject=errors.New("invalid publish subject")
// ErrBadSubject represents an error condition for an invalid subject.
ErrBadSubject=errors.New("invalid subject")
// ErrBadQualifier is used to error on a bad qualifier for a transform.
ErrBadQualifier=errors.New("bad qualifier")
// ErrBadClientProtocol signals a client requested an invalid client protocol.
ErrBadClientProtocol=errors.New("invalid client protocol")
// ErrTooManyConnections signals a client that the maximum number of connections supported by the
// server has been reached.
ErrTooManyConnections=errors.New("maximum connections exceeded")
// ErrTooManyAccountConnections signals that an account has reached its maximum number of active
// connections.
ErrTooManyAccountConnections=errors.New("maximum account active connections exceeded")
// ErrLeafNodeLoop signals a leafnode is trying to register for a cluster we already have registered.
ErrLeafNodeLoop=errors.New("leafnode loop detected")
// ErrTooManySubs signals a client that the maximum number of subscriptions per connection
// has been reached.
ErrTooManySubs=errors.New("maximum subscriptions exceeded")
// ErrTooManySubTokens signals a client that the subject has too many tokens.
ErrTooManySubTokens=errors.New("subject has exceeded number of tokens limit")
// ErrClientConnectedToRoutePort represents an error condition when a client
// attempted to connect to the route listen port.
ErrClientConnectedToRoutePort=errors.New("attempted to connect to route port")
// ErrClientConnectedToLeafNodePort represents an error condition when a client
// attempted to connect to the leaf node listen port.
ErrClientConnectedToLeafNodePort=errors.New("attempted to connect to leaf node port")
// ErrLeafNodeHasSameClusterName represents an error condition when a leafnode is a cluster
// and it has the same cluster name as the hub cluster.
ErrLeafNodeHasSameClusterName=errors.New("remote leafnode has same cluster name")
// ErrLeafNodeDisabled is when we disable leafnodes.
ErrLeafNodeDisabled=errors.New("leafnodes disabled")
// ErrConnectedToWrongPort represents an error condition when a connection is attempted
// to the wrong listen port (for instance a LeafNode to a client port, etc...)
ErrConnectedToWrongPort=errors.New("attempted to connect to wrong port")
// ErrAccountExists is returned when an account is attempted to be registered
// but already exists.
ErrAccountExists=errors.New("account exists")
// ErrBadAccount represents a malformed or incorrect account.
ErrBadAccount=errors.New("bad account")
// ErrReservedAccount represents a reserved account that can not be created.
ErrReservedAccount=errors.New("reserved account")
// ErrMissingAccount is returned when an account does not exist.
ErrMissingAccount=errors.New("account missing")
// ErrMissingService is returned when an account does not have an exported service.
ErrMissingService=errors.New("service missing")
// ErrBadServiceType is returned when latency tracking is being applied to non-singleton response types.
ErrBadServiceType=errors.New("bad service response type")
// ErrBadSampling is returned when the sampling for latency tracking is not 1 >= sample <= 100.
ErrBadSampling=errors.New("bad sampling percentage, should be 1-100")
// ErrAccountValidation is returned when an account has failed validation.
ErrAccountValidation=errors.New("account validation failed")
// ErrAccountExpired is returned when an account has expired.
ErrAccountExpired=errors.New("account expired")
// ErrNoAccountResolver is returned when we attempt an update but do not have an account resolver.
ErrNoAccountResolver=errors.New("account resolver missing")
// ErrAccountResolverUpdateTooSoon is returned when we attempt an update too soon to last request.
ErrAccountResolverUpdateTooSoon=errors.New("account resolver update too soon")
// ErrAccountResolverSameClaims is returned when same claims have been fetched.
ErrAccountResolverSameClaims=errors.New("account resolver no new claims")
// ErrStreamImportAuthorization is returned when a stream import is not authorized.
ErrStreamImportAuthorization=errors.New("stream import not authorized")
// ErrStreamImportBadPrefix is returned when a stream import prefix contains wildcards.
ErrStreamImportBadPrefix=errors.New("stream import prefix can not contain wildcard tokens")
// ErrStreamImportDuplicate is returned when a stream import is a duplicate of one that already exists.
ErrStreamImportDuplicate=errors.New("stream import already exists")
// ErrServiceImportAuthorization is returned when a service import is not authorized.
ErrServiceImportAuthorization=errors.New("service import not authorized")
// ErrImportFormsCycle is returned when an import would form a cycle.
ErrImportFormsCycle=errors.New("import forms a cycle")
// ErrCycleSearchDepth is returned when we have exceeded our maximum search depth..
ErrCycleSearchDepth=errors.New("search cycle depth exhausted")
// ErrClientOrRouteConnectedToGatewayPort represents an error condition when
// a client or route attempted to connect to the Gateway port.
ErrClientOrRouteConnectedToGatewayPort=errors.New("attempted to connect to gateway port")
// ErrWrongGateway represents an error condition when a server receives a connect
// request from a remote Gateway with a destination name that does not match the server's
// Gateway's name.
ErrWrongGateway=errors.New("wrong gateway")
// ErrGatewayNameHasSpaces signals that the gateway name contains spaces, which is not allowed.
ErrGatewayNameHasSpaces=errors.New("gateway name cannot contain spaces")
// ErrNoSysAccount is returned when an attempt to publish or subscribe is made
// when there is no internal system account defined.
ErrNoSysAccount=errors.New("system account not setup")
// ErrRevocation is returned when a credential has been revoked.
ErrRevocation=errors.New("credentials have been revoked")
// ErrServerNotRunning is used to signal an error that a server is not running.
ErrServerNotRunning=errors.New("server is not running")
// ErrServerNameHasSpaces signals that the server name contains spaces, which is not allowed.
ErrServerNameHasSpaces=errors.New("server name cannot contain spaces")
// ErrBadMsgHeader signals the parser detected a bad message header
ErrBadMsgHeader=errors.New("bad message header detected")
// ErrMsgHeadersNotSupported signals the parser detected a message header
// but they are not supported on this server.
ErrMsgHeadersNotSupported=errors.New("message headers not supported")
// ErrNoRespondersRequiresHeaders signals that a client needs to have headers
// on if they want no responders behavior.
ErrNoRespondersRequiresHeaders=errors.New("no responders requires headers support")
// ErrClusterNameConfigConflict signals that the options for cluster name in cluster and gateway are in conflict.
ErrClusterNameConfigConflict=errors.New("cluster name conflicts between cluster and gateway definitions")
// ErrClusterNameRemoteConflict signals that a remote server has a different cluster name.
ErrClusterNameRemoteConflict=errors.New("cluster name from remote server conflicts")
// ErrClusterNameHasSpaces signals that the cluster name contains spaces, which is not allowed.
ErrClusterNameHasSpaces=errors.New("cluster name cannot contain spaces")
// ErrMalformedSubject is returned when a subscription is made with a subject that does not conform to subject rules.
ErrMalformedSubject=errors.New("malformed subject")
// ErrSubscribePermissionViolation is returned when processing of a subscription fails due to permissions.
ErrSubscribePermissionViolation=errors.New("subscribe permission violation")
// ErrNoTransforms signals no subject transforms are available to map this subject.
ErrNoTransforms=errors.New("no matching transforms available")
// ErrCertNotPinned is returned when pinned certs are set and the certificate is not in it
ErrCertNotPinned=errors.New("certificate not pinned")
// ErrDuplicateServerName is returned when processing a server remote connection and
// the server reports that this server name is already used in the cluster.
ErrDuplicateServerName=errors.New("duplicate server name")
// ErrMinimumVersionRequired is returned when a connection is not at the minimum version required.
ErrMinimumVersionRequired=errors.New("minimum version required")
// ErrInvalidMappingDestination is used for all subject mapping destination errors
ErrInvalidMappingDestination=errors.New("invalid mapping destination")
// ErrInvalidMappingDestinationSubject is used to error on a bad transform destination mapping
ErrInvalidMappingDestinationSubject=fmt.Errorf("%w: invalid transform", ErrInvalidMappingDestination)
// ErrMappingDestinationNotUsingAllWildcards is used to error on a transform destination not using all of the token wildcards
ErrMappingDestinationNotUsingAllWildcards=fmt.Errorf("%w: not using all of the token wildcard(s)", ErrInvalidMappingDestination)
// ErrUnknownMappingDestinationFunction is returned when a subject mapping destination contains an unknown mustache-escaped mapping function.
ErrUnknownMappingDestinationFunction=fmt.Errorf("%w: unknown function", ErrInvalidMappingDestination)
// ErrMappingDestinationIndexOutOfRange is returned when the mapping destination function is passed an out of range wildcard index value for one of it's arguments
ErrMappingDestinationIndexOutOfRange=fmt.Errorf("%w: wildcard index out of range", ErrInvalidMappingDestination)
// ErrMappingDestinationNotEnoughArgs is returned when the mapping destination function is not passed enough arguments
ErrMappingDestinationNotEnoughArgs=fmt.Errorf("%w: not enough arguments passed to the function", ErrInvalidMappingDestination)
// ErrMappingDestinationInvalidArg is returned when the mapping destination function is passed and invalid argument
ErrMappingDestinationInvalidArg=fmt.Errorf("%w: function argument is invalid or in the wrong format", ErrInvalidMappingDestination)
// ErrMappingDestinationTooManyArgs is returned when the mapping destination function is passed too many arguments
ErrMappingDestinationTooManyArgs=fmt.Errorf("%w: too many arguments passed to the function", ErrInvalidMappingDestination)
// ErrMappingDestinationNotSupportedForImport is returned when you try to use a mapping function other than wildcard in a transform that needs to be reversible (i.e. an import)
ErrMappingDestinationNotSupportedForImport=fmt.Errorf("%w: the only mapping function allowed for import transforms is {{Wildcard()}}", ErrInvalidMappingDestination)
)
// mappingDestinationErr is a type of subject mapping destination error
typemappingDestinationErrstruct {
tokenstring
errerror
}
func (e*mappingDestinationErr) Error() string {
returnfmt.Sprintf("%s in %s", e.err, e.token)
}
func (e*mappingDestinationErr) Is(targeterror) bool {
returntarget==ErrInvalidMappingDestination
}
// configErr is a configuration error.
typeconfigErrstruct {
tokentoken
reasonstring
}
// Source reports the location of a configuration error.
func (e*configErr) Source() string {
returnfmt.Sprintf("%s:%d:%d", e.token.SourceFile(), e.token.Line(), e.token.Position())
}
// Error reports the location and reason from a configuration error.
func (e*configErr) Error() string {
ife.token!=nil {
returnfmt.Sprintf("%s: %s", e.Source(), e.reason)
}
returne.reason
}
// unknownConfigFieldErr is an error reported in pedantic mode.
typeunknownConfigFieldErrstruct {
configErr
fieldstring
}
// Error reports that an unknown field was in the configuration.
func (e*unknownConfigFieldErr) Error() string {
returnfmt.Sprintf("%s: unknown field %q", e.Source(), e.field)
}
// configWarningErr is an error reported in pedantic mode.
typeconfigWarningErrstruct {
configErr
fieldstring
}
// Error reports a configuration warning.
func (e*configWarningErr) Error() string {
returnfmt.Sprintf("%s: invalid use of field %q: %s", e.Source(), e.field, e.reason)
}
// processConfigErr is the result of processing the configuration from the server.
typeprocessConfigErrstruct {
errors []error
warnings []error
}
// Error returns the collection of errors separated by new lines,
// warnings appear first then hard errors.
func (e*processConfigErr) Error() string {
varmsgstring
for_, err:=rangee.Warnings() {
msg+=err.Error() +"\n"
}
for_, err:=rangee.Errors() {
msg+=err.Error() +"\n"
}
returnmsg
}
// Warnings returns the list of warnings.
func (e*processConfigErr) Warnings() []error {
returne.warnings
}
// Errors returns the list of errors.
func (e*processConfigErr) Errors() []error {
returne.errors
}
// errCtx wraps an error and stores additional ctx information for tracing.
// Does not print or return it unless explicitly requested.
typeerrCtxstruct {
error
ctxstring
}
funcNewErrorCtx(errerror, formatstring, args...any) error {
return&errCtx{err, fmt.Sprintf(format, args...)}
}
// Unwrap implement to work with errors.Is and errors.As
func (e*errCtx) Unwrap() error {
ife==nil {
returnnil
}
returne.error
}
// Context for error
func (e*errCtx) Context() string {
ife==nil {
return""
}
returne.ctx
}
// UnpackIfErrorCtx return Error or, if type is right error and context
funcUnpackIfErrorCtx(errerror) string {
ife, ok:=err.(*errCtx); ok {
if_, ok:=e.error.(*errCtx); ok {
returnfmt.Sprint(UnpackIfErrorCtx(e.error), ": ", e.Context())
}
returnfmt.Sprint(e.Error(), ": ", e.Context())
}
returnerr.Error()
}
// implements: go 1.13 errors.Unwrap(err error) error
// TODO replace with native code once we no longer support go1.12
funcerrorsUnwrap(errerror) error {
u, ok:=err.(interface {
Unwrap() error
})
if!ok {
returnnil
}
returnu.Unwrap()
}
// ErrorIs implements: go 1.13 errors.Is(err, target error) bool
// TODO replace with native code once we no longer support go1.12
funcErrorIs(err, targeterror) bool {
// this is an outright copy of go 1.13 errors.Is(err, target error) bool
// removed isComparable
iferr==nil||target==nil {
returnerr==target
}
for {
iferr==target {
returntrue
}
ifx, ok:=err.(interface{ Is(error) bool }); ok&&x.Is(target) {
returntrue
}
// TODO: consider supporing target.Is(err). This would allow
// user-definable predicates, but also may allow for coping with sloppy
// APIs, thereby making it easier to get away with them.
iferr=errorsUnwrap(err); err==nil {
returnfalse
}
}
}