Skip to content

Commit 1482c9c

Browse files
authored
Merge pull request #85 from samchon/features/error
Uniform error pattern.
2 parents 065f61b + 6baad9c commit 1482c9c

14 files changed

+681
-2793
lines changed

package.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "tgrid",
3-
"version": "1.0.1",
3+
"version": "1.0.2",
44
"main": "lib/index.js",
55
"typings": "lib/index.d.ts",
66
"exports": {
@@ -55,7 +55,7 @@
5555
"ts-node": "^10.9.2",
5656
"ts-patch": "^3.1.2",
5757
"tslib": "^2.6.2",
58-
"typescript": "^5.4.3",
58+
"typescript": "5.4.5",
5959
"typescript-transform-paths": "^3.4.7",
6060
"whatwg-fetch": "^3.6.2"
6161
},

src/components/Communicator.ts

+8-14
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,4 @@
1-
import {
2-
ConditionVariable,
3-
DomainError,
4-
HashMap,
5-
Pair,
6-
RuntimeError,
7-
} from "tstl";
1+
import { ConditionVariable, HashMap, Pair } from "tstl";
82

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

10195
for (const entry of this.promises_) {
10296
const reject: FunctionLike = entry.second.second;
@@ -292,11 +286,11 @@ export abstract class Communicator<
292286
//----
293287
if (this.provider_ === undefined)
294288
// PROVIDER MUST BE
295-
throw new RuntimeError(
289+
throw new Error(
296290
`Error on Communicator._Handle_function(): the provider is not specified yet.`,
297291
);
298292
else if (this.provider_ === null)
299-
throw new DomainError(
293+
throw new Error(
300294
"Error on Communicator._Handle_function(): the provider would not be.",
301295
);
302296

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

312306
// SECURITY-ERRORS
313307
if (name[0] === "_")
314-
throw new RuntimeError(
308+
throw new Error(
315309
`Error on Communicator._Handle_function(): RFC does not allow access to a member starting with the underscore: Provider.${invoke.listener}()`,
316310
);
317311
else if (name[name.length - 1] === "_")
318-
throw new RuntimeError(
312+
throw new Error(
319313
`Error on Communicator._Handle_function(): RFC does not allow access to a member ending with the underscore: Provider.${invoke.listener}().`,
320314
);
321315
else if (name === "toString" && func === Function.toString)
322-
throw new RuntimeError(
316+
throw new Error(
323317
`Error on Communicator._Handle_function(): RFC on Function.toString() is not allowed: Provider.${invoke.listener}().`,
324318
);
325319
else if (name === "constructor" || name === "prototype")
326-
throw new RuntimeError(
320+
throw new Error(
327321
`Error on Communicator._Handle_function(): RFC does not allow access to ${name}: Provider.${invoke.listener}().`,
328322
);
329323
}

src/protocols/internal/AcceptorBase.ts

+6-8
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
import { DomainError, RuntimeError } from "tstl";
2-
31
import { Communicator } from "../../components/Communicator";
42

53
/**
@@ -97,27 +95,27 @@ export abstract class AcceptorBase<
9795
if (this.state_ === AcceptorBase.State.OPEN) return null;
9896
// ERROR, ONE OF THEM
9997
else if (this.state_ === AcceptorBase.State.NONE)
100-
return new DomainError(
98+
return new Error(
10199
`Error on ${this.constructor.name}.${method}(): not accepted yet.`,
102100
);
103101
else if (this.state_ === AcceptorBase.State.ACCEPTING)
104-
return new DomainError(
102+
return new Error(
105103
`Error on ${this.constructor.name}.${method}(): it's on accepting, wait for a second.`,
106104
);
107105
else if (
108106
this.state_ === AcceptorBase.State.REJECTING ||
109-
AcceptorBase.State.CLOSING
107+
this.state_ === AcceptorBase.State.CLOSING
110108
)
111-
return new RuntimeError(
109+
return new Error(
112110
`Error on ${this.constructor.name}.${method}(): the connection is on closing.`,
113111
);
114112
else if (this.state_ === AcceptorBase.State.CLOSED)
115-
return new RuntimeError(
113+
return new Error(
116114
`Error on ${this.constructor.name}.${method}(): the connection has been closed.`,
117115
);
118116
// UNKNOWN ERROR, IT MAY NOT OCCURED
119117
else
120-
return new RuntimeError(
118+
return new Error(
121119
`Error on ${this.constructor.name}.${method}(): unknown error, but not connected.`,
122120
);
123121
}

src/protocols/internal/ConnectorBase.ts

+5-7
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
import { DomainError, RuntimeError } from "tstl";
2-
31
import { Communicator } from "../../components/Communicator";
42

53
/**
@@ -91,24 +89,24 @@ export abstract class ConnectorBase<
9189
if (this.state_ === ConnectorBase.State.OPEN) return null;
9290
// ERROR, ONE OF THEM
9391
else if (this.state_ === ConnectorBase.State.NONE)
94-
return new DomainError(
92+
return new Error(
9593
`Error on ${this.constructor.name}.${method}(): connect first.`,
9694
);
9795
else if (this.state_ === ConnectorBase.State.CONNECTING)
98-
return new DomainError(
96+
return new Error(
9997
`Error on ${this.constructor.name}.${method}(): it's on connecting, wait for a second.`,
10098
);
10199
else if (this.state_ === ConnectorBase.State.CLOSING)
102-
return new RuntimeError(
100+
return new Error(
103101
`Error on ${this.constructor.name}.${method}(): the connection is on closing.`,
104102
);
105103
else if (this.state_ === ConnectorBase.State.CLOSED)
106-
return new RuntimeError(
104+
return new Error(
107105
`Error on ${this.constructor.name}.${method}(): the connection has been closed.`,
108106
);
109107
// UNKNOWN ERROR, IT MAY NOT OCCURED
110108
else
111-
return new RuntimeError(
109+
return new Error(
112110
`Error on ${this.constructor.name}.${method}(): unknown error, but not connected.`,
113111
);
114112
}

src/protocols/web/WebSocketAcceptor.ts

+3-4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import type http from "http";
2-
import { DomainError } from "tstl";
32
import type WebSocket from "ws";
43

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

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

233232
// SEND CLOSING FRAME

src/protocols/web/WebSocketConnector.ts

+16-6
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { DomainError, is_node, sleep_for } from "tstl";
1+
import { is_node, sleep_for } from "tstl";
22

33
import { Invoke } from "../../components/Invoke";
44
import { ConnectorBase } from "../internal/ConnectorBase";
@@ -78,15 +78,15 @@ export class WebSocketConnector<
7878
// TEST CONDITION
7979
if (this.socket_ && this.state !== WebSocketConnector.State.CLOSED)
8080
if (this.socket_.readyState === WebSocketConnector.State.CONNECTING)
81-
throw new DomainError(
81+
throw new Error(
8282
"Error on WebSocketConnector.connect(): already connecting.",
8383
);
8484
else if (this.socket_.readyState === WebSocketConnector.State.OPEN)
85-
throw new DomainError(
85+
throw new Error(
8686
"Error on WebSocketConnector.connect(): already connected.",
8787
);
8888
else
89-
throw new DomainError(
89+
throw new Error(
9090
"Error on WebSocketConnector.connect(): already closing.",
9191
);
9292

@@ -147,7 +147,12 @@ export class WebSocketConnector<
147147
reject(new WebSocketError(evt.code, evt.reason));
148148
});
149149
this.socket_!.onerror = once(() => {
150-
reject(new WebSocketError(1006, "Connection refused."));
150+
reject(
151+
new WebSocketError(
152+
1006,
153+
"Error on WebSocketConnector.connect(): connection refused.",
154+
),
155+
);
151156
});
152157
});
153158
}
@@ -215,7 +220,12 @@ export class WebSocketConnector<
215220
this.socket_!.onerror = once(() => {
216221
if (expired === false) {
217222
completed = true;
218-
reject(new WebSocketError(1006, "Connection refused."));
223+
reject(
224+
new WebSocketError(
225+
1006,
226+
"Error on WebSocketConnector.connect(): connection refused.",
227+
),
228+
);
219229
}
220230
});
221231
});

src/protocols/web/WebSocketServer.ts

+6-8
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import type http from "http";
22
import type https from "https";
33
import type net from "net";
4-
import { DomainError, RuntimeError, is_node } from "tstl";
4+
import { is_node } from "tstl";
55
import type WebSocket from "ws";
66

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

8787
public constructor(key?: string, cert?: string) {
8888
if (is_node() === false)
89-
throw new DomainError(
89+
throw new Error(
9090
"Error on WebSocketServer.constructor(): only available in NodeJS.",
9191
);
9292

@@ -126,17 +126,15 @@ export class WebSocketServer<
126126
//----
127127
// POSSIBLE TO OPEN?
128128
if (this.state_ === WebSocketServer.State.OPEN)
129-
throw new DomainError(
129+
throw new Error(
130130
"Error on WebSocketServer.open(): it has already been opened.",
131131
);
132132
else if (this.state_ === WebSocketServer.State.OPENING)
133-
throw new DomainError(
133+
throw new Error(
134134
"Error on WebSocketServer.open(): it's on opening, wait for a second.",
135135
);
136136
else if (this.state_ === WebSocketServer.State.CLOSING)
137-
throw new RuntimeError(
138-
"Error on WebSocketServer.open(): it's on closing.",
139-
);
137+
throw new Error("Error on WebSocketServer.open(): it's on closing.");
140138
// DO OPEN
141139
else if (
142140
this.server_ === null ||
@@ -190,7 +188,7 @@ export class WebSocketServer<
190188
public async close(): Promise<void> {
191189
// VALIDATION
192190
if (this.state_ !== WebSocketServer.State.OPEN)
193-
throw new DomainError(
191+
throw new Error(
194192
"Error on WebSocketServer.close(): server is not opened.",
195193
);
196194

src/protocols/workers/SharedWorkerAcceptor.ts

+2-4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
import { DomainError } from "tstl";
2-
31
import { Invoke } from "../../components/Invoke";
42
import { AcceptorBase } from "../internal/AcceptorBase";
53
import { IReject } from "./internal/IReject";
@@ -133,7 +131,7 @@ export class SharedWorkerAcceptor<
133131
public async accept(provider: Provider): Promise<void> {
134132
// TEST CONDITION
135133
if (this.state_ !== SharedWorkerAcceptor.State.NONE)
136-
throw new DomainError(
134+
throw new Error(
137135
"Error on SharedWorkerAcceptor.accept(): you've already accepted (or rejected) the connection.",
138136
);
139137

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

src/protocols/workers/SharedWorkerConnector.ts

+13-8
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { DomainError, RuntimeError, sleep_until } from "tstl";
1+
import { sleep_until } from "tstl";
22

33
import { Invoke } from "../../components/Invoke";
44
import { ConnectorBase } from "../internal/ConnectorBase";
@@ -85,10 +85,15 @@ export class SharedWorkerConnector<
8585
// TEST CONDITION
8686
if (this.port_ && this.state_ !== SharedWorkerConnector.State.CLOSED) {
8787
if (this.state_ === SharedWorkerConnector.State.CONNECTING)
88-
throw new DomainError("On connecting.");
88+
throw new Error(
89+
"Error on SharedWorkerConnector.connect(): on connecting.",
90+
);
8991
else if (this.state_ === SharedWorkerConnector.State.OPEN)
90-
throw new DomainError("Already connected.");
91-
else throw new DomainError("Closing.");
92+
throw new Error(
93+
"Error on SharedWorkerConnector.connect(): already connected.",
94+
);
95+
else
96+
throw new Error("Error on SharedWorkerConnector.connect(): closing.");
9297
}
9398

9499
//----
@@ -113,7 +118,7 @@ export class SharedWorkerConnector<
113118
(await this._Handshake(options.timeout, at)) !==
114119
SharedWorkerConnector.State.CONNECTING
115120
)
116-
throw new DomainError(
121+
throw new Error(
117122
`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.`,
118123
);
119124

@@ -142,9 +147,9 @@ export class SharedWorkerConnector<
142147
reject.name === "reject" &&
143148
typeof reject.message === "string"
144149
)
145-
throw new RuntimeError(reject.message);
150+
throw new Error(reject.message);
146151
else
147-
throw new DomainError(
152+
throw new Error(
148153
`Error on SharedWorkerConnector.connect(): target shared-worker may not be opened by TGrid. It's not following the TGrid's own handshake rule.`,
149154
);
150155
}
@@ -170,7 +175,7 @@ export class SharedWorkerConnector<
170175
sleep_until(at).then(() => {
171176
if (completed === false) {
172177
reject(
173-
new DomainError(
178+
new Error(
174179
`Error on SharedWorkerConnector.connect(): target shared-worker is not sending handshake data over ${timeout} milliseconds.`,
175180
),
176181
);

src/protocols/workers/SharedWorkerServer.ts

+5-5
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { DomainError, HashSet, is_node } from "tstl";
1+
import { HashSet, is_node } from "tstl";
22

33
import { IHeaderWrapper } from "../internal/IHeaderWrapper";
44
import { IServer } from "../internal/IServer";
@@ -85,15 +85,15 @@ export class SharedWorkerServer<
8585
): Promise<void> {
8686
// TEST CONDITION
8787
if (is_node() === true)
88-
throw new DomainError(
88+
throw new Error(
8989
"Error on SharedWorkerServer.open(): SharedWorker is not supported in the NodeJS.",
9090
);
9191
else if (self.document !== undefined)
92-
throw new DomainError(
92+
throw new Error(
9393
"Error on SharedWorkerServer.open(): this is not the SharedWorker.",
9494
);
9595
else if (this.state_ !== SharedWorkerServer.State.NONE)
96-
throw new DomainError(
96+
throw new Error(
9797
"Error on SharedWorkerServer.open(): the server has been opened yet.",
9898
);
9999

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

0 commit comments

Comments
 (0)