nats
Version:
Node.js client for NATS, a lightweight, high-performance cloud native messaging system
121 lines • 4.68 kB
JavaScript
;
/*
* Copyright 2018-2021 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.
*/
Object.defineProperty(exports, "__esModule", { value: true });
exports.NatsError = exports.isNatsError = exports.Messages = exports.ErrorCode = void 0;
var ErrorCode;
(function (ErrorCode) {
// emitted by the client
ErrorCode["ApiError"] = "BAD API";
ErrorCode["BadAuthentication"] = "BAD_AUTHENTICATION";
ErrorCode["BadCreds"] = "BAD_CREDS";
ErrorCode["BadHeader"] = "BAD_HEADER";
ErrorCode["BadJson"] = "BAD_JSON";
ErrorCode["BadPayload"] = "BAD_PAYLOAD";
ErrorCode["BadSubject"] = "BAD_SUBJECT";
ErrorCode["Cancelled"] = "CANCELLED";
ErrorCode["ConnectionClosed"] = "CONNECTION_CLOSED";
ErrorCode["ConnectionDraining"] = "CONNECTION_DRAINING";
ErrorCode["ConnectionRefused"] = "CONNECTION_REFUSED";
ErrorCode["ConnectionTimeout"] = "CONNECTION_TIMEOUT";
ErrorCode["Disconnect"] = "DISCONNECT";
ErrorCode["InvalidOption"] = "INVALID_OPTION";
ErrorCode["InvalidPayload"] = "INVALID_PAYLOAD";
ErrorCode["MaxPayloadExceeded"] = "MAX_PAYLOAD_EXCEEDED";
ErrorCode["NoResponders"] = "503";
ErrorCode["NotFunction"] = "NOT_FUNC";
ErrorCode["RequestError"] = "REQUEST_ERROR";
ErrorCode["ServerOptionNotAvailable"] = "SERVER_OPT_NA";
ErrorCode["SubClosed"] = "SUB_CLOSED";
ErrorCode["SubDraining"] = "SUB_DRAINING";
ErrorCode["Timeout"] = "TIMEOUT";
ErrorCode["Tls"] = "TLS";
ErrorCode["Unknown"] = "UNKNOWN_ERROR";
ErrorCode["WssRequired"] = "WSS_REQUIRED";
// jetstream
ErrorCode["JetStreamInvalidAck"] = "JESTREAM_INVALID_ACK";
ErrorCode["JetStream404NoMessages"] = "404";
ErrorCode["JetStream408RequestTimeout"] = "408";
//@deprecated: use JetStream409
ErrorCode["JetStream409MaxAckPendingExceeded"] = "409";
ErrorCode["JetStream409"] = "409";
ErrorCode["JetStreamNotEnabled"] = "503";
ErrorCode["JetStreamIdleHeartBeat"] = "IDLE_HEARTBEAT";
// emitted by the server
ErrorCode["AuthorizationViolation"] = "AUTHORIZATION_VIOLATION";
ErrorCode["AuthenticationExpired"] = "AUTHENTICATION_EXPIRED";
ErrorCode["ProtocolError"] = "NATS_PROTOCOL_ERR";
ErrorCode["PermissionsViolation"] = "PERMISSIONS_VIOLATION";
})(ErrorCode = exports.ErrorCode || (exports.ErrorCode = {}));
class Messages {
constructor() {
this.messages = new Map();
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) {
return messages.getMessage(s);
}
getMessage(s) {
return this.messages.get(s) || s;
}
}
exports.Messages = Messages;
// safari doesn't support static class members
const messages = new Messages();
function isNatsError(err) {
return typeof err.code === "string";
}
exports.isNatsError = isNatsError;
class NatsError extends Error {
/**
* @param {String} message
* @param {String} code
* @param {Error} [chainedError]
* @constructor
*
* @api private
*/
constructor(message, code, chainedError) {
super(message);
this.name = "NatsError";
this.message = message;
this.code = code;
this.chainedError = chainedError;
}
static errorForCode(code, chainedError) {
const m = Messages.getMessage(code);
return new NatsError(m, code, chainedError);
}
isAuthError() {
return this.code === ErrorCode.AuthenticationExpired ||
this.code === ErrorCode.AuthorizationViolation;
}
isPermissionError() {
return this.code === ErrorCode.PermissionsViolation;
}
isProtocolError() {
return this.code === ErrorCode.ProtocolError;
}
isJetStreamError() {
return this.api_error !== undefined;
}
jsError() {
return this.api_error ? this.api_error : null;
}
}
exports.NatsError = NatsError;
//# sourceMappingURL=error.js.map