Add proper external data API errors handling

This commit is contained in:
Paul Chavard 2021-03-03 16:42:29 +01:00
parent e5a9e90304
commit 9f8c55969d

View file

@ -14,7 +14,7 @@ export const queryClient = new QueryClient({
}); });
function buildURL(scope, term) { function buildURL(scope, term) {
term = encodeURIComponent(term); term = encodeURIComponent(term.replace(/\(|\)/g, ''));
if (scope === 'adresse') { if (scope === 'adresse') {
return `${api_adresse_url}/search?q=${term}&limit=5`; return `${api_adresse_url}/search?q=${term}&limit=5`;
} else if (scope === 'annuaire-education') { } else if (scope === 'annuaire-education') {
@ -42,7 +42,12 @@ async function defaultQueryFn({ queryKey: [scope, term] }) {
const url = buildURL(scope, term); const url = buildURL(scope, term);
const [options, controller] = buildOptions(); const [options, controller] = buildOptions();
const promise = fetch(url, options).then((response) => response.json()); const promise = fetch(url, options).then((response) => {
if (response.ok) {
return response.json();
}
throw new Error(`Error fetching from "${scope}" API`);
});
promise.cancel = () => controller && controller.abort(); promise.cancel = () => controller && controller.abort();
return promise; return promise;
} }