all over the place with comments and console statements but now properly chains all the calls for login (except translation may have an issue)

This commit is contained in:
2020-06-10 21:05:45 +00:00
parent c0fcecb3f8
commit 0a43e53ab7
25 changed files with 1966 additions and 976 deletions

View File

@@ -150,6 +150,63 @@ export default {
return Promise.resolve(response);
}
},
statusEx(response) {
//Handle expected api errors
if (response.status == 401) {
//must reject if not Authenticated
throw new Error("[ErrorUserNotAuthenticated]");
}
if (response.status == 403) {
//must reject if not Authorized
throw new Error("[ErrorUserNotAuthorized]");
}
//404 not found is an expected status not worth logging allow to bubble up
//for client code to deal with
if (response.status == 404) {
return;
}
if (response.status == 405) {
//Probably a development error
throw new Error("Method Not Allowed (route issue?) " + response.url);
}
if (response.status >= 200 && response.status < 300) {
return;
} else {
//log unhandled api error
window.$gz.store.commit(
"logItem",
"API error: status=" +
response.status +
", statusText=" +
response.statusText +
", url=" +
response.url
);
}
},
async extractBodyEx(response) {
if (response.status == 204) {
//no content, nothing to process
return response;
}
let contentType = response.headers.get("content-type");
if (!contentType) {
return response;
}
//console.log("gzapi::extractBody method, content type is:", contentType);
if (contentType.includes("json")) {
return await response.json();
}
if (contentType.includes("text/plain")) {
return await response.text();
}
return response;
},
extractBody(response) {
if (response.status == 204) {
//no content, nothing to process
@@ -372,9 +429,58 @@ export default {
});
});
},
//////////////////////////////////////
// Test delay for troubleshooting
//
doDelayAsync: () => {
return new Promise((resolve) => {
setTimeout(() => resolve("I did something"), 10000);
});
},
///////////////////////////////////
// POST / PUT DATA TO API SERVER
//
async upsertEx(route, data) {
try {
console.log("gzapi:upsertEx TOP");
let that = this;
// return new Promise(function upsertDataToServer(resolve, reject) {
//determine if this is a new or existing record
let fetchOptions = undefined;
if (data.concurrency) {
//has concurrency token, so this is a PUT as it's updating an existing record
fetchOptions = that.fetchPutOptions(data);
} else {
//Does not have a concurrency token so this is a POST as it's posting a new record without a concurrency token
fetchOptions = that.fetchPostOptions(data);
//ensure the route doesn't end in /0 which will happen if it's a new record since the edit forms just send the url here with the ID regardless
if (window.$gz._.endsWith(route, "/0")) {
route = route.slice(0, -2);
}
}
// console.log("** gzapi:upsertEx calling test delay");
// await this.doDelayAsync();
// console.log("** gzapi:upsertEx back from delay continuing..");
console.log("gzapi:upsertEx calling fetch");
let r = await fetch(that.APIUrl(route), fetchOptions);
console.log("gzapi:upsertEx calling statusEx");
that.statusEx(r);
console.log("gzapi:upsertEx calling extractBodyEx");
r = await that.extractBodyEx(r);
console.log("gzapi:upsertEx done, returning response");
return r;
// eslint-disable-next-line
// .then((response) => {
// //Note: response.error indicates there is an error, however this is not an unusual condition
// //it could be validation errors or other general error so we need to treat it here like it's normal
// //and let the caller deal with it appropriately
// resolve(response);
// })
} catch (error) {
handleError("UPSERT", error, route, reject);
}
// });
},
upsert(route, data) {
let that = this;
return new Promise(function upsertDataToServer(resolve, reject) {