Skip to content

Commit

Permalink
Error handling search (#181)
Browse files Browse the repository at this point in the history
* Don't fetch person info when developing locally

* Better error handling for search
  • Loading branch information
stephenramthun authored Sep 2, 2019
1 parent 7f7778a commit 1f8ac08
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 27 deletions.
8 changes: 6 additions & 2 deletions src/components/Search/Search.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -79,8 +79,12 @@ const Search = ({ history }) => {
</button>
{behandlingerCtx.error && (
<ErrorModal
errorMessage={behandlingerCtx.error}
onClose={() => behandlingerCtx.clearError()}
errorMessage={behandlingerCtx.error.message}
onClose={
behandlingerCtx.error.statusCode !== 401
? () => behandlingerCtx.clearError()
: undefined
}
/>
)}
</div>
Expand Down
9 changes: 6 additions & 3 deletions src/components/widgets/Personinfo/Personinfo.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand Down
27 changes: 16 additions & 11 deletions src/context/BehandlingerContext.js
Original file line number Diff line number Diff line change
Expand Up @@ -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.'
});
}
});
};

Expand Down
12 changes: 6 additions & 6 deletions src/io/http.js
Original file line number Diff line number Diff line change
@@ -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 */
Expand All @@ -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 {
Expand All @@ -46,7 +46,7 @@ export const putFeedback = async feedback => {
});

if (response.status !== 204) {
throw ResponseError(response);
throw ResponseError(response.statusText, response.status);
}
};

Expand Down
12 changes: 7 additions & 5 deletions src/io/http.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});

0 comments on commit 1f8ac08

Please sign in to comment.