refactor: Returns `null` instead of throwing
Signed-off-by: Tim Hårek Andreassen <tim@harek.no>
This commit is contained in:
parent
ae7821d31e
commit
93d11a56b1
10
lib/nrk.ts
10
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<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 [
|
||||
|
|
|
|||
|
|
@ -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 () => {
|
||||
|
|
|
|||
Loading…
Reference in New Issue