Skip to content

Commit

Permalink
Merge pull request #85 from samchon/features/error
Browse files Browse the repository at this point in the history
Uniform error pattern.
  • Loading branch information
samchon authored Jun 25, 2024
2 parents 065f61b + 6baad9c commit 1482c9c
Show file tree
Hide file tree
Showing 14 changed files with 681 additions and 2,793 deletions.
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "tgrid",
"version": "1.0.1",
"version": "1.0.2",
"main": "lib/index.js",
"typings": "lib/index.d.ts",
"exports": {
Expand Down Expand Up @@ -55,7 +55,7 @@
"ts-node": "^10.9.2",
"ts-patch": "^3.1.2",
"tslib": "^2.6.2",
"typescript": "^5.4.3",
"typescript": "5.4.5",
"typescript-transform-paths": "^3.4.7",
"whatwg-fetch": "^3.6.2"
},
Expand Down
22 changes: 8 additions & 14 deletions src/components/Communicator.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,4 @@
import {
ConditionVariable,
DomainError,
HashMap,
Pair,
RuntimeError,
} from "tstl";
import { ConditionVariable, HashMap, Pair } from "tstl";

import { Driver } from "../typings/Driver";
import { serializeError } from "../utils/internal/serializeError";
Expand Down Expand Up @@ -96,7 +90,7 @@ export abstract class Communicator<
// REJECT UNRETURNED FUNCTIONS
const rejectError: Error = error
? error
: new RuntimeError("Connection has been closed.");
: new Error("Connection has been closed.");

for (const entry of this.promises_) {
const reject: FunctionLike = entry.second.second;
Expand Down Expand Up @@ -292,11 +286,11 @@ export abstract class Communicator<
//----
if (this.provider_ === undefined)
// PROVIDER MUST BE
throw new RuntimeError(
throw new Error(
`Error on Communicator._Handle_function(): the provider is not specified yet.`,
);
else if (this.provider_ === null)
throw new DomainError(
throw new Error(
"Error on Communicator._Handle_function(): the provider would not be.",
);

Expand All @@ -311,19 +305,19 @@ export abstract class Communicator<

// SECURITY-ERRORS
if (name[0] === "_")
throw new RuntimeError(
throw new Error(
`Error on Communicator._Handle_function(): RFC does not allow access to a member starting with the underscore: Provider.${invoke.listener}()`,
);
else if (name[name.length - 1] === "_")
throw new RuntimeError(
throw new Error(
`Error on Communicator._Handle_function(): RFC does not allow access to a member ending with the underscore: Provider.${invoke.listener}().`,
);
else if (name === "toString" && func === Function.toString)
throw new RuntimeError(
throw new Error(
`Error on Communicator._Handle_function(): RFC on Function.toString() is not allowed: Provider.${invoke.listener}().`,
);
else if (name === "constructor" || name === "prototype")
throw new RuntimeError(
throw new Error(
`Error on Communicator._Handle_function(): RFC does not allow access to ${name}: Provider.${invoke.listener}().`,
);
}
Expand Down
14 changes: 6 additions & 8 deletions src/protocols/internal/AcceptorBase.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import { DomainError, RuntimeError } from "tstl";

import { Communicator } from "../../components/Communicator";

/**
Expand Down Expand Up @@ -97,27 +95,27 @@ export abstract class AcceptorBase<
if (this.state_ === AcceptorBase.State.OPEN) return null;
// ERROR, ONE OF THEM
else if (this.state_ === AcceptorBase.State.NONE)
return new DomainError(
return new Error(
`Error on ${this.constructor.name}.${method}(): not accepted yet.`,
);
else if (this.state_ === AcceptorBase.State.ACCEPTING)
return new DomainError(
return new Error(
`Error on ${this.constructor.name}.${method}(): it's on accepting, wait for a second.`,
);
else if (
this.state_ === AcceptorBase.State.REJECTING ||
AcceptorBase.State.CLOSING
this.state_ === AcceptorBase.State.CLOSING
)
return new RuntimeError(
return new Error(
`Error on ${this.constructor.name}.${method}(): the connection is on closing.`,
);
else if (this.state_ === AcceptorBase.State.CLOSED)
return new RuntimeError(
return new Error(
`Error on ${this.constructor.name}.${method}(): the connection has been closed.`,
);
// UNKNOWN ERROR, IT MAY NOT OCCURED
else
return new RuntimeError(
return new Error(
`Error on ${this.constructor.name}.${method}(): unknown error, but not connected.`,
);
}
Expand Down
12 changes: 5 additions & 7 deletions src/protocols/internal/ConnectorBase.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import { DomainError, RuntimeError } from "tstl";

import { Communicator } from "../../components/Communicator";

/**
Expand Down Expand Up @@ -91,24 +89,24 @@ export abstract class ConnectorBase<
if (this.state_ === ConnectorBase.State.OPEN) return null;
// ERROR, ONE OF THEM
else if (this.state_ === ConnectorBase.State.NONE)
return new DomainError(
return new Error(
`Error on ${this.constructor.name}.${method}(): connect first.`,
);
else if (this.state_ === ConnectorBase.State.CONNECTING)
return new DomainError(
return new Error(
`Error on ${this.constructor.name}.${method}(): it's on connecting, wait for a second.`,
);
else if (this.state_ === ConnectorBase.State.CLOSING)
return new RuntimeError(
return new Error(
`Error on ${this.constructor.name}.${method}(): the connection is on closing.`,
);
else if (this.state_ === ConnectorBase.State.CLOSED)
return new RuntimeError(
return new Error(
`Error on ${this.constructor.name}.${method}(): the connection has been closed.`,
);
// UNKNOWN ERROR, IT MAY NOT OCCURED
else
return new RuntimeError(
return new Error(
`Error on ${this.constructor.name}.${method}(): unknown error, but not connected.`,
);
}
Expand Down
7 changes: 3 additions & 4 deletions src/protocols/web/WebSocketAcceptor.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import type http from "http";
import { DomainError } from "tstl";
import type WebSocket from "ws";

import { Invoke } from "../../components/Invoke";
Expand Down Expand Up @@ -198,7 +197,7 @@ export class WebSocketAcceptor<
public async accept(provider: Provider): Promise<void> {
// VALIDATION
if (this.state_ !== WebSocketAcceptor.State.NONE)
throw new DomainError(
throw new Error(
"Error on WebSocketAcceptor.accept(): you've already accepted (or rejected) the connection.",
);

Expand Down Expand Up @@ -226,8 +225,8 @@ export class WebSocketAcceptor<
public async reject(status?: number, reason?: string): Promise<void> {
// VALIDATION
if (this.state_ !== WebSocketAcceptor.State.NONE)
throw new DomainError(
"You've already accepted (or rejected) the connection.",
throw new Error(
"Error on WebSocketAcceptor.reject(): you've already accepted (or rejected) the connection.",
);

// SEND CLOSING FRAME
Expand Down
22 changes: 16 additions & 6 deletions src/protocols/web/WebSocketConnector.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { DomainError, is_node, sleep_for } from "tstl";
import { is_node, sleep_for } from "tstl";

import { Invoke } from "../../components/Invoke";
import { ConnectorBase } from "../internal/ConnectorBase";
Expand Down Expand Up @@ -78,15 +78,15 @@ export class WebSocketConnector<
// TEST CONDITION
if (this.socket_ && this.state !== WebSocketConnector.State.CLOSED)
if (this.socket_.readyState === WebSocketConnector.State.CONNECTING)
throw new DomainError(
throw new Error(
"Error on WebSocketConnector.connect(): already connecting.",
);
else if (this.socket_.readyState === WebSocketConnector.State.OPEN)
throw new DomainError(
throw new Error(
"Error on WebSocketConnector.connect(): already connected.",
);
else
throw new DomainError(
throw new Error(
"Error on WebSocketConnector.connect(): already closing.",
);

Expand Down Expand Up @@ -147,7 +147,12 @@ export class WebSocketConnector<
reject(new WebSocketError(evt.code, evt.reason));
});
this.socket_!.onerror = once(() => {
reject(new WebSocketError(1006, "Connection refused."));
reject(
new WebSocketError(
1006,
"Error on WebSocketConnector.connect(): connection refused.",
),
);
});
});
}
Expand Down Expand Up @@ -215,7 +220,12 @@ export class WebSocketConnector<
this.socket_!.onerror = once(() => {
if (expired === false) {
completed = true;
reject(new WebSocketError(1006, "Connection refused."));
reject(
new WebSocketError(
1006,
"Error on WebSocketConnector.connect(): connection refused.",
),
);
}
});
});
Expand Down
14 changes: 6 additions & 8 deletions src/protocols/web/WebSocketServer.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import type http from "http";
import type https from "https";
import type net from "net";
import { DomainError, RuntimeError, is_node } from "tstl";
import { is_node } from "tstl";
import type WebSocket from "ws";

import { NodeModule } from "../../utils/internal/NodeModule";
Expand Down Expand Up @@ -86,7 +86,7 @@ export class WebSocketServer<

public constructor(key?: string, cert?: string) {
if (is_node() === false)
throw new DomainError(
throw new Error(
"Error on WebSocketServer.constructor(): only available in NodeJS.",
);

Expand Down Expand Up @@ -126,17 +126,15 @@ export class WebSocketServer<
//----
// POSSIBLE TO OPEN?
if (this.state_ === WebSocketServer.State.OPEN)
throw new DomainError(
throw new Error(
"Error on WebSocketServer.open(): it has already been opened.",
);
else if (this.state_ === WebSocketServer.State.OPENING)
throw new DomainError(
throw new Error(
"Error on WebSocketServer.open(): it's on opening, wait for a second.",
);
else if (this.state_ === WebSocketServer.State.CLOSING)
throw new RuntimeError(
"Error on WebSocketServer.open(): it's on closing.",
);
throw new Error("Error on WebSocketServer.open(): it's on closing.");
// DO OPEN
else if (
this.server_ === null ||
Expand Down Expand Up @@ -190,7 +188,7 @@ export class WebSocketServer<
public async close(): Promise<void> {
// VALIDATION
if (this.state_ !== WebSocketServer.State.OPEN)
throw new DomainError(
throw new Error(
"Error on WebSocketServer.close(): server is not opened.",
);

Expand Down
6 changes: 2 additions & 4 deletions src/protocols/workers/SharedWorkerAcceptor.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import { DomainError } from "tstl";

import { Invoke } from "../../components/Invoke";
import { AcceptorBase } from "../internal/AcceptorBase";
import { IReject } from "./internal/IReject";
Expand Down Expand Up @@ -133,7 +131,7 @@ export class SharedWorkerAcceptor<
public async accept(provider: Provider): Promise<void> {
// TEST CONDITION
if (this.state_ !== SharedWorkerAcceptor.State.NONE)
throw new DomainError(
throw new Error(
"Error on SharedWorkerAcceptor.accept(): you've already accepted (or rejected) the connection.",
);

Expand Down Expand Up @@ -165,7 +163,7 @@ export class SharedWorkerAcceptor<
public async reject(reason: string = "Rejected by server"): Promise<void> {
// TEST CONDITION
if (this.state_ !== SharedWorkerAcceptor.State.NONE)
throw new DomainError(
throw new Error(
"Error on SharedWorkerAcceptor.reject(): you've already accepted (or rejected) the connection.",
);

Expand Down
21 changes: 13 additions & 8 deletions src/protocols/workers/SharedWorkerConnector.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { DomainError, RuntimeError, sleep_until } from "tstl";
import { sleep_until } from "tstl";

import { Invoke } from "../../components/Invoke";
import { ConnectorBase } from "../internal/ConnectorBase";
Expand Down Expand Up @@ -85,10 +85,15 @@ export class SharedWorkerConnector<
// TEST CONDITION
if (this.port_ && this.state_ !== SharedWorkerConnector.State.CLOSED) {
if (this.state_ === SharedWorkerConnector.State.CONNECTING)
throw new DomainError("On connecting.");
throw new Error(
"Error on SharedWorkerConnector.connect(): on connecting.",
);
else if (this.state_ === SharedWorkerConnector.State.OPEN)
throw new DomainError("Already connected.");
else throw new DomainError("Closing.");
throw new Error(
"Error on SharedWorkerConnector.connect(): already connected.",
);
else
throw new Error("Error on SharedWorkerConnector.connect(): closing.");
}

//----
Expand All @@ -113,7 +118,7 @@ export class SharedWorkerConnector<
(await this._Handshake(options.timeout, at)) !==
SharedWorkerConnector.State.CONNECTING
)
throw new DomainError(
throw new Error(
`Error on SharedWorkerConnector.connect(): target shared-worker may not be opened by TGrid. It's not following the TGrid's own handshake rule when connecting.`,
);

Expand Down Expand Up @@ -142,9 +147,9 @@ export class SharedWorkerConnector<
reject.name === "reject" &&
typeof reject.message === "string"
)
throw new RuntimeError(reject.message);
throw new Error(reject.message);
else
throw new DomainError(
throw new Error(
`Error on SharedWorkerConnector.connect(): target shared-worker may not be opened by TGrid. It's not following the TGrid's own handshake rule.`,
);
}
Expand All @@ -170,7 +175,7 @@ export class SharedWorkerConnector<
sleep_until(at).then(() => {
if (completed === false) {
reject(
new DomainError(
new Error(
`Error on SharedWorkerConnector.connect(): target shared-worker is not sending handshake data over ${timeout} milliseconds.`,
),
);
Expand Down
10 changes: 5 additions & 5 deletions src/protocols/workers/SharedWorkerServer.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { DomainError, HashSet, is_node } from "tstl";
import { HashSet, is_node } from "tstl";

import { IHeaderWrapper } from "../internal/IHeaderWrapper";
import { IServer } from "../internal/IServer";
Expand Down Expand Up @@ -85,15 +85,15 @@ export class SharedWorkerServer<
): Promise<void> {
// TEST CONDITION
if (is_node() === true)
throw new DomainError(
throw new Error(
"Error on SharedWorkerServer.open(): SharedWorker is not supported in the NodeJS.",
);
else if (self.document !== undefined)
throw new DomainError(
throw new Error(
"Error on SharedWorkerServer.open(): this is not the SharedWorker.",
);
else if (this.state_ !== SharedWorkerServer.State.NONE)
throw new DomainError(
throw new Error(
"Error on SharedWorkerServer.open(): the server has been opened yet.",
);

Expand Down Expand Up @@ -122,7 +122,7 @@ export class SharedWorkerServer<
public async close(): Promise<void> {
// TEST VALIDATION
if (this.state_ !== SharedWorkerServer.State.OPEN)
throw new DomainError(
throw new Error(
"Error on SharedWorkerServer.close(): the server is not opened.",
);

Expand Down
Loading

0 comments on commit 1482c9c

Please sign in to comment.