mirror of
https://github.com/EvolutionAPI/evolution-api.git
synced 2025-12-09 01:49:37 -06:00
refactor(chatbot): melhorar tratamento de erros em mensagens no Chatwoot
- Implementada a função `handleStaleConversationError` para centralizar a lógica de tratamento de erros relacionados a conversas não encontradas. - A lógica de retry foi aprimorada para as funções `createMessage` e `sendData`, garantindo que as operações sejam reprocessadas corretamente em caso de falhas. - Removido código duplicado e melhorada a legibilidade do serviço Chatwoot.
This commit is contained in:
parent
8697329f71
commit
5dc1d02d0a
@ -905,14 +905,35 @@ export class ChatwootService {
|
||||
try {
|
||||
return await doCreateMessage(conversationId);
|
||||
} catch (error) {
|
||||
const errorMessage = error.toString().toLowerCase();
|
||||
const status = error.response?.status;
|
||||
if (errorMessage.includes('not found') || status === 404) {
|
||||
this.logger.warn(`Conversation ${conversationId} not found. Retrying...`);
|
||||
return this.handleStaleConversationError(
|
||||
error,
|
||||
instance,
|
||||
conversationId,
|
||||
messageBody,
|
||||
messageBodyForRetry,
|
||||
'createMessage',
|
||||
(newConvId) => doCreateMessage(newConvId),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
private async handleStaleConversationError(
|
||||
error: any,
|
||||
instance: InstanceDto,
|
||||
conversationId: number,
|
||||
messageBody: any,
|
||||
messageBodyForRetry: any,
|
||||
functionName: string,
|
||||
originalFunction: (newConversationId: number) => Promise<any>,
|
||||
) {
|
||||
if (axios.isAxiosError(error) && error.response?.status === 404) {
|
||||
this.logger.warn(
|
||||
`Conversation ${conversationId} not found in Chatwoot. Retrying operation from ${functionName}...`,
|
||||
);
|
||||
const bodyForRetry = messageBodyForRetry || messageBody;
|
||||
|
||||
if (!bodyForRetry) {
|
||||
this.logger.error('Cannot retry createMessage without a message body for context.');
|
||||
if (!bodyForRetry || !bodyForRetry.key?.remoteJid) {
|
||||
this.logger.error(`Cannot retry ${functionName} without a message body for context.`);
|
||||
return null;
|
||||
}
|
||||
|
||||
@ -922,18 +943,17 @@ export class ChatwootService {
|
||||
|
||||
const newConversationId = await this.createConversation(instance, bodyForRetry);
|
||||
if (!newConversationId) {
|
||||
this.logger.error(`Failed to create new conversation for ${remoteJid}`);
|
||||
this.logger.error(`Failed to create new conversation for ${remoteJid} during retry.`);
|
||||
return null;
|
||||
}
|
||||
|
||||
this.logger.log(`Retrying message creation for ${remoteJid} with new conversation ${newConversationId}`);
|
||||
return await doCreateMessage(newConversationId);
|
||||
this.logger.log(`Retrying ${functionName} for ${remoteJid} with new conversation ${newConversationId}`);
|
||||
return await originalFunction(newConversationId);
|
||||
} else {
|
||||
this.logger.error(`Error creating message: ${error}`);
|
||||
this.logger.error(`Error in ${functionName}: ${error}`);
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public async getOpenConversationByContact(
|
||||
instance: InstanceDto,
|
||||
@ -1086,34 +1106,15 @@ export class ChatwootService {
|
||||
try {
|
||||
return await doSendData(conversationId);
|
||||
} catch (error) {
|
||||
const errorMessage = error.toString().toLowerCase();
|
||||
const status = error.response?.status;
|
||||
|
||||
if (errorMessage.includes('not found') || status === 404) {
|
||||
this.logger.warn(`Conversation ${conversationId} not found. Retrying...`);
|
||||
const bodyForRetry = messageBodyForRetry || messageBody;
|
||||
|
||||
if (!bodyForRetry) {
|
||||
this.logger.error('Cannot retry sendData without a message body for context.');
|
||||
return null;
|
||||
}
|
||||
|
||||
const {remoteJid} = bodyForRetry.key;
|
||||
const cacheKey = `${instance.instanceName}:createConversation-${remoteJid}`;
|
||||
await this.cache.delete(cacheKey);
|
||||
|
||||
const newConversationId = await this.createConversation(instance, bodyForRetry);
|
||||
if (!newConversationId) {
|
||||
this.logger.error(`Failed to create new conversation for ${remoteJid}`);
|
||||
return null;
|
||||
}
|
||||
|
||||
this.logger.log(`Retrying sendData for ${remoteJid} with new conversation ${newConversationId}`);
|
||||
return await doSendData(newConversationId);
|
||||
} else {
|
||||
this.logger.error(error);
|
||||
return null;
|
||||
}
|
||||
return this.handleStaleConversationError(
|
||||
error,
|
||||
instance,
|
||||
conversationId,
|
||||
messageBody,
|
||||
messageBodyForRetry,
|
||||
'sendData',
|
||||
(newConvId) => doSendData(newConvId),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Loading…
Reference in New Issue
Block a user