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 parsedResponse = JSON.parse(response.text()) as FindQuery;
|
||||||
const cleanedResponse = {
|
const cleanedResponse = {
|
||||||
...parsedResponse
|
...parsedResponse
|
||||||
};
|
};
|
||||||
|
|
||||||
return removeNullOrUndefinedProperties(cleanedResponse) as any as FindQuery;
|
return removeNullOrUndefinedProperties(cleanedResponse) as any as FindQuery;
|
||||||
}
|
}
|
||||||
@@ -9,11 +9,12 @@ import { and, eq, inArray } from 'drizzle-orm';
|
|||||||
import { NextApiRequest, NextApiResponse } from 'next';
|
import { NextApiRequest, NextApiResponse } from 'next';
|
||||||
|
|
||||||
export default async function search(req: NextApiRequest, res: NextApiResponse) {
|
export default async function search(req: NextApiRequest, res: NextApiResponse) {
|
||||||
const { query } = req.body;
|
try {
|
||||||
const currentUser = await getCurrentUser(req);
|
const { query } = req.body;
|
||||||
const parsedQuery = await parseFindQuery(query as string);
|
const currentUser = await getCurrentUser(req);
|
||||||
const { personIds } = parsedQuery;
|
const parsedQuery = await parseFindQuery(query as string);
|
||||||
const url = ENV.IMMICH_URL + "/api/search/smart";
|
const { personIds } = parsedQuery;
|
||||||
|
const url = ENV.IMMICH_URL + "/api/search/smart";
|
||||||
|
|
||||||
let dbPeople: Person[] = [];
|
let dbPeople: Person[] = [];
|
||||||
if (personIds) {
|
if (personIds) {
|
||||||
@@ -59,5 +60,9 @@ export default async function search(req: NextApiRequest, res: NextApiResponse)
|
|||||||
}).catch(err => {
|
}).catch(err => {
|
||||||
res.status(500).json({ error: err.message });
|
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