diff --git a/lib/nrk.ts b/lib/nrk.ts index 5e4819b..fec43c5 100644 --- a/lib/nrk.ts +++ b/lib/nrk.ts @@ -45,11 +45,11 @@ async function withDownloadLink( } } -// TODO: Replace throws with `console.error` and return `null` if bad response. export const nrkRadio = { - search: async (query: string): Promise => { + search: async (query: string): Promise => { if (query === "") { - throw "Empty search query."; + console.error("Empty search query."); + return null; } const { status, body } = await get< searchComponents["schemas"]["searchresult"] @@ -57,7 +57,9 @@ export const nrkRadio = { if (status === STATUS_CODE.OK && body) { return body.results.series?.results; } - throw `Something went wrong with ${query} - got status ${status}`; + + console.error(`Something went wrong with ${query} - got status ${status}`); + return null; }, getSerieData: async (seriesId: string) => { let [ diff --git a/lib/nrk_test.ts b/lib/nrk_test.ts index 0d67a9f..cb4eb03 100644 --- a/lib/nrk_test.ts +++ b/lib/nrk_test.ts @@ -1,5 +1,4 @@ import { assertEquals } from "$std/assert/assert_equals.ts"; -import { assertRejects } from "$std/assert/assert_rejects.ts"; import { nrkRadio } from "./nrk.ts"; import { assertGreaterOrEqual } from "$std/assert/assert_greater_or_equal.ts"; import { assertExists } from "https://deno.land/std@0.216.0/assert/assert_exists.ts"; @@ -12,8 +11,9 @@ Deno.test("Verify search query `trygd` returns one result: 'Trygdekontoret'", as assertEquals(result[0].seriesId, "trygdekontoret"); }); -Deno.test("Verify empty search query yields error", async () => { - await assertRejects(async () => await nrkRadio.search("")); +Deno.test("Verify empty search query yields `null`", async () => { + const result = await nrkRadio.search(""); + assertEquals(result, null); }); Deno.test("Verify getting series data for 'trygdekontoret' works", async () => {