diff --git a/src/components/Search/Search.jsx b/src/components/Search/Search.jsx index 28d4f3284..7185fd113 100644 --- a/src/components/Search/Search.jsx +++ b/src/components/Search/Search.jsx @@ -79,8 +79,12 @@ const Search = ({ history }) => { {behandlingerCtx.error && ( behandlingerCtx.clearError()} + errorMessage={behandlingerCtx.error.message} + onClose={ + behandlingerCtx.error.statusCode !== 401 + ? () => behandlingerCtx.clearError() + : undefined + } /> )} diff --git a/src/components/widgets/Personinfo/Personinfo.jsx b/src/components/widgets/Personinfo/Personinfo.jsx index dd3bd63cf..aca8007a5 100644 --- a/src/components/widgets/Personinfo/Personinfo.jsx +++ b/src/components/widgets/Personinfo/Personinfo.jsx @@ -20,9 +20,12 @@ const Personinfo = withBehandlingContext(({ behandling }) => { }); useEffect(() => { - getPerson(aktorId).then(response => { - response.data && setPerson(response.data); - }); + // eslint-disable-next-line no-undef + if (process.env.NODE_ENV !== 'development') { + getPerson(aktorId).then(response => { + response.data && setPerson(response.data); + }); + } }, []); return ( diff --git a/src/context/BehandlingerContext.js b/src/context/BehandlingerContext.js index 96d77a708..a1d8ab71e 100644 --- a/src/context/BehandlingerContext.js +++ b/src/context/BehandlingerContext.js @@ -24,19 +24,24 @@ export const BehandlingerProvider = ({ children }) => { const fetchBehandlinger = value => { return behandlingerFor(value) .then(response => { - if (response.status === 200) { - const newData = { behandlinger: response.data }; - setBehandlinger(newData); - return newData; - } else if (response.status === 401) { - setError('Du må logge inn på nytt.'); - } else { - setError(`Fant ingen behandlinger for '${value}'`); - } + const newData = { behandlinger: response.data }; + setBehandlinger(newData); + return newData; }) .catch(err => { - console.log(err); // eslint-disable-line no-console - setError('Kunne ikke utføre søket'); + if (err.statusCode === 401) { + setError({ ...err, message: 'Du må logge inn på nytt.' }); + } else if (err.statusCode === 404) { + setError({ + ...err, + message: `Fant ingen behandlinger for ${value}.` + }); + } else { + setError({ + ...err, + message: 'Kunne ikke utføre søket. Prøv igjen senere.' + }); + } }); }; diff --git a/src/io/http.js b/src/io/http.js index 5d97416db..ffb98f300 100644 --- a/src/io/http.js +++ b/src/io/http.js @@ -1,8 +1,8 @@ 'use strict'; -const ResponseError = response => ({ - message: response.statusText, - statusCode: response.status +export const ResponseError = (message, statusCode) => ({ + message, + statusCode }); /* eslint-disable no-undef */ @@ -21,8 +21,8 @@ const getData = async response => { const get = async url => { const response = await fetch(url); - if (!response) { - throw Error(); + if (response.status >= 400) { + throw ResponseError(response.statusText, response.status); } return { @@ -46,7 +46,7 @@ export const putFeedback = async feedback => { }); if (response.status !== 204) { - throw ResponseError(response); + throw ResponseError(response.statusText, response.status); } }; diff --git a/src/io/http.test.js b/src/io/http.test.js index 7bc701c7c..11bf6e0a0 100644 --- a/src/io/http.test.js +++ b/src/io/http.test.js @@ -28,10 +28,12 @@ test('behandlinger ikke funnet', async () => { } }); }); - const response = await behandlingerFor(12345); - expect(fetch).toHaveBeenCalledTimes(1); - expect(response).toEqual({ - data: undefined, - status: 404 + const response = await behandlingerFor(12345).catch(err => { + expect(err).toEqual({ + message: undefined, + statusCode: 404 + }); }); + expect(fetch).toHaveBeenCalledTimes(1); + expect(response).toEqual(undefined); });