Skip to content

Commit 1c8fab8

Browse files
authored
refactor(msteams): unify proactive send failure handling (#129995)
1 parent 0102fcb commit 1c8fab8

1 file changed

Lines changed: 22 additions & 57 deletions

File tree

‎extensions/msteams/src/send.ts‎

Lines changed: 22 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,16 @@ const FILE_CONSENT_THRESHOLD_BYTES = 4 * 1024 * 1024; // 4MB
6666
*/
6767
const MSTEAMS_MAX_MEDIA_BYTES = 100 * 1024 * 1024;
6868

69+
function createMSTeamsSendError(errorPrefix: string, error: unknown): Error {
70+
const classification = classifyMSTeamsSendError(error);
71+
const hint = formatMSTeamsSendErrorHint(classification);
72+
const status = classification.statusCode ? ` (HTTP ${classification.statusCode})` : "";
73+
return new Error(
74+
`${errorPrefix} failed${status}: ${formatUnknownError(error)}${hint ? ` (${hint})` : ""}`,
75+
{ cause: error },
76+
);
77+
}
78+
6979
function createMSTeamsSendReceipt(params: {
7080
conversationId: string;
7181
platformMessageIds: readonly string[];
@@ -333,13 +343,7 @@ export async function sendMessageMSTeams(
333343
kind: "media",
334344
});
335345
} catch (err) {
336-
const classification = classifyMSTeamsSendError(err);
337-
const hint = formatMSTeamsSendErrorHint(classification);
338-
const status = classification.statusCode ? ` (HTTP ${classification.statusCode})` : "";
339-
throw new Error(
340-
`msteams file send failed${status}: ${formatUnknownError(err)}${hint ? ` (${hint})` : ""}`,
341-
{ cause: err },
342-
);
346+
throw createMSTeamsSendError("msteams file send", err);
343347
}
344348
}
345349

@@ -387,13 +391,7 @@ async function sendTextWithMedia(
387391
serviceUrlBoundary: ctx.sdkCloudOptions,
388392
});
389393
} catch (err) {
390-
const classification = classifyMSTeamsSendError(err);
391-
const hint = formatMSTeamsSendErrorHint(classification);
392-
const status = classification.statusCode ? ` (HTTP ${classification.statusCode})` : "";
393-
throw new Error(
394-
`msteams send failed${status}: ${formatUnknownError(err)}${hint ? ` (${hint})` : ""}`,
395-
{ cause: err },
396-
);
394+
throw createMSTeamsSendError("msteams send", err);
397395
}
398396

399397
const messageId = platformMessageIds[0] ?? "unknown";
@@ -439,13 +437,7 @@ async function sendProactiveActivity({
439437
try {
440438
return await sendProactiveActivityRaw({ ctx, activity });
441439
} catch (err) {
442-
const classification = classifyMSTeamsSendError(err);
443-
const hint = formatMSTeamsSendErrorHint(classification);
444-
const status = classification.statusCode ? ` (HTTP ${classification.statusCode})` : "";
445-
throw new Error(
446-
`${errorPrefix} failed${status}: ${formatUnknownError(err)}${hint ? ` (${hint})` : ""}`,
447-
{ cause: err },
448-
);
440+
throw createMSTeamsSendError(errorPrefix, err);
449441
}
450442
}
451443

@@ -544,31 +536,16 @@ export async function sendAdaptiveCardMSTeams(
544536
};
545537
}
546538

547-
type EditMSTeamsMessageParams = {
539+
type MSTeamsMessageMutationParams = {
548540
/** Full config (for credentials) */
549541
cfg: OpenClawConfig;
550542
/** Conversation ID or user ID */
551543
to: string;
552-
/** Activity ID of the message to edit */
544+
/** Activity ID of the message to edit or delete */
553545
activityId: string;
554-
/** New message text */
555-
text: string;
556546
};
557547

558-
type EditMSTeamsMessageResult = {
559-
conversationId: string;
560-
};
561-
562-
type DeleteMSTeamsMessageParams = {
563-
/** Full config (for credentials) */
564-
cfg: OpenClawConfig;
565-
/** Conversation ID or user ID */
566-
to: string;
567-
/** Activity ID of the message to delete */
568-
activityId: string;
569-
};
570-
571-
type DeleteMSTeamsMessageResult = {
548+
type MSTeamsMessageMutationResult = {
572549
conversationId: string;
573550
};
574551

@@ -579,8 +556,8 @@ type DeleteMSTeamsMessageResult = {
579556
* original turn context.
580557
*/
581558
export async function editMessageMSTeams(
582-
params: EditMSTeamsMessageParams,
583-
): Promise<EditMSTeamsMessageResult> {
559+
params: MSTeamsMessageMutationParams & { text: string },
560+
): Promise<MSTeamsMessageMutationResult> {
584561
const { cfg, to, activityId, text } = params;
585562
const { app, conversationId, ref, log, sdkCloudOptions } = await resolveMSTeamsSendContext({
586563
cfg,
@@ -603,13 +580,7 @@ export async function editMessageMSTeams(
603580
{ serviceUrlBoundary: sdkCloudOptions },
604581
);
605582
} catch (err) {
606-
const classification = classifyMSTeamsSendError(err);
607-
const hint = formatMSTeamsSendErrorHint(classification);
608-
const status = classification.statusCode ? ` (HTTP ${classification.statusCode})` : "";
609-
throw new Error(
610-
`msteams edit failed${status}: ${formatUnknownError(err)}${hint ? ` (${hint})` : ""}`,
611-
{ cause: err },
612-
);
583+
throw createMSTeamsSendError("msteams edit", err);
613584
}
614585

615586
log.info("edited proactive message", { conversationId, activityId });
@@ -624,8 +595,8 @@ export async function editMessageMSTeams(
624595
* original turn context.
625596
*/
626597
export async function deleteMessageMSTeams(
627-
params: DeleteMSTeamsMessageParams,
628-
): Promise<DeleteMSTeamsMessageResult> {
598+
params: MSTeamsMessageMutationParams,
599+
): Promise<MSTeamsMessageMutationResult> {
629600
const { cfg, to, activityId } = params;
630601
const { app, conversationId, ref, log, sdkCloudOptions } = await resolveMSTeamsSendContext({
631602
cfg,
@@ -640,13 +611,7 @@ export async function deleteMessageMSTeams(
640611
serviceUrlBoundary: sdkCloudOptions,
641612
});
642613
} catch (err) {
643-
const classification = classifyMSTeamsSendError(err);
644-
const hint = formatMSTeamsSendErrorHint(classification);
645-
const status = classification.statusCode ? ` (HTTP ${classification.statusCode})` : "";
646-
throw new Error(
647-
`msteams delete failed${status}: ${formatUnknownError(err)}${hint ? ` (${hint})` : ""}`,
648-
{ cause: err },
649-
);
614+
throw createMSTeamsSendError("msteams delete", err);
650615
}
651616

652617
log.info("deleted proactive message", { conversationId, activityId });

0 commit comments

Comments
 (0)