refactor(process): use dedicated CommandLaneClearedError in clearCommandLane

Replace bare `new Error("Command lane cleared")` with a dedicated
`CommandLaneClearedError` class so callers that fire-and-forget
enqueued tasks can catch this specific type and avoid surfacing
unhandled rejection warnings.
This commit is contained in:
Yi LIU
2026-02-14 01:43:33 +08:00
committed by Peter Steinberger
parent a49dd83b14
commit a5ccfa57a8
2 changed files with 14 additions and 2 deletions

View File

@@ -18,6 +18,7 @@ vi.mock("../logging/diagnostic.js", () => ({
import {
clearCommandLane,
CommandLaneClearedError,
enqueueCommand,
enqueueCommandInLane,
getActiveTaskCount,
@@ -218,7 +219,7 @@ describe("command queue", () => {
expect(removed).toBe(1); // only the queued (not active) entry
// The queued promise should reject.
await expect(second).rejects.toThrow("Command lane cleared");
await expect(second).rejects.toBeInstanceOf(CommandLaneClearedError);
// Let the active task finish normally.
resolve1();

View File

@@ -1,5 +1,16 @@
import { diagnosticLogger as diag, logLaneDequeue, logLaneEnqueue } from "../logging/diagnostic.js";
import { CommandLane } from "./lanes.js";
/**
* Dedicated error type thrown when a queued command is rejected because
* its lane was cleared. Callers that fire-and-forget enqueued tasks can
* catch (or ignore) this specific type to avoid unhandled-rejection noise.
*/
export class CommandLaneClearedError extends Error {
constructor(lane?: string) {
super(lane ? `Command lane "${lane}" cleared` : "Command lane cleared");
this.name = "CommandLaneClearedError";
}
}
// Minimal in-process queue to serialize command executions.
// Default lane ("main") preserves the existing behavior. Additional lanes allow
@@ -164,7 +175,7 @@ export function clearCommandLane(lane: string = CommandLane.Main) {
const removed = state.queue.length;
const pending = state.queue.splice(0);
for (const entry of pending) {
entry.reject(new Error("Command lane cleared"));
entry.reject(new CommandLaneClearedError(cleaned));
}
return removed;
}