Skip to content

Commit 0c6cfdb

Browse files
committed
feat: Add error handling to useCollections
1 parent d9f858c commit 0c6cfdb

File tree

2 files changed

+46
-2
lines changed

2 files changed

+46
-2
lines changed

src/hooks/useCollections.test.ts

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,44 @@ describe('useCollections', () => {
2424
expect(result.current.state).toEqual('IDLE');
2525
});
2626

27+
it('handles error with JSON response', async () => {
28+
fetch
29+
.mockResponseOnce(JSON.stringify({ links: [] }), { url: 'https://fake-stac-api.net' })
30+
.mockResponseOnce(JSON.stringify({ error: 'Wrong query' }), { status: 400, statusText: 'Bad Request' });
31+
32+
const { result, waitForNextUpdate } = renderHook(
33+
() => useCollections(),
34+
{ wrapper }
35+
);
36+
37+
await waitForNextUpdate();
38+
await waitForNextUpdate();
39+
40+
expect(result.current.error).toEqual({
41+
status: 400,
42+
statusText: 'Bad Request',
43+
detail: { error: 'Wrong query' }
44+
});
45+
});
46+
47+
it('handles error with non-JSON response', async () => {
48+
fetch
49+
.mockResponseOnce(JSON.stringify({ links: [] }), { url: 'https://fake-stac-api.net' })
50+
.mockResponseOnce('Wrong query', { status: 400, statusText: 'Bad Request' });
51+
52+
const { result, waitForNextUpdate } = renderHook(
53+
() => useCollections(),
54+
{ wrapper }
55+
);
56+
await waitForNextUpdate();
57+
await waitForNextUpdate();
58+
expect(result.current.error).toEqual({
59+
status: 400,
60+
statusText: 'Bad Request',
61+
detail: 'Wrong query'
62+
});
63+
});
64+
2765
it('reloads collections', async () => {
2866
fetch
2967
.mockResponseOnce(JSON.stringify({ links: [] }), { url: 'https://fake-stac-api.net' })
@@ -40,6 +78,7 @@ describe('useCollections', () => {
4078

4179
expect(result.current.state).toEqual('IDLE');
4280
act(() => result.current.reload());
81+
4382
await waitForNextUpdate();
4483
expect(result.current.collections).toEqual({ data: 'reloaded' });
4584
});

src/hooks/useCollections.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { useCallback, useEffect, useState, useMemo } from 'react';
2-
import type { LoadingState } from '../types';
2+
import { type ApiError, type LoadingState } from '../types';
33
import type { CollectionsResponse } from '../types/stac';
44
import debounce from '../utils/debounce';
55
import { useStacApiContext } from '../context';
@@ -8,11 +8,14 @@ type StacCollectionsHook = {
88
collections?: CollectionsResponse,
99
reload: () => void,
1010
state: LoadingState
11+
error?: ApiError
1112
};
1213

1314
function useCollections(): StacCollectionsHook {
1415
const { stacApi, collections, setCollections } = useStacApiContext();
1516
const [ state, setState ] = useState<LoadingState>('IDLE');
17+
const [ error, setError ] = useState<ApiError>();
18+
1619

1720
const _getCollections = useCallback(
1821
() => {
@@ -23,6 +26,7 @@ function useCollections(): StacCollectionsHook {
2326
stacApi.getCollections()
2427
.then(response => response.json())
2528
.then(setCollections)
29+
.catch((err) => setError(err))
2630
.finally(() => setState('IDLE'));
2731
}
2832
},
@@ -42,7 +46,8 @@ function useCollections(): StacCollectionsHook {
4246
return {
4347
collections,
4448
reload: getCollections,
45-
state
49+
state,
50+
error,
4651
};
4752
}
4853

0 commit comments

Comments
 (0)