This commit is contained in:
2019-07-24 15:09:00 +00:00
parent 604fe1dd72
commit 565a092251
10 changed files with 77 additions and 76 deletions

View File

@@ -1,11 +1,6 @@
/* Xeslint-disable */
import _ from "../libs/lodash.min.js";
import store from "../store";
import router from "../router";
import auth from "./auth";
import errorHandler from "./errorhandler";
import gzevent from "./eventbus";
import gzlocale from "./locale";
function stringifyPrimitive(v) {
switch (typeof v) {
@@ -27,13 +22,13 @@ function stringifyPrimitive(v) {
// Show unexpected errors during development
//
function devShowUnknownError(error) {
if (errorHandler.devMode) {
if (window.$gz.errorHandler.devMode) {
// eslint-disable-next-line
console.log("gzapi::devShowUnknownError, error is:");
// eslint-disable-next-line
console.log(error);
gzevent.$emit(
window.$gz.eventBus.$emit(
"notify-warning",
"DEV ERROR gzapi::devShowUnknownError - unexpected error during api operation see console "
);
@@ -47,7 +42,7 @@ function devShowUnknownError(error) {
function handleError(action, error, route, reject) {
var errorMessage =
"API error: " + action + " route =" + route + ", message =" + error.message;
store.commit("logItem", errorMessage);
window.$gz.store.commit("logItem", errorMessage);
//Handle 403 not authorized
//popup not authorized, log, then go to HOME
@@ -55,16 +50,25 @@ function handleError(action, error, route, reject) {
//reaction to directly entered or opened link, not application logic driving it, so home is safest choice
//
if (error.message && error.message.includes("NotAuthorized")) {
store.commit("logItem", "Not authorized, redirecting to HOME");
gzevent.$emit("notify-warning", gzlocale.get("ErrorUserNotAuthorized"));
window.$gz.store.commit("logItem", "Not authorized, redirecting to HOME");
window.$gz.eventBus.$emit(
"notify-warning",
window.$gz.locale.get("ErrorUserNotAuthorized")
);
router.push("/");
return reject("[ErrorUserNotAuthorized]");
}
//Handle 401 not authenticated
if (error.message && error.message.includes("NotAuthenticated")) {
store.commit("logItem", "User is not authenticated, redirecting to LOGIN");
gzevent.$emit("notify-error", gzlocale.get("ErrorUserNotAuthenticated"));
window.$gz.store.commit(
"logItem",
"User is not authenticated, redirecting to LOGIN"
);
window.$gz.eventBus.$emit(
"notify-error",
window.$gz.locale.get("ErrorUserNotAuthenticated")
);
auth.logout();
router.push("/login");
return reject("[ErrorUserNotAuthenticated]");
@@ -78,7 +82,7 @@ function handleError(action, error, route, reject) {
error.message.includes("NetworkError") ||
error.message.includes("Network request failed")
) {
store.commit("logItem", "Network error");
window.$gz.store.commit("logItem", "Network error");
//note: using locale key in square brackets
return reject("[ErrorServerUnresponsive]");
//throw "Error: unable to contact server";
@@ -105,7 +109,7 @@ export default {
return Promise.resolve(response);
} else {
//log unhandled api error
store.commit(
window.$gz.store.commit(
"logItem",
"API error: status=" +
response.status +
@@ -134,14 +138,14 @@ export default {
//Accept: "application/json, text/plain, */*",
Accept: "application/json",
"Content-Type": "application/json-patch+json",
Authorization: "Bearer " + store.state.apiToken
Authorization: "Bearer " + window.$gz.store.state.apiToken
};
},
postAuthorizedHeaders() {
return {
Accept: "application/json",
"Content-Type": "application/json",
Authorization: "Bearer " + store.state.apiToken
Authorization: "Bearer " + window.$gz.store.state.apiToken
};
},
postUnAuthorizedHeaders() {
@@ -191,7 +195,7 @@ export default {
};
},
APIUrl(apiPath) {
if ("" == store.state.apiUrl) {
if ("" == window.$gz.store.state.apiUrl) {
//construct the api url and store it
//development location?
if (
@@ -199,29 +203,30 @@ export default {
window.location.hostname == "192.168.1.56") &&
window.location.port == "8080"
) {
store.commit("setAPIURL", "http://localhost:7575/api/v8.0/");
store.commit("setHelpURL", "http://localhost:7575/docs/");
store.commit(
window.$gz.store.commit("setAPIURL", "http://localhost:7575/api/v8.0/");
window.$gz.store.commit("setHelpURL", "http://localhost:7575/docs/");
window.$gz.store.commit(
"logItem",
"gzapi::APIUrl -> setting to dev. mode: " + store.state.apiUrl
"gzapi::APIUrl -> setting to dev. mode: " +
window.$gz.store.state.apiUrl
);
} else {
//production location <protocol>//<hostname>:<port>/
store.commit(
window.$gz.store.commit(
"setHelpURL",
window.location.protocol + "//" + window.location.host + "/docs/"
);
store.commit(
window.$gz.store.commit(
"setAPIURL",
window.location.protocol + "//" + window.location.host + "/api/v8.0/"
);
store.commit(
window.$gz.store.commit(
"logItem",
"gzapi::APIUrl -> setting to: " + store.state.apiUrl
"gzapi::APIUrl -> setting to: " + window.$gz.store.state.apiUrl
);
}
}
return store.state.apiUrl + apiPath;
return window.$gz.store.state.apiUrl + apiPath;
},
/////////////////////////////
// REPLACE END OF URL
@@ -297,7 +302,7 @@ export default {
//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 (_.endsWith(route, "/0")) {
if (window.$gz._.endsWith(route, "/0")) {
route = route.slice(0, -2);
}
}