Add proper external data API errors handling
This commit is contained in:
parent
e5a9e90304
commit
9f8c55969d
1 changed files with 7 additions and 2 deletions
|
@ -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;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue