Skip to content

Commit

Permalink
refactor: Improve error handling and logging in aiHandler.js and inde…
Browse files Browse the repository at this point in the history
…x.js
  • Loading branch information
xuelink committed Jun 3, 2024
1 parent 63dad07 commit 6451404
Showing 1 changed file with 15 additions and 0 deletions.
15 changes: 15 additions & 0 deletions copilot/aiHandler.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,15 @@ const generationConfig = {

async function handleInteraction(userMessage) {
try {
if (
!userMessage ||
typeof userMessage !== "string" ||
userMessage.trim() === ""
) {
console.error("Invalid user message:", userMessage);
throw new Error("Invalid user message");
}

const chatSession = model.startChat({
generationConfig,
safetySettings,
Expand All @@ -30,6 +39,12 @@ async function handleInteraction(userMessage) {

// Send user message
const result = await chatSession.sendMessage(userMessage);
if (!result || !result.response) {
console.error("Invalid response from generative model:", result);
throw new Error("Invalid response from generative model");
}

// Return the corrected message
return result.response;
} catch (error) {
console.error("Error in handleInteraction:", error);
Expand Down

0 comments on commit 6451404

Please sign in to comment.