Skip to content

Commit f7ef870

Browse files
authored
Merge pull request #19 from Milly/rejects-instead-throws
🐛 returns rejected Promise instead throws Error
2 parents 6729a74 + abe5762 commit f7ef870

File tree

2 files changed

+15
-16
lines changed

2 files changed

+15
-16
lines changed

session.ts

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -70,27 +70,26 @@ export class Session {
7070

7171
/**
7272
* Send a command or a message to the peer.
73+
* If the session is not running, the promise will be rejected with {@link Error}.
7374
* @param data The data to send.
74-
* @throws If the session is not running.
7575
*/
7676
send(data: Command | Message): Promise<void> {
7777
if (!this.#running) {
78-
throw new Error("Session is not running");
78+
return Promise.reject(new Error("Session is not running"));
7979
}
8080
const { innerWriter } = this.#running;
8181
return innerWriter.write(data);
8282
}
8383

8484
/**
8585
* Receive a message from the peer.
86+
* If the session is not running or the message ID is already reserved, the promise will be rejected with {@link Error}.
8687
* @param msgid The message ID to receive.
8788
* @returns The received message.
88-
* @throws If the session is not running.
89-
* @throws If the message ID is already reserved.
9089
*/
9190
recv(msgid: number): Promise<Message> {
9291
if (!this.#running) {
93-
throw new Error("Session is not running");
92+
return Promise.reject(new Error("Session is not running"));
9493
}
9594
const { reservator } = this.#running;
9695
return reservator.reserve(msgid);
@@ -157,25 +156,25 @@ export class Session {
157156

158157
/**
159158
* Wait until the session is shutdown.
159+
* If the session is not running, the promise will be rejected with {@link Error}.
160160
* @returns A promise that is fulfilled when the session is shutdown.
161-
* @throws If the session is not running.
162161
*/
163162
wait(): Promise<void> {
164163
if (!this.#running) {
165-
throw new Error("Session is not running");
164+
return Promise.reject(new Error("Session is not running"));
166165
}
167166
const { waiter } = this.#running;
168167
return waiter;
169168
}
170169

171170
/**
172171
* Shutdown the session.
172+
* If the session is not running, the promise will be rejected with {@link Error}.
173173
* @returns A promise that is fulfilled when the session is shutdown.
174-
* @throws If the session is not running.
175174
*/
176175
shutdown(): Promise<void> {
177176
if (!this.#running) {
178-
throw new Error("Session is not running");
177+
return Promise.reject(new Error("Session is not running"));
179178
}
180179
// Abort consumer to shutdown session properly.
181180
const { consumerController, waiter } = this.#running;
@@ -185,12 +184,12 @@ export class Session {
185184

186185
/**
187186
* Shutdown the session forcibly.
187+
* If the session is not running, the promise will be rejected with {@link Error}.
188188
* @returns A promise that is fulfilled when the session is shutdown.
189-
* @throws If the session is not running.
190189
*/
191190
forceShutdown(): Promise<void> {
192191
if (!this.#running) {
193-
throw new Error("Session is not running");
192+
return Promise.reject(new Error("Session is not running"));
194193
}
195194
// Abort consumer and producer to shutdown session forcibly.
196195
const { consumerController, producerController, waiter } = this.#running;

session_test.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ Deno.test("Session.send", async (t) => {
4848
const { session } = createDummySession();
4949

5050
const command = buildRedrawCommand();
51-
assertThrows(
51+
assertRejects(
5252
() => session.send(command),
5353
Error,
5454
"Session is not running",
@@ -95,7 +95,7 @@ Deno.test("Session.recv", async (t) => {
9595
() => {
9696
const { session } = createDummySession();
9797

98-
assertThrows(() => session.recv(-1), Error, "Session is not running");
98+
assertRejects(() => session.recv(-1), Error, "Session is not running");
9999
},
100100
);
101101

@@ -161,7 +161,7 @@ Deno.test("Session.wait", async (t) => {
161161
() => {
162162
const { session } = createDummySession();
163163

164-
assertThrows(() => session.wait(), Error, "Session is not running");
164+
assertRejects(() => session.wait(), Error, "Session is not running");
165165
},
166166
);
167167

@@ -200,7 +200,7 @@ Deno.test("Session.shutdown", async (t) => {
200200
() => {
201201
const { session } = createDummySession();
202202

203-
assertThrows(() => session.shutdown(), Error, "Session is not running");
203+
assertRejects(() => session.shutdown(), Error, "Session is not running");
204204
},
205205
);
206206

@@ -251,7 +251,7 @@ Deno.test("Session.forceShutdown", async (t) => {
251251
() => {
252252
const { session } = createDummySession();
253253

254-
assertThrows(
254+
assertRejects(
255255
() => session.forceShutdown(),
256256
Error,
257257
"Session is not running",

0 commit comments

Comments
 (0)