-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
wip - removing NatsError in favor of easier more generic APIs and tes…
…ting with instanceof. Signed-off-by: Alberto Ricart <[email protected]>
- Loading branch information
Showing
52 changed files
with
1,254 additions
and
1,043 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,195 @@ | ||
/* | ||
* Copyright 2024 Synadia Communications, Inc | ||
* 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. | ||
*/ | ||
|
||
export class InvalidSubjectError extends Error { | ||
constructor(subject: string, options?: ErrorOptions) { | ||
super(`illegal subject: '${subject}'`, options); | ||
this.name = "InvalidSubjectError"; | ||
} | ||
} | ||
|
||
export class InvalidHeaderError extends Error { | ||
constructor(message: string, options?: ErrorOptions) { | ||
super(`invalid header: ${message}`, options); | ||
this.name = "InvalidHeaderError"; | ||
} | ||
} | ||
|
||
export class InvalidOptionError extends Error { | ||
constructor(message: string, options?: ErrorOptions) { | ||
super(message, options); | ||
this.name = "InvalidOptionError"; | ||
} | ||
|
||
static exclusiveOptions(opts: string[]): InvalidOptionError { | ||
const names = opts.map((o) => `'${o}'`).join(","); | ||
return new InvalidOptionError(`options ${names} are mutually exclusive.`); | ||
} | ||
|
||
static illegalArgument(name: string, message: string): InvalidOptionError { | ||
return new InvalidOptionError(`argument '${name}' ${message}`); | ||
} | ||
|
||
static illegalOption(prop: string, message: string): InvalidOptionError { | ||
return new InvalidOptionError(`option '${prop}' ${message}`); | ||
} | ||
} | ||
|
||
export class InvalidOperationError extends Error { | ||
constructor(message: string, options?: ErrorOptions) { | ||
super(message, options); | ||
this.name = "InvalidOperationError"; | ||
} | ||
} | ||
|
||
export class UserAuthenticationExpiredError extends Error { | ||
constructor(message: string, options?: ErrorOptions) { | ||
super(message, options); | ||
this.name = "UserAuthenticationExpiredError"; | ||
} | ||
|
||
static parse(s: string): UserAuthenticationExpiredError | null { | ||
const ss = s.toLowerCase(); | ||
if (ss.indexOf("user authentication expired") !== -1) { | ||
return new UserAuthenticationExpiredError(s); | ||
} | ||
return null; | ||
} | ||
} | ||
|
||
export class AuthorizationError extends Error { | ||
constructor(message: string, options?: ErrorOptions) { | ||
super(message, options); | ||
this.name = "AuthorizationError"; | ||
} | ||
|
||
static parse(s: string): AuthorizationError | null { | ||
const messages = [ | ||
"authorization violation", | ||
"account authentication expired", | ||
"authentication timeout", | ||
]; | ||
|
||
const ss = s.toLowerCase(); | ||
|
||
for (let i = 0; i < messages.length; i++) { | ||
if (ss.indexOf(messages[i]) !== -1) { | ||
return new AuthorizationError(s); | ||
} | ||
} | ||
|
||
return null; | ||
} | ||
} | ||
|
||
export class ClosedConnectionError extends Error { | ||
constructor() { | ||
super("closed connection"); | ||
this.name = "ClosedConnectionError"; | ||
} | ||
} | ||
|
||
export class ConnectionDrainingError extends Error { | ||
constructor() { | ||
super("connection draining"); | ||
this.name = "DrainingConnectionError"; | ||
} | ||
} | ||
|
||
export class ConnectionError extends Error { | ||
constructor(message: string, options?: ErrorOptions) { | ||
super(message, options); | ||
this.name = "ConnectionError"; | ||
} | ||
} | ||
|
||
export class ProtocolError extends Error { | ||
constructor(message: string, options?: ErrorOptions) { | ||
super(message, options); | ||
this.name = "ProtocolError"; | ||
} | ||
} | ||
|
||
export class RequestError extends Error { | ||
constructor(message = "", options?: ErrorOptions) { | ||
super(message, options); | ||
this.name = "RequestError"; | ||
} | ||
} | ||
|
||
export class TimeoutError extends Error { | ||
constructor(options?: ErrorOptions) { | ||
super("timeout", options); | ||
this.name = "TimeoutError"; | ||
} | ||
} | ||
|
||
export class NoRespondersError extends Error { | ||
subject: string; | ||
constructor(subject: string, options?: ErrorOptions) { | ||
super(`no responders: '${subject}'`, options); | ||
this.subject = subject; | ||
this.name = "NoResponders"; | ||
} | ||
} | ||
|
||
export class ServerError extends Error { | ||
constructor(message: string, options?: ErrorOptions) { | ||
super(message, options); | ||
this.name = "ServerError"; | ||
} | ||
} | ||
|
||
export class PermissionViolationError extends Error { | ||
operation: "publish" | "subscription"; | ||
subject: string; | ||
queue?: string; | ||
|
||
constructor( | ||
message: string, | ||
operation: "publish" | "subscription", | ||
subject: string, | ||
queue?: string, | ||
options?: ErrorOptions, | ||
) { | ||
super(message, options); | ||
this.name = "PermissionViolationError"; | ||
this.operation = operation; | ||
this.subject = subject; | ||
this.queue = queue; | ||
} | ||
|
||
static parse(s: string): PermissionViolationError | null { | ||
const t = s ? s.toLowerCase() : ""; | ||
if (t.indexOf("permissions violation") === -1) { | ||
return null; | ||
} | ||
let operation: "publish" | "subscription" = "publish"; | ||
let subject = ""; | ||
let queue: string | undefined = undefined; | ||
const m = s.match(/(Publish|Subscription) to "(\S+)"/); | ||
if (m) { | ||
operation = m[1].toLowerCase() as "publish" | "subscription"; | ||
subject = m[2]; | ||
if (operation === "subscription") { | ||
const qm = s.match(/using queue "(\S+)"/); | ||
if (qm) { | ||
queue = qm[1]; | ||
} | ||
} | ||
} | ||
return new PermissionViolationError(s, operation, subject, queue); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.