From 888091ab4c881813ad991d1d550cb644039880ea Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Antti=20M=C3=A4ki?= Date: Fri, 10 Nov 2023 15:11:08 +0200 Subject: [PATCH] @thunderstore/thunderstore-api: improve error handling Sentry handling doesn't need to be done on package level since we're doing it on app level. Try to throw useful error messages, taking into account I don't how much of the error stack is available on Sentry or NextJs logs this way. Refs no ticket --- packages/thunderstore-api/src/apiFetch.ts | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/packages/thunderstore-api/src/apiFetch.ts b/packages/thunderstore-api/src/apiFetch.ts index 184fc779f..ddc4c00e0 100644 --- a/packages/thunderstore-api/src/apiFetch.ts +++ b/packages/thunderstore-api/src/apiFetch.ts @@ -1,8 +1,5 @@ import { RequestConfig } from "./index"; -/** - * TODO: plug Sentry to error handling. - */ export async function apiFetch( config: RequestConfig, path: string, @@ -16,11 +13,13 @@ export async function apiFetch( try { response = await fetch(url, settings); } catch (e) { - throw new Error(e instanceof Error ? e.message : "Data fetching error"); + const msg = e instanceof Error ? e.message : "Unknown error"; + throw new Error(`Fetching ${url.href}: ${msg}`); } if (!response.ok) { - throw new Error(`${response.status}: ${response.statusText}`); + const prefix = `Fetching ${url.href}`; + throw new Error(`${prefix}: (${response.status}) ${response.statusText}`); } let values; @@ -28,7 +27,9 @@ export async function apiFetch( try { values = await response.json(); } catch (e) { - throw new Error(e instanceof Error ? e.message : "Deserialization error"); + const prefix = `Deserializing response from ${url.href}`; + const msg = e instanceof Error ? e.message : "Unknown error"; + throw new Error(`${prefix}: ${msg}`); } return values;