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'; import { ChatbotService } from 'src/chatbot/chatbot.service';
@Module({ @Module({
imports: [HttpModule], imports: [
HttpModule.register({
timeout: 5 * 60 * 1000, // 5 minutes in milliseconds
})
],
controllers: [ChatbotController], controllers: [ChatbotController],
providers: [ChatbotService], providers: [ChatbotService],
exports: [], exports: [],

View File

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