Skip to content

Commit 025023b

Browse files
committed
feat: Add error handling for useCollection
1 parent e57e3a0 commit 025023b

File tree

2 files changed

+86
-8
lines changed

2 files changed

+86
-8
lines changed

src/hooks/useCollection.test.ts

Lines changed: 63 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,11 @@ describe('useCollection' ,() => {
1111
it('queries collection', async () => {
1212
fetch
1313
.mockResponseOnce(JSON.stringify({ links: [] }), { url: 'https://fake-stac-api.net' })
14-
.mockResponseOnce(JSON.stringify({
14+
.mockResponseOnce(JSON.stringify({
1515
collections: [
1616
{id: 'abc', title: 'Collection A'},
1717
{id: 'def', title: 'Collection B'}
18-
]
18+
]
1919
}));
2020

2121
const { result, waitForNextUpdate } = renderHook(
@@ -27,4 +27,65 @@ describe('useCollection' ,() => {
2727
expect(result.current.collection).toEqual({id: 'abc', title: 'Collection A'});
2828
expect(result.current.state).toEqual('IDLE');
2929
});
30+
31+
it('returns error if collection does not exist', async () => {
32+
fetch
33+
.mockResponseOnce(JSON.stringify({ links: [] }), { url: 'https://fake-stac-api.net' })
34+
.mockResponseOnce(JSON.stringify({
35+
collections: [
36+
{id: 'abc', title: 'Collection A'},
37+
{id: 'def', title: 'Collection B'}
38+
]
39+
}));
40+
41+
const { result, waitForNextUpdate } = renderHook(
42+
() => useCollection('ghi'),
43+
{ wrapper }
44+
);
45+
await waitForNextUpdate();
46+
await waitForNextUpdate();
47+
expect(result.current.error).toEqual({
48+
status: 404,
49+
statusText: 'Not found',
50+
detail: 'Collection does not exist'
51+
});
52+
});
53+
54+
it('handles error with JSON response', async () => {
55+
fetch
56+
.mockResponseOnce(JSON.stringify({ links: [] }), { url: 'https://fake-stac-api.net' })
57+
.mockResponseOnce(JSON.stringify({ error: 'Wrong query' }), { status: 400, statusText: 'Bad Request' });
58+
59+
const { result, waitForNextUpdate } = renderHook(
60+
() => useCollection('abc'),
61+
{ wrapper }
62+
);
63+
await waitForNextUpdate();
64+
await waitForNextUpdate();
65+
66+
expect(result.current.error).toEqual({
67+
status: 400,
68+
statusText: 'Bad Request',
69+
detail: { error: 'Wrong query' }
70+
});
71+
});
72+
73+
it('handles error with non-JSON response', async () => {
74+
fetch
75+
.mockResponseOnce(JSON.stringify({ links: [] }), { url: 'https://fake-stac-api.net' })
76+
.mockResponseOnce('Wrong query', { status: 400, statusText: 'Bad Request' });
77+
78+
const { result, waitForNextUpdate } = renderHook(
79+
() => useCollection('abc'),
80+
{ wrapper }
81+
);
82+
await waitForNextUpdate();
83+
await waitForNextUpdate();
84+
85+
expect(result.current.error).toEqual({
86+
status: 400,
87+
statusText: 'Bad Request',
88+
detail: 'Wrong query'
89+
});
90+
});
3091
});

src/hooks/useCollection.ts

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,42 @@
1-
import { useMemo } from 'react';
1+
import { useMemo, useState, useEffect } from 'react';
22

3-
import type { LoadingState } from '../types';
3+
import type { ApiError, LoadingState } from '../types';
44
import type { Collection } from '../types/stac';
55
import useCollections from './useCollections';
66

77
type StacCollectionHook = {
88
collection?: Collection,
9-
state: LoadingState
9+
state: LoadingState,
10+
error?: ApiError
1011
};
1112

1213
function useCollection(collectionId: string): StacCollectionHook {
13-
const { collections, state } = useCollections();
14+
const { collections, state, error: requestError } = useCollections();
15+
const [ error, setError ] = useState<ApiError>();
16+
17+
useEffect(() => {
18+
setError(requestError);
19+
}, [requestError]);
1420

1521
const collection = useMemo(
16-
() => collections?.collections.find(({ id }) => id === collectionId),
22+
() => {
23+
const coll = collections?.collections.find(({ id }) => id === collectionId);
24+
if (!coll) {
25+
setError({
26+
status: 404,
27+
statusText: 'Not found',
28+
detail: 'Collection does not exist'
29+
});
30+
}
31+
return coll;
32+
},
1733
[collectionId, collections]
1834
);
1935

2036
return {
2137
collection,
22-
state
38+
state,
39+
error
2340
};
2441
}
2542

0 commit comments

Comments
 (0)