Skip to content

Commit

Permalink
refactor: Clarify error message when waitForSpan failed
Browse files Browse the repository at this point in the history
  • Loading branch information
mrasu committed Apr 14, 2024
1 parent e5673ac commit 0d66330
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 9 deletions.
7 changes: 3 additions & 4 deletions src/eventBus/infra/memoryBus.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import { EventBus, WatchCallback } from "@/eventBus/infra/eventBus";
import { TimeoutError } from "@/eventBus/infra/timeoutError";
import { ErrorMessage } from "@/type/common";

const TIMEOUT_ERROR_MESSAGE: ErrorMessage = { error: true, reason: "timeout" };

export class MemoryBus implements EventBus {
private watchingEvents: Map<string, WatchCallback[]> = new Map();

Expand All @@ -17,7 +16,7 @@ export class MemoryBus implements EventBus {
timeoutMs: number,
fn: (data: unknown) => Promise<T | undefined>,
): Promise<T | ErrorMessage> {
return new Promise((resolve) => {
return new Promise((resolve, reject) => {
const callback = async (data: unknown): Promise<void> => {
const res = await fn(data);
if (!res) {
Expand All @@ -31,7 +30,7 @@ export class MemoryBus implements EventBus {

const timeoutId = setTimeout(() => {
this.off(eventName, callback);
resolve(TIMEOUT_ERROR_MESSAGE);
reject(new TimeoutError());
}, timeoutMs);

this.on(eventName, callback);
Expand Down
7 changes: 7 additions & 0 deletions src/eventBus/infra/timeoutError.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { EchoedError } from "@/echoedError";

export class TimeoutError extends EchoedError {
constructor() {
super("timeout");
}
}
16 changes: 14 additions & 2 deletions src/eventBus/spanBus.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { EventBus } from "@/eventBus/infra/eventBus";
import { TimeoutError } from "@/eventBus/infra/timeoutError";
import {
JsonReceiveSpanEvent,
JsonWaitForSpanEvent,
Expand Down Expand Up @@ -53,9 +54,20 @@ export class SpanBus {
filter,
wantId,
});
const span = await listener;

return span;
try {
return await listener;
} catch (e) {
if (e instanceof TimeoutError) {
const waitTime = new Intl.NumberFormat("en").format(waitTimeoutMs);
return {
error: true,
reason: `No matching span found within ${waitTime}ms.`,
};
}

throw e;
}
}

private async emitWaitForSpanEvent(event: WaitForSpanEvent): Promise<void> {
Expand Down
4 changes: 1 addition & 3 deletions src/server/request.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,7 @@ export async function requestWaitForSpanEvent(
const resp = restoreWaitForSpanEventResponse(jsonResponse);

if ("error" in resp) {
throw new EchoedFatalError(
`Error happens while waiting for span. ${resp.reason}`,
);
throw new EchoedFatalError(resp.reason);
}

return resp.span;
Expand Down

0 comments on commit 0d66330

Please sign in to comment.