From d47916d78615db6aca8a33bbf187599acf26f6e3 Mon Sep 17 00:00:00 2001 From: Varun Raj Date: Tue, 25 Mar 2025 21:48:42 +0530 Subject: [PATCH] fix: improve error handling in search API and log response from Gemini - Wrapped the search API logic in a try-catch block to handle potential errors during query parsing. - Added console logging for the response received from the Gemini API for better debugging. - Ensured that a user-friendly error message is returned in case of failure. --- src/helpers/gemini.helper.ts | 3 +++ src/pages/api/find/search.ts | 15 ++++++++++----- 2 files changed, 13 insertions(+), 5 deletions(-) diff --git a/src/helpers/gemini.helper.ts b/src/helpers/gemini.helper.ts index bfe853e..52b5e9f 100644 --- a/src/helpers/gemini.helper.ts +++ b/src/helpers/gemini.helper.ts @@ -80,9 +80,12 @@ export const parseFindQuery = async (query: string): Promise => { } }); + console.log("response from gemini", response.text()); + const parsedResponse = JSON.parse(response.text()) as FindQuery; const cleanedResponse = { ...parsedResponse }; + return removeNullOrUndefinedProperties(cleanedResponse) as any as FindQuery; } diff --git a/src/pages/api/find/search.ts b/src/pages/api/find/search.ts index 471bc40..2d9f34a 100644 --- a/src/pages/api/find/search.ts +++ b/src/pages/api/find/search.ts @@ -9,11 +9,12 @@ import { and, eq, inArray } from 'drizzle-orm'; import { NextApiRequest, NextApiResponse } from 'next'; export default async function search(req: NextApiRequest, res: NextApiResponse) { - const { query } = req.body; - const currentUser = await getCurrentUser(req); - const parsedQuery = await parseFindQuery(query as string); - const { personIds } = parsedQuery; - const url = ENV.IMMICH_URL + "/api/search/smart"; + try { + const { query } = req.body; + const currentUser = await getCurrentUser(req); + const parsedQuery = await parseFindQuery(query as string); + const { personIds } = parsedQuery; + const url = ENV.IMMICH_URL + "/api/search/smart"; let dbPeople: Person[] = []; if (personIds) { @@ -59,5 +60,9 @@ export default async function search(req: NextApiRequest, res: NextApiResponse) }).catch(err => { res.status(500).json({ error: err.message }); }); + } catch (err: any) { + console.error(err); + res.status(500).json({ error: "Failed to parse query" }); + } } \ No newline at end of file