Skip to content

Commit

Permalink
try catch for http and network errors
Browse files Browse the repository at this point in the history
  • Loading branch information
jalowe13 committed Nov 27, 2024
1 parent 60117a9 commit 90aabb2
Showing 1 changed file with 15 additions and 12 deletions.
27 changes: 15 additions & 12 deletions client/src/redux/sagas/datafiles.sagas.js
Original file line number Diff line number Diff line change
Expand Up @@ -485,20 +485,23 @@ export async function uploadFileUtil(api, scheme, system, path, file) {
`/api/datafiles/${api}/upload/${scheme}/${system}/${apiPath}/`
);

const request = await fetch(url, {
method: 'POST',
headers: { 'X-CSRFToken': Cookies.get('csrftoken') },
credentials: 'same-origin',
body: formData,
});
if (!request.ok) {
if (request.status === 403 || request.status === 500) {
const responseText = await request.text();
throw new Error(responseText);
try {
const request = await fetch(url, {
method: 'POST',
headers: { 'X-CSRFToken': Cookies.get('csrftoken') },
credentials: 'same-origin',
body: formData,
});
if (!request.ok) {
throw new Error(`HTTP error: ${request.status}`);
}
throw new Error(request.status);
return request;
} catch (error) {
if (error instanceof TypeError) {
throw new Error('Network error: The file upload was blocked.');
}
throw error;
}
return request;
}

export function* watchUpload() {
Expand Down

0 comments on commit 90aabb2

Please sign in to comment.