Skip to content

Commit

Permalink
fetch-error-context (#33)
Browse files Browse the repository at this point in the history
add context to NotFoundError

resolves #20
  • Loading branch information
wydengyre authored Jan 27, 2024
1 parent ee85b6f commit ccc4ea1
Show file tree
Hide file tree
Showing 5 changed files with 9 additions and 5 deletions.
4 changes: 2 additions & 2 deletions lib/error.ts
Original file line number Diff line number Diff line change
@@ -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";
}
}
1 change: 1 addition & 0 deletions lib/feed.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
2 changes: 1 addition & 1 deletion lib/feed.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand Down
5 changes: 4 additions & 1 deletion lib/genres.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
2 changes: 1 addition & 1 deletion lib/genres.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand Down

0 comments on commit ccc4ea1

Please sign in to comment.