better logging errors in messages (#12136)
This commit is contained in:
+1
-23
@@ -1,4 +1,4 @@
|
||||
import { Injectable, Logger } from '@nestjs/common';
|
||||
import { Injectable } from '@nestjs/common';
|
||||
|
||||
import { AxiosResponse } from 'axios';
|
||||
import { gmail_v1 as gmailV1 } from 'googleapis';
|
||||
@@ -12,8 +12,6 @@ import { MessageWithParticipants } from 'src/modules/messaging/message-import-ma
|
||||
|
||||
@Injectable()
|
||||
export class GmailGetMessagesService {
|
||||
private readonly logger = new Logger(GmailGetMessagesService.name);
|
||||
|
||||
constructor(
|
||||
private readonly fetchByBatchesService: GmailFetchByBatchService,
|
||||
private readonly gmailHandleErrorService: GmailHandleErrorService,
|
||||
@@ -25,25 +23,13 @@ export class GmailGetMessagesService {
|
||||
ConnectedAccountWorkspaceEntity,
|
||||
'accessToken' | 'refreshToken' | 'id' | 'handle' | 'handleAliases'
|
||||
>,
|
||||
workspaceId: string,
|
||||
): Promise<MessageWithParticipants[]> {
|
||||
let startTime = Date.now();
|
||||
|
||||
const { messageIdsByBatch, batchResponses } =
|
||||
await this.fetchByBatchesService.fetchAllByBatches(
|
||||
messageIds,
|
||||
connectedAccount.accessToken,
|
||||
'batch_gmail_messages',
|
||||
);
|
||||
let endTime = Date.now();
|
||||
|
||||
this.logger.log(
|
||||
`Messaging import for workspace ${workspaceId} and account ${connectedAccount.id} fetching ${
|
||||
messageIds.length
|
||||
} messages in ${endTime - startTime}ms`,
|
||||
);
|
||||
|
||||
startTime = Date.now();
|
||||
|
||||
const messages = batchResponses.flatMap((response, index) => {
|
||||
return this.formatBatchResponseAsMessage(
|
||||
@@ -53,14 +39,6 @@ export class GmailGetMessagesService {
|
||||
);
|
||||
});
|
||||
|
||||
endTime = Date.now();
|
||||
|
||||
this.logger.log(
|
||||
`Messaging import for workspace ${workspaceId} and account ${connectedAccount.id} formatting ${
|
||||
messageIds.length
|
||||
} messages in ${endTime - startTime}ms`,
|
||||
);
|
||||
|
||||
return messages;
|
||||
}
|
||||
|
||||
|
||||
+12
-1
@@ -1,4 +1,4 @@
|
||||
import { Injectable } from '@nestjs/common';
|
||||
import { Injectable, Logger } from '@nestjs/common';
|
||||
|
||||
import { ConnectedAccountWorkspaceEntity } from 'src/modules/connected-account/standard-objects/connected-account.workspace-entity';
|
||||
import { MicrosoftImportDriverException } from 'src/modules/messaging/message-import-manager/drivers/microsoft/exceptions/microsoft-import-driver.exception';
|
||||
@@ -8,6 +8,7 @@ import { isMicrosoftClientTemporaryError } from 'src/modules/messaging/message-i
|
||||
|
||||
@Injectable()
|
||||
export class MicrosoftFetchByBatchService {
|
||||
private readonly logger = new Logger(MicrosoftFetchByBatchService.name);
|
||||
constructor(
|
||||
private readonly microsoftClientProvider: MicrosoftClientProvider,
|
||||
) {}
|
||||
@@ -56,8 +57,18 @@ export class MicrosoftFetchByBatchService {
|
||||
typeof error.body === 'string' &&
|
||||
isMicrosoftClientTemporaryError(error.body)
|
||||
) {
|
||||
// TODO: remove this log once we catch better the error codes
|
||||
this.logger.error(
|
||||
`Error temporary (${error.code}) fetching messages for account ${connectedAccount.id.slice(0, 8)}`,
|
||||
);
|
||||
this.logger.log(error);
|
||||
throw new MicrosoftImportDriverException(error.body, error.code, 429);
|
||||
} else {
|
||||
// TODO: remove this log once we catch better the error codes
|
||||
this.logger.error(
|
||||
`Error unknown (${error.code}) fetching messages for account ${connectedAccount.id.slice(0, 8)}`,
|
||||
);
|
||||
this.logger.log(error);
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
-1
@@ -51,7 +51,6 @@ xdescribe('Microsoft dev tests : get messages service', () => {
|
||||
const result = await service.getMessages(
|
||||
mockMessageIds,
|
||||
mockConnectedAccount,
|
||||
'workspace-1',
|
||||
);
|
||||
|
||||
expect(result).toHaveLength(1);
|
||||
|
||||
-11
@@ -28,10 +28,7 @@ export class MicrosoftGetMessagesService {
|
||||
async getMessages(
|
||||
messageIds: string[],
|
||||
connectedAccount: ConnectedAccountType,
|
||||
workspaceId: string,
|
||||
): Promise<MessageWithParticipants[]> {
|
||||
const startTime = Date.now();
|
||||
|
||||
try {
|
||||
const { batchResponses } =
|
||||
await this.microsoftFetchByBatchService.fetchAllByBatches(
|
||||
@@ -44,14 +41,6 @@ export class MicrosoftGetMessagesService {
|
||||
connectedAccount,
|
||||
);
|
||||
|
||||
const endTime = Date.now();
|
||||
|
||||
this.logger.log(
|
||||
`Messaging import for workspace ${workspaceId} and account ${
|
||||
connectedAccount.id
|
||||
} fetched ${messages.length} messages in ${endTime - startTime}ms`,
|
||||
);
|
||||
|
||||
return messages;
|
||||
} catch (error) {
|
||||
this.microsoftHandleErrorService.handleMicrosoftMessageFetchError(error);
|
||||
|
||||
+4
-1
@@ -1,3 +1,6 @@
|
||||
export const isMicrosoftClientTemporaryError = (body: string): boolean => {
|
||||
return body.includes('Unexpected token < in JSON at position');
|
||||
return (
|
||||
body.includes('Unexpected token < in JSON at position') ||
|
||||
body.includes('ApplicationThrottled 429 error')
|
||||
);
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user