From ccc4ea1552f8e3ed4e17be7975dbf3285b4e3a86 Mon Sep 17 00:00:00 2001 From: Matt Leon <108271225+wydengyre@users.noreply.github.com> Date: Sun, 28 Jan 2024 05:55:11 +0700 Subject: [PATCH] fetch-error-context (#33) add context to NotFoundError resolves https://github.com/wydengyre/raiplayrss/issues/20 --- lib/error.ts | 4 ++-- lib/feed.test.ts | 1 + lib/feed.ts | 2 +- lib/genres.test.ts | 5 ++++- lib/genres.ts | 2 +- 5 files changed, 9 insertions(+), 5 deletions(-) diff --git a/lib/error.ts b/lib/error.ts index fc65893..398792e 100644 --- a/lib/error.ts +++ b/lib/error.ts @@ -1,6 +1,6 @@ export class NotFoundError extends Error { - constructor(url: URL) { - super(`Not found: ${url}`); + constructor(url: URL, context: string) { + super(`Not found (${context}): ${url}`); this.name = "NotFoundError"; } } diff --git a/lib/feed.test.ts b/lib/feed.test.ts index 5288a85..7639ab8 100644 --- a/lib/feed.test.ts +++ b/lib/feed.test.ts @@ -51,6 +51,7 @@ async function convertFeed404() { const conf: ConvertConf = { raiBaseUrl, baseUrl, poolSize, fetch: fetchFn }; const expectedErr = new NotFoundError( new URL("https://rai.dev/programmi/foo.json"), + "fetching feed", ); await assert.rejects(convertFeed(conf, "programmi/foo.json"), expectedErr); } diff --git a/lib/feed.ts b/lib/feed.ts index 8bb289f..a04b286 100644 --- a/lib/feed.ts +++ b/lib/feed.ts @@ -68,7 +68,7 @@ class FeedFetcher { const res = await this.#fetch(url); if (!res.ok) { if (res.status === 404) { - throw new NotFoundError(url); + throw new NotFoundError(url, "fetching feed"); } throw new Error( `Failed to fetch ${url}: ${res.status} ${res.statusText}`.trim(), diff --git a/lib/genres.test.ts b/lib/genres.test.ts index 7a291b8..b1864fd 100644 --- a/lib/genres.test.ts +++ b/lib/genres.test.ts @@ -28,7 +28,10 @@ async function genresHtmlNotFound() { }; const conf = confWithFetch(fetchFn); - const expectedErr = new NotFoundError(new URL("https://rai.dev/generi.json")); + const expectedErr = new NotFoundError( + new URL("https://rai.dev/generi.json"), + "fetching genres", + ); const p = genresHtml(conf); await assert.rejects(p, expectedErr); } diff --git a/lib/genres.ts b/lib/genres.ts index be111e9..3537963 100644 --- a/lib/genres.ts +++ b/lib/genres.ts @@ -30,7 +30,7 @@ const fetchGenres = async (c: Conf) => { if (!res.ok) { if (res.status === 404) { - throw new NotFoundError(url); + throw new NotFoundError(url, "fetching genres"); } throw new Error( `Failed to fetch ${url} ${res.status} ${res.statusText}`.trim(),