Skip to content

Commit 707009d

Browse files
committed
fix: improve error handling for abort scenarios and enhance error message logging
1 parent c79e54b commit 707009d

1 file changed

Lines changed: 29 additions & 7 deletions

File tree

index.ts

Lines changed: 29 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,20 @@ const createSessionBodySchema = z.object({
7575
triggerMessage: z.string().optional(),
7676
}).strict();
7777

78+
function isAbortError(error: unknown): boolean {
79+
return (
80+
error instanceof DOMException && error.name === "AbortError"
81+
) || (
82+
typeof error === "object" &&
83+
error !== null &&
84+
"name" in error &&
85+
(error.name === "AbortError" || error.name === "APIUserAbortError")
86+
);
87+
}
88+
89+
function getErrorMessage(error: unknown): string {
90+
return error instanceof Error ? error.message : String(error);
91+
}
7892

7993
export default class AdminForthAgentPlugin extends AdminForthPlugin {
8094
options: PluginOptions;
@@ -216,7 +230,11 @@ export default class AdminForthAgentPlugin extends AdminForthPlugin {
216230

217231
const userLanguage = await detectUserLanguage(selectedMode.completionAdapter, input.prompt, input.previousUserMessages)
218232
.catch((error) => {
219-
logger.warn(`Failed to detect user language: ${error.message}`);
233+
if (input.abortSignal?.aborted || isAbortError(error)) {
234+
throw error;
235+
}
236+
237+
logger.warn(`Failed to detect user language: ${getErrorMessage(error)}`);
220238
return null;
221239
});
222240
const systemPrompt = buildAgentTurnSystemPrompt({
@@ -257,6 +275,10 @@ export default class AdminForthAgentPlugin extends AdminForthPlugin {
257275
});
258276

259277
for await (const rawChunk of stream as AsyncIterable<[any, any]>) {
278+
if (input.abortSignal?.aborted) {
279+
throw new DOMException("This operation was aborted", "AbortError");
280+
}
281+
260282
const [token, metadata] = rawChunk;
261283

262284
const nodeName =
@@ -327,13 +349,13 @@ export default class AdminForthAgentPlugin extends AdminForthPlugin {
327349
});
328350
fullResponse = agentResponse.text;
329351
} catch (error) {
330-
if (input.abortSignal?.aborted) {
352+
if (input.abortSignal?.aborted || isAbortError(error)) {
331353
aborted = true;
332354
logger.info(input.abortLogMessage);
333355
} else {
334356
failed = true;
335-
logger.error(`${input.failureLogMessage}:\n${error.message}`);
336-
fullResponse = error.message;
357+
fullResponse = getErrorMessage(error);
358+
logger.error(`${input.failureLogMessage}:\n${fullResponse}`);
337359
input.emitErrorResponse?.(fullResponse);
338360
}
339361
}
@@ -437,13 +459,13 @@ export default class AdminForthAgentPlugin extends AdminForthPlugin {
437459
abortSignal,
438460
});
439461
} catch (error) {
440-
if (abortSignal.aborted) {
462+
if (abortSignal.aborted || isAbortError(error)) {
441463
logger.info("Agent speech transcription aborted by the client");
442464
stream.end();
443465
return null;
444466
}
445467

446-
logger.error(`Agent speech transcription failed:\n${error.message}`);
468+
logger.error(`Agent speech transcription failed:\n${getErrorMessage(error)}`);
447469
stream.error("Speech transcription failed. Check server logs for details.");
448470
stream.end();
449471
return null;
@@ -545,7 +567,7 @@ export default class AdminForthAgentPlugin extends AdminForthPlugin {
545567
stream.end();
546568
return null;
547569
} catch (error) {
548-
if (abortSignal.aborted) {
570+
if (abortSignal.aborted || isAbortError(error)) {
549571
logger.info("Agent speech audio streaming aborted by the client");
550572
} else {
551573
logger.error(`Agent speech audio streaming failed:\n${error}`);

0 commit comments

Comments
 (0)