refactor(chatbot): better handling of potential errors

This commit is contained in:
Nic D 2026-02-24 11:04:54 -05:00
parent b7cf7d7208
commit 043bc91a56
2 changed files with 23 additions and 10 deletions

View File

@ -4,7 +4,11 @@ import { HttpModule } from '@nestjs/axios';
import { ChatbotService } from 'src/chatbot/chatbot.service';
@Module({
imports: [HttpModule],
imports: [
HttpModule.register({
timeout: 5 * 60 * 1000, // 5 minutes in milliseconds
})
],
controllers: [ChatbotController],
providers: [ChatbotService],
exports: [],

View File

@ -2,8 +2,8 @@ import { Injectable } from '@nestjs/common';
import { UserMessageDto } from 'src/chatbot/dtos/user-message.dto';
import { HttpService } from '@nestjs/axios';
import { firstValueFrom } from 'rxjs';
import { PageContextDto } from 'src/chatbot/dtos/page-context.dto';
import { Message } from 'src/chatbot/dtos/dialog-message.dto';
import { AxiosError, isAxiosError } from 'axios';
@Injectable()
export class ChatbotService {
@ -11,15 +11,24 @@ export class ChatbotService {
sessionId: string = 'testing';
async pingExternalApi(body: UserMessageDto, email: string): Promise<Message> {
const { data } = await firstValueFrom(this.httpService.post(
'https://n8nai.targo.ca/webhook/chatty-Mcbot',
{ userInput: body.userInput, userId: email, sessionId: this.sessionId, pageContext: body.pageContext ?? undefined }
));
try {
const { data } = await firstValueFrom(this.httpService.post(
'https://n8nai.targo.ca/webhook/chatty-Mcbot',
{ userInput: body.userInput, userId: email, sessionId: this.sessionId, pageContext: body.pageContext ?? undefined }
));
return {
text: data[0].output,
sent: false,
};
} catch (error) {
console.error(error);
return {
text: data[0].output,
sent: false,
};
return {
text: 'chatbot.error.GENERIC_RESPONSE_ERROR',
sent: false,
}
}
}
// async sendPageContext(body: PageContextDto, email: string) {