From 14c19a93f34f402653ffae533170569454e67c78 Mon Sep 17 00:00:00 2001 From: Charles Bochet Date: Sun, 9 Jun 2024 22:43:31 +0200 Subject: [PATCH] Handle Network errors in messaging sync --- .../messaging-error-handling.service.ts | 18 ++++++++++++++ ...g-gmail-full-message-list-fetch.service.ts | 24 ++++++++++++++++++- 2 files changed, 41 insertions(+), 1 deletion(-) diff --git a/packages/twenty-server/src/modules/messaging/common/services/messaging-error-handling.service.ts b/packages/twenty-server/src/modules/messaging/common/services/messaging-error-handling.service.ts index 80d7b93d60a1..c8cdce1d02a9 100644 --- a/packages/twenty-server/src/modules/messaging/common/services/messaging-error-handling.service.ts +++ b/packages/twenty-server/src/modules/messaging/common/services/messaging-error-handling.service.ts @@ -94,6 +94,24 @@ export class MessagingErrorHandlingService { workspaceId, ); break; + case 500: + if (reason === 'backendError') { + await this.handleRateLimitExceeded( + error, + syncStep, + messageChannel, + workspaceId, + ); + } else { + await this.messagingChannelSyncStatusService.markAsFailedUnknownAndFlushMessagesToImport( + messageChannel.id, + workspaceId, + ); + throw new Error( + `Unhandled Gmail error code ${code} with reason ${reason}`, + ); + } + break; case 'ECONNRESET': case 'ENOTFOUND': case 'ECONNABORTED': diff --git a/packages/twenty-server/src/modules/messaging/message-import-manager/drivers/gmail/services/messaging-gmail-full-message-list-fetch.service.ts b/packages/twenty-server/src/modules/messaging/message-import-manager/drivers/gmail/services/messaging-gmail-full-message-list-fetch.service.ts index f5bff6f037fd..079baf1ac9cf 100644 --- a/packages/twenty-server/src/modules/messaging/message-import-manager/drivers/gmail/services/messaging-gmail-full-message-list-fetch.service.ts +++ b/packages/twenty-server/src/modules/messaging/message-import-manager/drivers/gmail/services/messaging-gmail-full-message-list-fetch.service.ts @@ -23,6 +23,8 @@ import { MessagingChannelSyncStatusService } from 'src/modules/messaging/common/ import { MessagingGmailClientProvider } from 'src/modules/messaging/message-import-manager/drivers/gmail/providers/messaging-gmail-client.provider'; import { MESSAGING_GMAIL_USERS_MESSAGES_LIST_MAX_RESULT } from 'src/modules/messaging/message-import-manager/drivers/gmail/constants/messaging-gmail-users-messages-list-max-result.constant'; import { MESSAGING_GMAIL_EXCLUDED_CATEGORIES } from 'src/modules/messaging/message-import-manager/drivers/gmail/constants/messaging-gmail-excluded-categories'; +import { GoogleAPIRefreshAccessTokenService } from 'src/modules/connected-account/services/google-api-refresh-access-token/google-api-refresh-access-token.service'; +import { ConnectedAccountRepository } from 'src/modules/connected-account/repositories/connected-account.repository'; @Injectable() export class MessagingGmailFullMessageListFetchService { @@ -40,8 +42,11 @@ export class MessagingGmailFullMessageListFetchService { MessageChannelMessageAssociationWorkspaceEntity, ) private readonly messageChannelMessageAssociationRepository: MessageChannelMessageAssociationRepository, + @InjectObjectMetadataRepository(ConnectedAccountWorkspaceEntity) + private readonly connectedAccountRepository: ConnectedAccountRepository, private readonly messagingChannelSyncStatusService: MessagingChannelSyncStatusService, private readonly gmailErrorHandlingService: MessagingErrorHandlingService, + private readonly googleAPIsRefreshAccessTokenService: GoogleAPIRefreshAccessTokenService, ) {} public async processMessageListFetch( @@ -54,9 +59,26 @@ export class MessagingGmailFullMessageListFetchService { workspaceId, ); + await this.googleAPIsRefreshAccessTokenService.refreshAndSaveAccessToken( + workspaceId, + connectedAccount.id, + ); + + const refreshedConnectedAccount = + await this.connectedAccountRepository.getById( + connectedAccount.id, + workspaceId, + ); + + if (!refreshedConnectedAccount) { + throw new Error( + `Connected account ${connectedAccount.id} not found in workspace ${workspaceId}`, + ); + } + const gmailClient: gmail_v1.Gmail = await this.gmailClientProvider.getGmailClient( - connectedAccount.refreshToken, + refreshedConnectedAccount.refreshToken, ); const { error: gmailError } =