Skip to content

Commit

Permalink
More tests, simpler error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
eyeseast committed Apr 23, 2024
1 parent 4ebed9a commit 5dcf9ae
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 17 deletions.
10 changes: 10 additions & 0 deletions src/lib/api/documents.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,16 @@ describe("document uploads and processing", () => {
expect(p).toMatchObject(pending);
});
});

test("pending error", async () => {
const mockFetch = vi.fn().mockImplementation(async () => {
throw new Error("Bad fetch");
});

documents.pending(mockFetch).then((p) => {
expect(p).toEqual([]);
});
});
});

describe("document helper methods", () => {
Expand Down
23 changes: 13 additions & 10 deletions src/lib/api/documents.ts
Original file line number Diff line number Diff line change
Expand Up @@ -163,19 +163,22 @@ export async function process(
*/
export async function cancel(id: number | string) {}

/**
* Get pending documents. This returns an empty array for any error.
*
* @param {fetch} fetch
* @returns {Promise<Pending>}
*/
export async function pending(fetch = globalThis.fetch): Promise<Pending[]> {
const endpoint = new URL("documents/pending/", BASE_API_URL);

// if this request fails for any reason, just return nothing
const resp = await fetch(endpoint, { credentials: "include" }).catch((e) => ({
ok: false,
json() {
return [];
},
}));

// if there's an error above, this will return an empty array
return resp.json();
try {
const resp = await fetch(endpoint, { credentials: "include" });
if (isErrorCode(resp.status)) return [];
return resp.json();
} catch (e) {
return [];
}
}

// utility functions
Expand Down
4 changes: 3 additions & 1 deletion src/lib/components/documents/ResultsList.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@
</script>

<div class="container">
<slot />
<slot name="start" />
{#each results as document (document.id)}
<Flex direction="column">
<Flex gap={0.625} align="center">
Expand Down Expand Up @@ -131,6 +131,8 @@
</Button>
{/if}
</div>

<slot name="end" />
</div>

<style>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
<Story name="Pending documents">
<div style="width: 36rem">
<ResultsList {results} {count} {next}>
<Pending {pending} />
<Pending {pending} slot="start" />
</ResultsList>
</div>
</Story>
Expand Down
12 changes: 7 additions & 5 deletions src/routes/app/+page.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -51,16 +51,18 @@
{#await searchResults}
<Empty icon={Hourglass24}>Loading…</Empty>
{:then results}
{#await pending then p}
<Pending pending={p} />
{/await}

<ResultsList
results={results.results}
count={results.count}
next={results.next}
auto
/>
>
<svelte:fragment slot="start">
{#await pending then p}
<Pending pending={p} />
{/await}
</svelte:fragment>
</ResultsList>
{/await}

<PageToolbar slot="footer">
Expand Down

0 comments on commit 5dcf9ae

Please sign in to comment.