mirror of
https://github.com/swc-project/swc.git
synced 2024-12-21 12:41:54 +03:00
123 lines
3.5 KiB
TypeScript
123 lines
3.5 KiB
TypeScript
|
// Loaded from https://raw.githubusercontent.com/nats-io/nats.deno/v1.0.0-rc4/nats-base-client/error.ts
|
||
|
|
||
|
|
||
|
/*
|
||
|
* 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.
|
||
|
*/
|
||
|
|
||
|
export enum ErrorCode {
|
||
|
// emitted by the client
|
||
|
API_ERROR = "BAD API",
|
||
|
BAD_AUTHENTICATION = "BAD_AUTHENTICATION",
|
||
|
BAD_CREDS = "BAD_CREDS",
|
||
|
BAD_HEADER = "BAD_HEADER",
|
||
|
BAD_JSON = "BAD_JSON",
|
||
|
BAD_PAYLOAD = "BAD_PAYLOAD",
|
||
|
BAD_SUBJECT = "BAD_SUBJECT",
|
||
|
CANCELLED = "CANCELLED",
|
||
|
CONNECTION_CLOSED = "CONNECTION_CLOSED",
|
||
|
CONNECTION_DRAINING = "CONNECTION_DRAINING",
|
||
|
CONNECTION_REFUSED = "CONNECTION_REFUSED",
|
||
|
CONNECTION_TIMEOUT = "CONNECTION_TIMEOUT",
|
||
|
DISCONNECT = "DISCONNECT",
|
||
|
INVALID_OPTION = "INVALID_OPTION",
|
||
|
INVALID_PAYLOAD_TYPE = "INVALID_PAYLOAD",
|
||
|
MAX_PAYLOAD_EXCEEDED = "MAX_PAYLOAD_EXCEEDED",
|
||
|
NO_RESPONDERS = "NO_RESPONDERS",
|
||
|
NOT_FUNC = "NOT_FUNC",
|
||
|
REQUEST_ERROR = "REQUEST_ERROR",
|
||
|
SERVER_OPTION_NA = "SERVER_OPT_NA",
|
||
|
SUB_CLOSED = "SUB_CLOSED",
|
||
|
SUB_DRAINING = "SUB_DRAINING",
|
||
|
TIMEOUT = "TIMEOUT",
|
||
|
TLS = "TLS",
|
||
|
UNKNOWN = "UNKNOWN_ERROR",
|
||
|
WSS_REQUIRED = "WSS_REQUIRED",
|
||
|
|
||
|
// emitted by the server
|
||
|
AUTHORIZATION_VIOLATION = "AUTHORIZATION_VIOLATION",
|
||
|
AUTHENTICATION_EXPIRED = "AUTHENTICATION_EXPIRED",
|
||
|
NATS_PROTOCOL_ERR = "NATS_PROTOCOL_ERR",
|
||
|
PERMISSIONS_VIOLATION = "PERMISSIONS_VIOLATION",
|
||
|
}
|
||
|
|
||
|
export class Messages {
|
||
|
messages: Map<string, string>;
|
||
|
|
||
|
constructor() {
|
||
|
this.messages = new Map<string, string>();
|
||
|
this.messages.set(
|
||
|
ErrorCode.INVALID_PAYLOAD_TYPE,
|
||
|
"Invalid payload type - payloads can be 'binary', 'string', or 'json'",
|
||
|
);
|
||
|
this.messages.set(ErrorCode.BAD_JSON, "Bad JSON");
|
||
|
this.messages.set(
|
||
|
ErrorCode.WSS_REQUIRED,
|
||
|
"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 class NatsError extends Error {
|
||
|
name: string;
|
||
|
message: string;
|
||
|
code: string;
|
||
|
chainedError?: Error;
|
||
|
|
||
|
/**
|
||
|
* @param {String} message
|
||
|
* @param {String} code
|
||
|
* @param {Error} [chainedError]
|
||
|
* @constructor
|
||
|
*
|
||
|
* @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.AUTHENTICATION_EXPIRED ||
|
||
|
this.code === ErrorCode.AUTHORIZATION_VIOLATION;
|
||
|
}
|
||
|
|
||
|
isPermissionError(): boolean {
|
||
|
return this.code === ErrorCode.PERMISSIONS_VIOLATION;
|
||
|
}
|
||
|
|
||
|
isProtocolError(): boolean {
|
||
|
return this.code === ErrorCode.NATS_PROTOCOL_ERR;
|
||
|
}
|
||
|
}
|