From 9f8c55969d3f96de1773624891bd1aae2c90f05e Mon Sep 17 00:00:00 2001 From: Paul Chavard Date: Wed, 3 Mar 2021 16:42:29 +0100 Subject: [PATCH] Add proper external data API errors handling --- app/javascript/components/shared/queryClient.js | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/app/javascript/components/shared/queryClient.js b/app/javascript/components/shared/queryClient.js index 82baed60a..8d64f0689 100644 --- a/app/javascript/components/shared/queryClient.js +++ b/app/javascript/components/shared/queryClient.js @@ -14,7 +14,7 @@ export const queryClient = new QueryClient({ }); function buildURL(scope, term) { - term = encodeURIComponent(term); + term = encodeURIComponent(term.replace(/\(|\)/g, '')); if (scope === 'adresse') { return `${api_adresse_url}/search?q=${term}&limit=5`; } else if (scope === 'annuaire-education') { @@ -42,7 +42,12 @@ async function defaultQueryFn({ queryKey: [scope, term] }) { const url = buildURL(scope, term); 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(); return promise; }