From d9ac662302921191b4557d477480a812354ce511 Mon Sep 17 00:00:00 2001 From: Guillim Date: Fri, 28 Mar 2025 17:37:46 +0100 Subject: [PATCH] Microsoft Throtling error and wrongly types on our side (#11266) --- .../microsoft-import-driver.exception.ts | 10 ++++++++++ .../services/microsoft-get-messages.service.ts | 2 +- .../services/microsoft-handle-error.service.ts | 16 ++++++++++++++++ ...messaging-import-exception-handler.service.ts | 9 +++++---- .../messaging-messages-import.service.ts | 2 +- 5 files changed, 33 insertions(+), 6 deletions(-) create mode 100644 packages/twenty-server/src/modules/messaging/message-import-manager/drivers/microsoft/exceptions/microsoft-import-driver.exception.ts diff --git a/packages/twenty-server/src/modules/messaging/message-import-manager/drivers/microsoft/exceptions/microsoft-import-driver.exception.ts b/packages/twenty-server/src/modules/messaging/message-import-manager/drivers/microsoft/exceptions/microsoft-import-driver.exception.ts new file mode 100644 index 000000000..0c362604b --- /dev/null +++ b/packages/twenty-server/src/modules/messaging/message-import-manager/drivers/microsoft/exceptions/microsoft-import-driver.exception.ts @@ -0,0 +1,10 @@ +import { CustomException } from 'src/utils/custom-exception'; + +export class MicrosoftImportDriverException extends CustomException { + statusCode: number; + constructor(message: string, code: string, statusCode: number) { + super(message, code); + this.statusCode = statusCode; + this.code = code; + } +} diff --git a/packages/twenty-server/src/modules/messaging/message-import-manager/drivers/microsoft/services/microsoft-get-messages.service.ts b/packages/twenty-server/src/modules/messaging/message-import-manager/drivers/microsoft/services/microsoft-get-messages.service.ts index 388263728..7998c211a 100644 --- a/packages/twenty-server/src/modules/messaging/message-import-manager/drivers/microsoft/services/microsoft-get-messages.service.ts +++ b/packages/twenty-server/src/modules/messaging/message-import-manager/drivers/microsoft/services/microsoft-get-messages.service.ts @@ -80,7 +80,7 @@ export class MicrosoftGetMessagesService { const messages = parsedResponses.map((response) => { if ('error' in response) { - this.microsoftHandleErrorService.handleMicrosoftMessageFetchError( + this.microsoftHandleErrorService.throwMicrosoftBatchError( response.error, ); } diff --git a/packages/twenty-server/src/modules/messaging/message-import-manager/drivers/microsoft/services/microsoft-handle-error.service.ts b/packages/twenty-server/src/modules/messaging/message-import-manager/drivers/microsoft/services/microsoft-handle-error.service.ts index 75ca905fd..d7a0ec650 100644 --- a/packages/twenty-server/src/modules/messaging/message-import-manager/drivers/microsoft/services/microsoft-handle-error.service.ts +++ b/packages/twenty-server/src/modules/messaging/message-import-manager/drivers/microsoft/services/microsoft-handle-error.service.ts @@ -4,6 +4,7 @@ import { MessageImportDriverException, MessageImportDriverExceptionCode, } from 'src/modules/messaging/message-import-manager/drivers/exceptions/message-import-driver.exception'; +import { MicrosoftImportDriverException } from 'src/modules/messaging/message-import-manager/drivers/microsoft/exceptions/microsoft-import-driver.exception'; @Injectable() export class MicrosoftHandleErrorService { @@ -29,9 +30,24 @@ export class MicrosoftHandleErrorService { ); } + if (error.statusCode === 429) { + throw new MessageImportDriverException( + `Microsoft Graph API ${error.code} ${error.statusCode} error: ${error.message}`, + MessageImportDriverExceptionCode.TEMPORARY_ERROR, + ); + } + throw new MessageImportDriverException( `Microsoft driver error: ${error.message}`, MessageImportDriverExceptionCode.UNKNOWN, ); } + + public throwMicrosoftBatchError(error: any): void { + throw new MicrosoftImportDriverException( + error.message, + error.code, + error.statusCode, + ); + } } diff --git a/packages/twenty-server/src/modules/messaging/message-import-manager/services/messaging-import-exception-handler.service.ts b/packages/twenty-server/src/modules/messaging/message-import-manager/services/messaging-import-exception-handler.service.ts index f31e56b7d..66f0eeaba 100644 --- a/packages/twenty-server/src/modules/messaging/message-import-manager/services/messaging-import-exception-handler.service.ts +++ b/packages/twenty-server/src/modules/messaging/message-import-manager/services/messaging-import-exception-handler.service.ts @@ -18,7 +18,8 @@ export enum MessageImportSyncStep { FULL_MESSAGE_LIST_FETCH = 'FULL_MESSAGE_LIST_FETCH', PARTIAL_MESSAGE_LIST_FETCH = 'PARTIAL_MESSAGE_LIST_FETCH', FULL_OR_PARTIAL_MESSAGE_LIST_FETCH = 'FULL_OR_PARTIAL_MESSAGE_LIST_FETCH', - MESSAGES_IMPORT = 'MESSAGES_IMPORT', + MESSAGES_IMPORT_PENDING = 'MESSAGES_IMPORT_PENDING', + MESSAGES_IMPORT_ONGOING = 'MESSAGES_IMPORT_ONGOING', } @Injectable() @@ -94,7 +95,7 @@ export class MessageImportExceptionHandlerService { workspaceId, ); throw new MessageImportException( - `Unknown error occurred multiple times while importing messages for message channel ${messageChannel.id} in workspace ${workspaceId}`, + `Unknown temporary error occurred multiple times while importing messages for message channel ${messageChannel.id} in workspace ${workspaceId}`, MessageImportExceptionCode.UNKNOWN, ); } @@ -122,8 +123,8 @@ export class MessageImportExceptionHandlerService { [messageChannel.id], ); break; - - case MessageImportSyncStep.MESSAGES_IMPORT: + case MessageImportSyncStep.MESSAGES_IMPORT_PENDING: + case MessageImportSyncStep.MESSAGES_IMPORT_ONGOING: await this.messageChannelSyncStatusService.scheduleMessagesImport([ messageChannel.id, ]); diff --git a/packages/twenty-server/src/modules/messaging/message-import-manager/services/messaging-messages-import.service.ts b/packages/twenty-server/src/modules/messaging/message-import-manager/services/messaging-messages-import.service.ts index 10a156bf7..671d0d267 100644 --- a/packages/twenty-server/src/modules/messaging/message-import-manager/services/messaging-messages-import.service.ts +++ b/packages/twenty-server/src/modules/messaging/message-import-manager/services/messaging-messages-import.service.ts @@ -192,7 +192,7 @@ export class MessagingMessagesImportService { await this.messageImportErrorHandlerService.handleDriverException( error, - MessageImportSyncStep.MESSAGES_IMPORT, + MessageImportSyncStep.MESSAGES_IMPORT_ONGOING, messageChannel, workspaceId, );