Skip to content

Commit

Permalink
Improved error handling on ditto calls
Browse files Browse the repository at this point in the history
Signed-off-by: Thomas Fries <thomas.fries0@gmail.com>
  • Loading branch information
thfries committed Jun 24, 2022
1 parent 443df8d commit 77de6a4
Showing 1 changed file with 27 additions and 22 deletions.
49 changes: 27 additions & 22 deletions ui/modules/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,27 +44,32 @@ export function setAuthHeader(forDevOps) {
* @return {Object} result as json object
*/
export async function callDittoREST(method, path, body) {
const response = await fetch(Environments.current().api_uri + '/api/2' + path, {
method: method,
headers: {
'Content-Type': 'application/json',
'Authorization': authHeader,
},
body: JSON.stringify(body),
});
if (!response.ok) {
response.json()
.then((dittoErr) => {
Utils.showError(dittoErr.message, dittoErr.error, dittoErr.status);
})
.catch((err) => {
Utils.showError('No error details from ditto', response.statusText, response.status);
});
throw new Error('An error occured: ' + response.status);
}
if (response.status != 204) {
return response.json();
} else {
return null;
try {
const response = await fetch(Environments.current().api_uri + '/api/2' + path, {
method: method,
headers: {
'Content-Type': 'application/json',
'Authorization': authHeader,
},
body: JSON.stringify(body),
});
if (!response.ok) {
response.json()
.then((dittoErr) => {
Utils.showError(dittoErr.message, dittoErr.error, dittoErr.status);
})
.catch((err) => {
Utils.showError('No error details from ditto', response.statusText, response.status);
});
throw new Error('An error occured: ' + response.status);
}
if (response.status != 204) {
return response.json();
} else {
return null;
}
} catch (err) {
Utils.showError(err);
throw err;
}
}

0 comments on commit 77de6a4

Please sign in to comment.