From 2e123c0765ddb3e31252bc09d6a3d7bd389a03f1 Mon Sep 17 00:00:00 2001 From: Kat Schelonka Date: Thu, 12 Mar 2026 15:30:16 -0700 Subject: [PATCH] fix(hibp): improve error tracking and fix json decode error --- src/utils/hibp.ts | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/src/utils/hibp.ts b/src/utils/hibp.ts index 768d2271c07..73833298f71 100644 --- a/src/utils/hibp.ts +++ b/src/utils/hibp.ts @@ -19,6 +19,7 @@ import { } from "../db/redis/client.ts"; import { config } from "../config.ts"; import { incKanonFailure, incKanonRequest } from "./metrics.ts"; +import { captureException } from "@sentry/node"; // TODO: fix hardcode const HIBP_USER_AGENT = "monitor/1.0.0"; @@ -69,9 +70,9 @@ async function _throttledFetch( try { meters?.requests?.(); const response = await fetch(url, reqOptions); - const responseJson = await response.json(); - if (response.ok) return responseJson as unknown; - + if (response.ok) { + return await response.json(); + } switch (response.status) { case 404: // 404 can mean "no results", return undefined response @@ -97,12 +98,13 @@ async function _throttledFetch( } default: meters?.failures?.(response.status ?? 500); - throw responseJson; + throw Error( + `HTTP Response: [${response.status}] ${response.statusText}`, + ); } } catch (e) { - if (e instanceof Error) { - logger.error("hibp_throttle_fetch_error", { stack: e.stack }); - } + logger.error("hibp_throttle_fetch_error", { error: e, url }); + captureException(e, { data: url }); throw e; } } @@ -116,7 +118,7 @@ async function hibpApiFetch(path: string, options = {}) { try { return await _throttledFetch(url, reqOptions); } catch (ex) { - logger.error("hibp_api_fetch", { exception: ex }); + logger.error("hibp_api_fetch", { exception: ex, path }); } } /* c8 ignore stop */