From 5ef3926d2af23631baa5ff1fef484f17253853eb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gerg=C5=91=20M=C3=B3ricz?= Date: Fri, 23 Aug 2024 18:47:56 +0200 Subject: [PATCH] fix(scrape,search): handle failed jobs --- apps/api/src/controllers/scrape.ts | 20 ++++++++++++++++---- apps/api/src/controllers/search.ts | 12 +++++++++--- 2 files changed, 25 insertions(+), 7 deletions(-) diff --git a/apps/api/src/controllers/scrape.ts b/apps/api/src/controllers/scrape.ts index 3ffbc92b..880cbbfa 100644 --- a/apps/api/src/controllers/scrape.ts +++ b/apps/api/src/controllers/scrape.ts @@ -58,9 +58,15 @@ export async function scrapeHelper( if (Date.now() >= start + timeout) { clearInterval(int); reject(new Error("Job wait ")); - } else if (await job.getState() === "completed") { - clearInterval(int); - resolve((await getScrapeQueue().getJob(job.id)).returnvalue); + } else { + const state = await job.getState(); + if (state === "completed") { + clearInterval(int); + resolve((await getScrapeQueue().getJob(job.id)).returnvalue); + } else if (state === "failed") { + clearInterval(int); + reject((await getScrapeQueue().getJob(job.id)).failedReason); + } } }, 1000); }))[0] @@ -72,6 +78,12 @@ export async function scrapeHelper( error: "Request timed out", returnCode: 408, } + } else if (typeof e === "string" && (e.includes("Error generating completions: ") || e.includes("Invalid schema for function"))) { + return { + success: false, + error: e, + returnCode: 500, + }; } else { throw e; } @@ -214,6 +226,6 @@ export async function scrapeController(req: Request, res: Response) { } catch (error) { Sentry.captureException(error); Logger.error(error); - return res.status(500).json({ error: error.message }); + return res.status(500).json({ error: typeof error === "string" ? error : (error?.message ?? "Internal Server Error") }); } } diff --git a/apps/api/src/controllers/search.ts b/apps/api/src/controllers/search.ts index d86862b1..aeb044d8 100644 --- a/apps/api/src/controllers/search.ts +++ b/apps/api/src/controllers/search.ts @@ -114,9 +114,15 @@ export async function searchHelper( if (Date.now() >= start + 60000) { clearInterval(int); reject(new Error("Job wait ")); - } else if (await x.getState() === "completed") { - clearInterval(int); - resolve((await getScrapeQueue().getJob(x.id)).returnvalue); + } else { + const state = await x.getState(); + if (state === "completed") { + clearInterval(int); + resolve((await getScrapeQueue().getJob(x.id)).returnvalue); + } else if (state === "failed") { + clearInterval(int); + reject((await getScrapeQueue().getJob(x.id)).failedReason); + } } }, 1000); })))).map(x => x[0]);