refactor: Returns `null` instead of throwing

Signed-off-by: Tim Hårek Andreassen <tim@harek.no>
This commit is contained in:
Tim Hårek Andreassen 2024-04-06 23:48:55 +02:00
parent ae7821d31e
commit 93d11a56b1
No known key found for this signature in database
GPG Key ID: E59C7734F0E10EB5
2 changed files with 9 additions and 7 deletions

View File

@ -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<SearchResultList> => {
search: async (query: string): Promise<SearchResultList | null> => {
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 [

View File

@ -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 () => {