Skip to content

Commit

Permalink
chore: removed sysexits
Browse files Browse the repository at this point in the history
  • Loading branch information
amydevs committed Sep 14, 2023
1 parent 56f299d commit 8cdc054
Show file tree
Hide file tree
Showing 9 changed files with 2 additions and 138 deletions.
3 changes: 0 additions & 3 deletions src/RPCServer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down Expand Up @@ -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),
};
Expand Down Expand Up @@ -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),
};
Expand Down
22 changes: 1 addition & 21 deletions src/errors/errors.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,15 @@
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<T> extends Error implements RPCError {
constructor(message?: string) {
super(message);
this.name = 'ErrorRPC';
this.description = 'Generic Error';
}
exitCode?: number;
description?: string;
}

Expand All @@ -22,20 +18,17 @@ class ErrorRPCDestroyed<T> extends ErrorRPC<T> {
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<T> extends ErrorRPC<T> {
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) {
Expand All @@ -49,7 +42,6 @@ class ErrorRPCStopping<T> extends ErrorRPC<T> {
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
}
}

Expand All @@ -63,7 +55,6 @@ class ErrorRPCHandlerFailed<T> extends ErrorRPC<T> {
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) {
Expand All @@ -74,15 +65,13 @@ class ErrorRPCHandlerFailed<T> extends ErrorRPC<T> {

class ErrorRPCMessageLength<T> extends ErrorRPC<T> {
static description = 'RPC Message exceeds maximum size';
exitCode = sysexits.DATAERR;
}

class ErrorRPCMissingResponse<T> extends ErrorRPC<T> {
constructor(message?: string) {
super(message);
this.name = 'ErrorRPCMissingResponse';
this.description = 'Stream ended before response';
this.exitCode = sysexits.UNAVAILABLE;
}
}

Expand All @@ -97,7 +86,6 @@ class ErrorRPCOutputStreamError<T> extends ErrorRPC<T> {
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) {
Expand All @@ -108,7 +96,6 @@ class ErrorRPCOutputStreamError<T> extends ErrorRPC<T> {

class ErrorRPCRemote<T> extends ErrorRPC<T> {
static description = 'Remote error from RPC call';
exitCode: number = sysexits.UNAVAILABLE;
metadata: JSONValue | undefined;

constructor(metadata?: JSONValue, message?: string, options?) {
Expand All @@ -129,7 +116,6 @@ class ErrorRPCRemote<T> extends ErrorRPC<T> {
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}`);
Expand All @@ -143,7 +129,6 @@ class ErrorRPCRemote<T> extends ErrorRPC<T> {
data: json.data.data,
cause: json.data.cause,
});
e.exitCode = json.data.exitCode;
e.stack = json.data.stack;
return e;
}
Expand All @@ -152,7 +137,6 @@ class ErrorRPCRemote<T> extends ErrorRPC<T> {
type: this.name,
data: {
description: this.description,
exitCode: this.exitCode,
},
};
}
Expand All @@ -163,7 +147,6 @@ class ErrorRPCStreamEnded<T> extends ErrorRPC<T> {
super(message);
this.name = 'ErrorRPCStreamEnded';
this.description = 'Handled stream has ended';
this.exitCode = sysexits.NOINPUT;
}
}

Expand All @@ -172,7 +155,6 @@ class ErrorRPCTimedOut<T> extends ErrorRPC<T> {
super(message);
this.name = 'ErrorRPCTimedOut';
this.description = 'RPC handler has timed out';
this.exitCode = sysexits.UNAVAILABLE;
}
}

Expand All @@ -181,7 +163,6 @@ class ErrorUtilsUndefinedBehaviour<T> extends ErrorRPC<T> {
super(message);
this.name = 'ErrorUtilsUndefinedBehaviour';
this.description = 'You should never see this error';
this.exitCode = sysexits.SOFTWARE;
}
}
export function never(): never {
Expand All @@ -194,7 +175,6 @@ class ErrorRPCMethodNotImplemented<T> extends ErrorRPC<T> {
this.name = 'ErrorRPCMethodNotImplemented';
this.description =
'This abstract method must be implemented in a derived class';
this.exitCode = sysexits.USAGE; // Or another suitable exit code
}
}

Expand Down
1 change: 0 additions & 1 deletion src/errors/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
export * from './sysexits';
export * from './errors';
91 changes: 0 additions & 91 deletions src/errors/sysexits.ts

This file was deleted.

5 changes: 0 additions & 5 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
9 changes: 0 additions & 9 deletions src/utils/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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');
}
Expand Down Expand Up @@ -369,9 +363,6 @@ function toError(errorData, metadata?: JSONValue): ErrorRPCRemote<unknown> {
const remoteError = new ErrorRPCRemote(metadata, error.message, {
cause: error,
});
if (error instanceof ErrorRPC) {
remoteError.exitCode = error.exitCode as number;
}
return remoteError;
}

Expand Down
3 changes: 0 additions & 3 deletions tests/RPC.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
3 changes: 0 additions & 3 deletions tests/RPCServer.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -433,7 +433,6 @@ describe(`${RPCServer.name}`, () => {
rpcServer.handleStream(readWriteStream);
const rawErrorMessage = (await outputResult)[0]!.toString();
const errorMessage = JSON.parse(rawErrorMessage);
expect(errorMessage.error.code).toEqual(error.exitCode);
expect(errorMessage.error.message).toEqual(error.description);
reject();
await expect(errorProm).toReject();
Expand Down Expand Up @@ -476,7 +475,6 @@ describe(`${RPCServer.name}`, () => {
const rawErrorMessage = (await outputResult)[0]!.toString();
expect(rawErrorMessage).not.toInclude('stack');
const errorMessage = JSON.parse(rawErrorMessage);
expect(errorMessage.error.code).toEqual(error.exitCode);
expect(errorMessage.error.message).toEqual(error.description);
reject();
await expect(errorProm).toReject();
Expand Down Expand Up @@ -780,7 +778,6 @@ describe(`${RPCServer.name}`, () => {
jsonrpc: '2.0',
id: null,
error: {
code: 1,
message: 'failure of some kind',
},
};
Expand Down
3 changes: 1 addition & 2 deletions tests/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<JSONRPCError>;
Expand Down

0 comments on commit 8cdc054

Please sign in to comment.