From 9ce03bf1b35303532adbe5c99d7da54f48b552bc Mon Sep 17 00:00:00 2001 From: Amy <50583248+amydevs@users.noreply.github.com> Date: Thu, 14 Sep 2023 11:00:14 +1000 Subject: [PATCH] chore: removed sysexits --- src/RPCServer.ts | 3 -- src/errors/errors.ts | 22 +--------- src/errors/index.ts | 1 - src/errors/sysexits.ts | 91 ----------------------------------------- src/types.ts | 5 --- src/utils/utils.ts | 9 ---- tests/RPC.test.ts | 3 -- tests/RPCServer.test.ts | 1 - tests/utils.ts | 3 +- 9 files changed, 2 insertions(+), 136 deletions(-) delete mode 100644 src/errors/sysexits.ts diff --git a/src/RPCServer.ts b/src/RPCServer.ts index 4b1f19e..4bc87a8 100644 --- a/src/RPCServer.ts +++ b/src/RPCServer.ts @@ -29,7 +29,6 @@ import * as rpcEvents from './events'; import * as rpcUtils from './utils'; import * as rpcErrors from './errors'; import * as rpcUtilsMiddleware from './utils'; -import sysexits from './errors/sysexits'; import { never } from './errors'; const cleanupReason = Symbol('CleanupReason'); @@ -305,7 +304,6 @@ class RPCServer extends EventTarget { controller.enqueue(value); } catch (e) { const rpcError: JSONRPCError = { - code: e.exitCode ?? sysexits.UNKNOWN, message: e.description ?? '', data: rpcUtils.fromError(e, this.sensitive), }; @@ -578,7 +576,6 @@ class RPCServer extends EventTarget { ); } catch (e) { const rpcError: JSONRPCError = { - code: e.exitCode ?? sysexits.UNKNOWN, message: e.description ?? '', data: rpcUtils.fromError(e, this.sensitive), }; diff --git a/src/errors/errors.ts b/src/errors/errors.ts index e37f544..1f39bc2 100644 --- a/src/errors/errors.ts +++ b/src/errors/errors.ts @@ -1,11 +1,8 @@ import type { Class } from '@matrixai/errors'; import type { JSONValue } from '@/types'; import { AbstractError } from '@matrixai/errors'; -import sysexits from './sysexits'; -interface RPCError extends Error { - exitCode?: number; -} +interface RPCError extends Error {} class ErrorRPC extends Error implements RPCError { constructor(message?: string) { @@ -13,7 +10,6 @@ class ErrorRPC extends Error implements RPCError { this.name = 'ErrorRPC'; this.description = 'Generic Error'; } - exitCode?: number; description?: string; } @@ -22,20 +18,17 @@ class ErrorRPCDestroyed extends ErrorRPC { super(message); // Call the parent constructor this.name = 'ErrorRPCDestroyed'; // Optionally set a specific name this.description = 'Rpc is destroyed'; // Set the specific description - this.exitCode = sysexits.USAGE; // Set the exit code } } class ErrorRPCParse extends ErrorRPC { static description = 'Failed to parse Buffer stream'; - exitCode = sysexits.SOFTWARE; cause: Error | undefined; // Added this line to hold the cause constructor(message?: string, options?: { cause: Error }) { super(message); // Call the parent constructor this.name = 'ErrorRPCParse'; // Optionally set a specific name this.description = 'Failed to parse Buffer stream'; // Set the specific description - this.exitCode = sysexits.SOFTWARE; // Set the exit code // Set the cause if provided in options if (options && options.cause) { @@ -49,7 +42,6 @@ class ErrorRPCStopping extends ErrorRPC { super(message); // Call the parent constructor this.name = 'ErrorRPCStopping'; // Optionally set a specific name this.description = 'Rpc is stopping'; // Set the specific description - this.exitCode = sysexits.USAGE; // Set the exit code } } @@ -63,7 +55,6 @@ class ErrorRPCHandlerFailed extends ErrorRPC { super(message); // Call the parent constructor this.name = 'ErrorRPCHandlerFailed'; // Optionally set a specific name this.description = 'Failed to handle stream'; // Set the specific description - this.exitCode = sysexits.SOFTWARE; // Set the exit code // Set the cause if provided in options if (options && options.cause) { @@ -74,7 +65,6 @@ class ErrorRPCHandlerFailed extends ErrorRPC { class ErrorRPCMessageLength extends ErrorRPC { static description = 'RPC Message exceeds maximum size'; - exitCode = sysexits.DATAERR; } class ErrorRPCMissingResponse extends ErrorRPC { @@ -82,7 +72,6 @@ class ErrorRPCMissingResponse extends ErrorRPC { super(message); this.name = 'ErrorRPCMissingResponse'; this.description = 'Stream ended before response'; - this.exitCode = sysexits.UNAVAILABLE; } } @@ -97,7 +86,6 @@ class ErrorRPCOutputStreamError extends ErrorRPC { super(message); this.name = 'ErrorRPCOutputStreamError'; this.description = 'Output stream failed, unable to send data'; - this.exitCode = sysexits.UNAVAILABLE; // Set the cause if provided in options if (options && options.cause) { @@ -108,7 +96,6 @@ class ErrorRPCOutputStreamError extends ErrorRPC { class ErrorRPCRemote extends ErrorRPC { static description = 'Remote error from RPC call'; - exitCode: number = sysexits.UNAVAILABLE; metadata: JSONValue | undefined; constructor(metadata?: JSONValue, message?: string, options?) { @@ -129,7 +116,6 @@ class ErrorRPCRemote extends ErrorRPC { isNaN(Date.parse(json.data.timestamp)) || typeof json.data.metadata !== 'object' || typeof json.data.data !== 'object' || - typeof json.data.exitCode !== 'number' || ('stack' in json.data && typeof json.data.stack !== 'string') ) { throw new TypeError(`Cannot decode JSON to ${this.name}`); @@ -143,7 +129,6 @@ class ErrorRPCRemote extends ErrorRPC { data: json.data.data, cause: json.data.cause, }); - e.exitCode = json.data.exitCode; e.stack = json.data.stack; return e; } @@ -152,7 +137,6 @@ class ErrorRPCRemote extends ErrorRPC { type: this.name, data: { description: this.description, - exitCode: this.exitCode, }, }; } @@ -163,7 +147,6 @@ class ErrorRPCStreamEnded extends ErrorRPC { super(message); this.name = 'ErrorRPCStreamEnded'; this.description = 'Handled stream has ended'; - this.exitCode = sysexits.NOINPUT; } } @@ -172,7 +155,6 @@ class ErrorRPCTimedOut extends ErrorRPC { super(message); this.name = 'ErrorRPCTimedOut'; this.description = 'RPC handler has timed out'; - this.exitCode = sysexits.UNAVAILABLE; } } @@ -181,7 +163,6 @@ class ErrorUtilsUndefinedBehaviour extends ErrorRPC { super(message); this.name = 'ErrorUtilsUndefinedBehaviour'; this.description = 'You should never see this error'; - this.exitCode = sysexits.SOFTWARE; } } export function never(): never { @@ -194,7 +175,6 @@ class ErrorRPCMethodNotImplemented extends ErrorRPC { this.name = 'ErrorRPCMethodNotImplemented'; this.description = 'This abstract method must be implemented in a derived class'; - this.exitCode = sysexits.USAGE; // Or another suitable exit code } } diff --git a/src/errors/index.ts b/src/errors/index.ts index 0df2a0a..f72bc43 100644 --- a/src/errors/index.ts +++ b/src/errors/index.ts @@ -1,2 +1 @@ -export * from './sysexits'; export * from './errors'; diff --git a/src/errors/sysexits.ts b/src/errors/sysexits.ts deleted file mode 100644 index 935c181..0000000 --- a/src/errors/sysexits.ts +++ /dev/null @@ -1,91 +0,0 @@ -const sysexits = Object.freeze({ - OK: 0, - GENERAL: 1, - // Sysexit standard starts at 64 to avoid conflicts - /** - * The command was used incorrectly, e.g., with the wrong number of arguments, - * a bad flag, a bad syntax in a parameter, or whatever. - */ - USAGE: 64, - /** - * The input data was incorrect in some way. This should only be used for - * user's data and not system files. - */ - DATAERR: 65, - /** - * An input file (not a system file) did not exist or was not readable. - * This could also include errors like "No message" to a mailer - * (if it cared to catch it). - */ - NOINPUT: 66, - /** - * The user specified did not exist. This might be used for mail addresses - * or remote logins. - */ - NOUSER: 67, - /** - * The host specified did not exist. This is used in mail addresses or - * network requests. - */ - NOHOST: 68, - /** - * A service is unavailable. This can occur if a support program or file - * does not exist. This can also be used as a catchall message when - * something you wanted to do does not work, but you do not know why. - */ - UNAVAILABLE: 69, - /** - * An internal software error has been detected. This should be limited to - * non-operating system related errors as possible. - */ - SOFTWARE: 70, - /** - * An operating system error has been detected. This is intended to be used - * for such things as "cannot fork", "cannot create pipe", or the like. - * It in-cludes things like getuid returning a user that does not exist in - * the passwd file. - */ - OSERR: 71, - /** - * Some system file (e.g., /etc/passwd, /var/run/utx.active, etc.) - * does not exist, cannot be opened, or has some sort of error - * (e.g., syntax error). - */ - OSFILE: 72, - /** - * A (user specified) output file cannot be created. - */ - CANTCREAT: 73, - /** - * An error occurred while doing I/O on some file. - */ - IOERR: 74, - /** - * Temporary failure, indicating something that is not really an error. - * In sendmail, this means that a mailer (e.g.) could not create a connection, - * and the request should be reattempted later. - */ - TEMPFAIL: 75, - /** - * The remote system returned something that was "not possible" during a - * protocol exchange. - */ - PROTOCOL: 76, - /** - * You did not have sufficient permission to perform the operation. This is - * not intended for file system problems, which should use EX_NOINPUT or - * EX_CANTCREAT, but rather for higher level permissions. - */ - NOPERM: 77, - /** - * Something was found in an un-configured or mis-configured state. - */ - CONFIG: 78, - CANNOT_EXEC: 126, - COMMAND_NOT_FOUND: 127, - INVALID_EXIT_ARG: 128, - // 128+ are reserved for signal exits - UNKNOWN: 255, -}); - -export default sysexits; diff --git a/src/types.ts b/src/types.ts index 4163056..b18cfe8 100644 --- a/src/types.ts +++ b/src/types.ts @@ -110,11 +110,6 @@ type JSONRPCResponseError = { * This is a JSON RPC error object, it encodes the error data for the JSONRPCResponseError object. */ type JSONRPCError = { - /** - * A Number that indicates the error type that occurred. - * This MUST be an integer. - */ - code: number; /** * A String providing a short description of the error. * The message SHOULD be limited to a concise single sentence. diff --git a/src/utils/utils.ts b/src/utils/utils.ts index cc3097b..9792983 100644 --- a/src/utils/utils.ts +++ b/src/utils/utils.ts @@ -149,12 +149,6 @@ function parseJSONRPCError(message: unknown): JSONRPCError { if (!isObject(message)) { throw new rpcErrors.ErrorRPCParse('must be a JSON POJO'); } - if (!('code' in message)) { - throw new rpcErrors.ErrorRPCParse('`code` property must be defined'); - } - if (typeof message.code !== 'number') { - throw new rpcErrors.ErrorRPCParse('`code` property must be a number'); - } if (!('message' in message)) { throw new rpcErrors.ErrorRPCParse('`message` property must be defined'); } @@ -369,9 +363,6 @@ function toError(errorData, metadata?: JSONValue): ErrorRPCRemote { const remoteError = new ErrorRPCRemote(metadata, error.message, { cause: error, }); - if (error instanceof ErrorRPC) { - remoteError.exitCode = error.exitCode as number; - } return remoteError; } diff --git a/tests/RPC.test.ts b/tests/RPC.test.ts index b3337b8..a6b20b2 100644 --- a/tests/RPC.test.ts +++ b/tests/RPC.test.ts @@ -443,9 +443,6 @@ describe('RPC', () => { const rejection = await callProm; expect(rejection).toBeInstanceOf(rpcErrors.ErrorRPCRemote); - // The error should have specific properties - expect(rejection).toMatchObject({ exitCode: 69 }); - // Cleanup await rpcServer.destroy(); await rpcClient.destroy(); diff --git a/tests/RPCServer.test.ts b/tests/RPCServer.test.ts index b7efc8a..82027fc 100644 --- a/tests/RPCServer.test.ts +++ b/tests/RPCServer.test.ts @@ -780,7 +780,6 @@ describe(`${RPCServer.name}`, () => { jsonrpc: '2.0', id: null, error: { - code: 1, message: 'failure of some kind', }, }; diff --git a/tests/utils.ts b/tests/utils.ts index 7679823..c301a67 100644 --- a/tests/utils.ts +++ b/tests/utils.ts @@ -149,12 +149,11 @@ const jsonRpcErrorArb = ( fc .record( { - code: fc.integer(), message: fc.string(), data: error.map((e) => fromError(e, sensitive)), }, { - requiredKeys: ['code', 'message'], + requiredKeys: ['message'], }, ) .noShrink() as fc.Arbitrary;