Cleanup AbstractFetch

and added fetchJSON mockup code for error handling regarding issue #49
This commit is contained in:
djmaze 2021-01-05 13:58:50 +01:00
parent 4723e1241e
commit fa7ea413dc
2 changed files with 176 additions and 172 deletions

35
dev/bootstrap.js vendored
View file

@ -90,15 +90,13 @@ export default (App) => {
cache: 'no-cache',
redirect: 'error',
referrerPolicy: 'no-referrer',
credentials: 'same-origin'
credentials: 'same-origin',
headers: {}
}, init);
init.headers.Accept = 'application/json';
if (postData) {
init.method = 'POST';
init.headers = {
// 'Content-Type': 'application/json'
'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8'
};
init.headers['Content-Type'] = 'application/x-www-form-urlencoded; charset=UTF-8';
postData.XToken = rl.settings.app('token');
// init.body = JSON.stringify(postData);
const formData = new FormData(),
@ -115,7 +113,30 @@ export default (App) => {
init.body = new URLSearchParams(formData);
}
return fetch(resource, init).then(response => response.json());
return fetch(resource, init).then(response => {
if (!response.ok) {
// return Promise.reject('Network response error: ' + response.status);
throw new Error('Network response error: ' + response.status);
}
/* TODO: use this for non-developers?
response.clone()
let data = response.text();
try {
return JSON.parse(data);
} catch (e) {
console.error(e);
// console.log(data);
return Promise.reject(Notification.JsonParse);
return {
Result: false,
ErrorCode: 952, // Notification.JsonParse
ErrorMessage: e.message,
ErrorMessageAdditional: data
}
}
*/
return response.json();
});
};
window.__APP_BOOT = fErrorCallback => {