Improved support for error handling

This commit is contained in:
Matias Espinoza 2023-03-13 13:23:11 -03:00
parent 03aa9d0c82
commit 7390cbdbbb
2 changed files with 13 additions and 6 deletions

View File

@ -1,6 +1,7 @@
import {
ChatCompletionRequestMessage, ChatCompletionResponseMessage, Configuration, OpenAIApi,
} from 'openai';
import process from 'process';
import { AI } from '@/models/ai';
import { Runnable } from '@/models/runnable';
import { Logger } from '@/logger';
@ -8,7 +9,7 @@ import { Logger } from '@/logger';
export class Api implements AI, Runnable {
private _logger: Logger;
private _api: OpenAIApi;
private _api!: OpenAIApi;
private readonly _configuration: Configuration;
@ -18,12 +19,16 @@ export class Api implements AI, Runnable {
this._configuration = new Configuration({
apiKey: process.env.OPENAI_API_KEY,
});
this._api = new OpenAIApi(this._configuration);
}
run(): void {
this._logger.service.info('OpenAI Service has been initialized successfully.');
try {
this._api = new OpenAIApi(this._configuration);
this._logger.service.info('OpenAI Service has been initialized successfully.');
} catch (error) {
this._logger.service.error(`Failed to start OpenAI Service: ${error}`);
process.exit(1);
}
}
async chatCompletion(chatHistory: ChatCompletionRequestMessage[])

View File

@ -1,6 +1,7 @@
import {
ActivityType, Client, CommandInteraction, IntentsBitField, Interaction, Partials,
} from 'discord.js';
import process from 'process';
import { Logger } from '@/logger';
import { Runnable } from '@/models/runnable';
import { AI } from '@/models/ai';
@ -46,8 +47,9 @@ export class Bot implements Runnable {
run(): void {
this._client.login(process.env.DISCORD_API_KEY).then(() => {
this._logger.service.info('Discord Service has been initialized successfully.');
}).catch((reason) => {
this._logger.service.error(`Failed to start Discord Service: ${reason}`);
}).catch((error) => {
this._logger.service.error(`Failed to start Discord Service: ${error}`);
process.exit(1);
});
this._client.on('ready', async () => {