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.
This commit is contained in:
1 parent
552cbca3b3
commit
d47916d786
2 files changed
+13
-5
No files matched your search
@@ -80,9 +80,12 @@ export const parseFindQuery = async (query: string): Promise<FindQuery> => {
|
||||
}
|
||||
});
|
||||
|
||||
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;
|
||||
}
|
||||
@@ -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" });
|
||||
}
|
||||
|
||||
}
|
||||
Reference in new issue
Block a user