Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Propagate rejections up to the functions that caused them #401

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 31 additions & 11 deletions src/protocol/protocol.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,17 +107,37 @@ export class WireProtocol {
if (this.#isPendingResponse) return;
this.#isPendingResponse = true;
while (this.#pendingResponses.size > 0) {
const headerBuffer = await this.#reader.readFull(new Uint8Array(16));
if (!headerBuffer) throw new MongoDriverError("Invalid response header");
const header = parseHeader(headerBuffer);
const bodyBuffer = await this.#reader.readFull(
new Uint8Array(header.messageLength - 16),
);
if (!bodyBuffer) throw new MongoDriverError("Invalid response body");
const reply = deserializeMessage(header, bodyBuffer);
const pendingMessage = this.#pendingResponses.get(header.responseTo);
this.#pendingResponses.delete(header.responseTo);
pendingMessage?.resolve(reply);
try {
const headerBuffer = await this.#reader.readFull(new Uint8Array(16));
if (!headerBuffer) {
throw new MongoDriverError("Invalid response header");
}
const header = parseHeader(headerBuffer);
const pendingMessage = this.#pendingResponses.get(header.responseTo);
try {
const bodyBuffer = await this.#reader.readFull(
new Uint8Array(header.messageLength - 16),
);
if (!bodyBuffer) throw new MongoDriverError("Invalid response body");
const reply = deserializeMessage(header, bodyBuffer);
pendingMessage?.resolve(reply);
} catch (error) {
pendingMessage?.reject(error);
}
this.#pendingResponses.delete(header.responseTo);
} catch (error) {
// If an error occurred in the above block, we won't be able to know for
// sure which specific message triggered the error.
// Though since the state appears to be so broken that we can't even
// read the header anymore, it's likely that the connection has
// simply closed.
// We'll just reject all pending messages so that the user can
// handle these themselves.
for (const pendingMessage of this.#pendingResponses.values()) {
pendingMessage.reject(error);
}
this.#pendingResponses.clear();
}
}
this.#isPendingResponse = false;
}
Expand Down
Loading