Skip to content

Commit

Permalink
wip - removing NatsError in favor of easier more generic APIs and tes…
Browse files Browse the repository at this point in the history
…ting with instanceof.

Signed-off-by: Alberto Ricart <[email protected]>
  • Loading branch information
aricart committed Oct 28, 2024
1 parent 2ac7ea9 commit 728cc56
Show file tree
Hide file tree
Showing 44 changed files with 393 additions and 505 deletions.
2 changes: 1 addition & 1 deletion core/deno.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@nats-io/nats-core",
"version": "3.0.0-30",
"version": "3.0.0-31",
"exports": {
".": "./src/mod.ts",
"./internal": "./src/internal_mod.ts"
Expand Down
2 changes: 1 addition & 1 deletion core/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@nats-io/nats-core",
"version": "3.0.0-30",
"version": "3.0.0-31",
"files": [
"lib/",
"LICENSE",
Expand Down
150 changes: 0 additions & 150 deletions core/src/core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,156 +44,6 @@ export enum DebugEvents {
ClientInitiatedReconnect = "client initiated reconnect",
}

export enum ErrorCode {
// emitted by the client
// ApiError = "BAD API",
BadAuthentication = "BAD_AUTHENTICATION",
BadCreds = "BAD_CREDS",
BadHeader = "BAD_HEADER",
BadJson = "BAD_JSON",
BadPayload = "BAD_PAYLOAD",
BadSubject = "BAD_SUBJECT",
Cancelled = "CANCELLED",
ConnectionClosed = "CONNECTION_CLOSED",
ConnectionDraining = "CONNECTION_DRAINING",
ConnectionRefused = "CONNECTION_REFUSED",
ConnectionTimeout = "CONNECTION_TIMEOUT",
Disconnect = "DISCONNECT",
InvalidOption = "INVALID_OPTION",
InvalidPayload = "INVALID_PAYLOAD",
MaxPayloadExceeded = "MAX_PAYLOAD_EXCEEDED",
NoResponders = "503",
NotFunction = "NOT_FUNC",
RequestError = "REQUEST_ERROR",
ServerOptionNotAvailable = "SERVER_OPT_NA",
SubClosed = "SUB_CLOSED",
SubDraining = "SUB_DRAINING",
Timeout = "TIMEOUT",
Tls = "TLS",
Unknown = "UNKNOWN_ERROR",
WssRequired = "WSS_REQUIRED",

// jetstream
JetStreamInvalidAck = "JESTREAM_INVALID_ACK",
JetStream404NoMessages = "404",
JetStream408RequestTimeout = "408",
//@deprecated: use JetStream409
JetStream409MaxAckPendingExceeded = "409",
JetStream409 = "409",
JetStreamNotEnabled = "503",
JetStreamIdleHeartBeat = "IDLE_HEARTBEAT",

// emitted by the server
AuthorizationViolation = "AUTHORIZATION_VIOLATION",
AuthenticationExpired = "AUTHENTICATION_EXPIRED",
ProtocolError = "NATS_PROTOCOL_ERR",
PermissionsViolation = "PERMISSIONS_VIOLATION",
AuthenticationTimeout = "AUTHENTICATION_TIMEOUT",
AccountExpired = "ACCOUNT_EXPIRED",
}

export function isNatsError(err: NatsError | Error): err is NatsError {
return typeof (err as NatsError).code === "string";
}

export class Messages {
messages: Map<string, string>;

constructor() {
this.messages = new Map<string, string>();
this.messages.set(
ErrorCode.InvalidPayload,
"Invalid payload type - payloads can be 'binary', 'string', or 'json'",
);
this.messages.set(ErrorCode.BadJson, "Bad JSON");
this.messages.set(
ErrorCode.WssRequired,
"TLS is required, therefore a secure websocket connection is also required",
);
}

static getMessage(s: string): string {
return messages.getMessage(s);
}

getMessage(s: string): string {
return this.messages.get(s) || s;
}
}

// safari doesn't support static class members
const messages: Messages = new Messages();

export type ApiError = {
/**
* Status code
*/
code: number;
/**
* JetStream Error Code
*/
err_code: number;
/**
* Error description
*/
description: string;
};

export class NatsError extends Error {
// TODO: on major version this should change to a number/enum
code: string;
permissionContext?: { operation: string; subject: string; queue?: string };
chainedError?: Error;
// these are for supporting jetstream
api_error?: ApiError;

/**
* @param {String} message
* @param {String} code
* @param {Error} [chainedError]
*
* @api private
*/
constructor(message: string, code: string, chainedError?: Error) {
super(message);
this.name = "NatsError";
this.message = message;
this.code = code;
this.chainedError = chainedError;
}

static errorForCode(code: string, chainedError?: Error): NatsError {
const m = Messages.getMessage(code);
return new NatsError(m, code, chainedError);
}

isAuthError(): boolean {
return this.code === ErrorCode.AuthenticationExpired ||
this.code === ErrorCode.AuthorizationViolation ||
this.code === ErrorCode.AccountExpired;
}

isAuthTimeout(): boolean {
return this.code === ErrorCode.AuthenticationTimeout;
}

isPermissionError(): boolean {
return this.code === ErrorCode.PermissionsViolation;
}

isProtocolError(): boolean {
return this.code === ErrorCode.ProtocolError;
}

isJetStreamError(): boolean {
return this.api_error !== undefined;
}

jsError(): ApiError | null {
return this.api_error ? this.api_error : null;
}
}

export type MsgCallback<T> = (err: Error | null, msg: T) => void;

/**
Expand Down
4 changes: 0 additions & 4 deletions core/src/internal_mod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,6 @@ export { Empty } from "./types.ts";
export { extractProtocolMessage, protoLen } from "./transport.ts";

export type {
ApiError,
Auth,
Authenticator,
CallbackFn,
Expand Down Expand Up @@ -119,11 +118,8 @@ export type {
export {
createInbox,
DebugEvents,
ErrorCode,
Events,
isNatsError,
Match,
NatsError,
RequestStrategy,
syncIterator,
} from "./core.ts";
Expand Down
3 changes: 0 additions & 3 deletions core/src/mod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ export {
deferred,
delay,
Empty,
ErrorCode,
Events,
headers,
InvalidOptionError,
Expand All @@ -39,7 +38,6 @@ export {
millis,
MsgHdrsImpl,
nanos,
NatsError,
nkeyAuthenticator,
nkeys,
NoRespondersError,
Expand All @@ -59,7 +57,6 @@ export {
} from "./internal_mod.ts";

export type {
ApiError,
Auth,
Authenticator,
Backoff,
Expand Down
3 changes: 1 addition & 2 deletions core/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
export type { ApiError, Dispatcher, MsgHdrs, QueuedIterator } from "./core.ts";
export { NatsError } from "./core.ts";
export type { Dispatcher, MsgHdrs, QueuedIterator } from "./core.ts";

export { Empty } from "./encoders.ts";
17 changes: 16 additions & 1 deletion core/src/version.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,17 @@
/*
* 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.
*/

// This file is generated - do not edit
export const version = "3.0.0-30";
export const version = "3.0.0-31";
4 changes: 2 additions & 2 deletions jetstream/deno.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@nats-io/jetstream",
"version": "3.0.0-15",
"version": "3.0.0-18",
"exports": {
".": "./src/mod.ts",
"./internal": "./src/internal_mod.ts"
Expand Down Expand Up @@ -33,6 +33,6 @@
"test": "deno test -A --parallel --reload --trace-leaks --quiet tests/ --import-map=import_map.json"
},
"imports": {
"@nats-io/nats-core": "jsr:@nats-io/nats-core@~3.0.0-30"
"@nats-io/nats-core": "jsr:@nats-io/nats-core@~3.0.0-31"
}
}
4 changes: 2 additions & 2 deletions jetstream/import_map.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
"imports": {
"@nats-io/nkeys": "jsr:@nats-io/[email protected]",
"@nats-io/nuid": "jsr:@nats-io/[email protected]",
"@nats-io/nats-core": "jsr:@nats-io/nats-core@~3.0.0-30",
"@nats-io/nats-core/internal": "jsr:@nats-io/nats-core@~3.0.0-30/internal",
"@nats-io/nats-core": "jsr:@nats-io/nats-core@~3.0.0-31",
"@nats-io/nats-core/internal": "jsr:@nats-io/nats-core@~3.0.0-31/internal",
"test_helpers": "../test_helpers/mod.ts",
"@std/io": "jsr:@std/[email protected]"
}
Expand Down
4 changes: 2 additions & 2 deletions jetstream/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@nats-io/jetstream",
"version": "3.0.0-15",
"version": "3.0.0-18",
"files": [
"lib/",
"LICENSE",
Expand Down Expand Up @@ -34,7 +34,7 @@
},
"description": "jetstream library - this library implements all the base functionality for NATS JetStream for javascript clients",
"dependencies": {
"@nats-io/nats-core": "~3.0.0-30"
"@nats-io/nats-core": "~3.0.0-31"
},
"devDependencies": {
"@types/node": "^22.7.6",
Expand Down
Loading

0 comments on commit 728cc56

Please sign in to comment.