feat(gemini): Add API key validation and error handling

Includes a check for the presence of the API key before initializing the Gemini AI client. Logs an error and returns an empty array if the API key is not found, preventing runtime errors and providing clearer feedback.
This commit is contained in:
Philip
2026-01-28 17:49:18 -08:00
parent c8ef03a27b
commit ee4f3766cd
+9 -1
View File
@@ -2,7 +2,15 @@ import { GoogleGenAI, Type } from "@google/genai";
import { Question } from "../types";
const generateQuestions = async (topic: string, count: number = 5): Promise<Question[]> => {
const ai = new GoogleGenAI({ apiKey: process.env.API_KEY });
// Use process.env.API_KEY as per guidelines.
const apiKey = process.env.API_KEY;
if (!apiKey) {
console.error("API Key is missing. Check your .env file.");
return [];
}
const ai = new GoogleGenAI({ apiKey });
try {
const response = await ai.models.generateContent({