Skip to content

Commit

Permalink
added 429 error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
noelzappy committed Jan 23, 2023
1 parent 164455a commit d1ee385
Showing 1 changed file with 9 additions and 3 deletions.
12 changes: 9 additions & 3 deletions src/controllers/message.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import {
saveConversation,
updateSingleMessageFromSender,
} from "../services/data.service";
import { ChatResponse, SendMessageOptions } from "chatgpt";
import { ChatError, ChatResponse, SendMessageOptions } from "chatgpt";
import DataModel from "../models/data.model";
import { Message } from "whatsapp-web.js";
import { personalMessageHandler } from "src/services/message.service";
Expand Down Expand Up @@ -83,8 +83,14 @@ export const handler = async (message: Message, p: any) => {
const end = Date.now() - start;

Logger.info(`ChatGPT took ` + end + "ms");
} catch (error: any) {
} catch (error: ChatError | any) {
Logger.error(`Failed to send message to ChatGPT API: ` + error);
// message.reply("I'm sorry, I'm not available at the moment to reply. I will as soon as possible.")

const sendErrorResponse = error?.error?.statusCode === 429;
if (sendErrorResponse) {
message.reply(
"I'm sorry, I'm not available at the moment to reply. Please try again after an hour."
);
}
}
};

0 comments on commit d1ee385

Please sign in to comment.