From 6ca9719de7041f61739b437045313c6192cce5aa Mon Sep 17 00:00:00 2001 From: Thibault Le Ouay Date: Mon, 31 Jul 2023 20:13:55 +0200 Subject: [PATCH] hotfix when third retry (#111) --- .../src/app/api/checker/regions/_checker.ts | 24 ++++++++++++++----- 1 file changed, 18 insertions(+), 6 deletions(-) diff --git a/apps/web/src/app/api/checker/regions/_checker.ts b/apps/web/src/app/api/checker/regions/_checker.ts index ec83f972..679c30c7 100644 --- a/apps/web/src/app/api/checker/regions/_checker.ts +++ b/apps/web/src/app/api/checker/regions/_checker.ts @@ -19,7 +19,7 @@ export const monitorSchema = tbIngestPingResponse.pick({ const tb = new Tinybird({ token: env.TINY_BIRD_API_KEY }); const monitor = async ( - res: Response, + res: { text: () => Promise; status: number }, monitorInfo: z.infer, region: string, latency: number, @@ -77,10 +77,22 @@ export const checker = async (request: Request, region: string) => { throw new Error("Invalid response body"); } - const startTime = Date.now(); - const res = await fetch(result.data.url, { cache: "no-store" }); - const endTime = Date.now(); - const latency = endTime - startTime; + try { + const startTime = Date.now(); + const res = await fetch(result.data.url, { cache: "no-store" }); - await monitor(res, result.data, region, latency); + const endTime = Date.now(); + const latency = endTime - startTime; + await monitor(res, result.data, region, latency); + } catch (e) { + // if on the third retry we still get an error, we should report it + if (request.headers.get("Upstash-Retried") === "3") { + await monitor( + { status: 500, text: () => Promise.resolve(`${e}`) }, + result.data, + region, + -1, + ); + } + } }; -- 2.51.2