|
|
|
|
@@ -4,7 +4,7 @@ import router from "../router";
|
|
|
|
|
import auth from "./auth";
|
|
|
|
|
import errorHandler from "./errorhandler";
|
|
|
|
|
|
|
|
|
|
var stringifyPrimitive = function(v) {
|
|
|
|
|
function stringifyPrimitive(v) {
|
|
|
|
|
switch (typeof v) {
|
|
|
|
|
case "string":
|
|
|
|
|
return v;
|
|
|
|
|
@@ -18,20 +18,55 @@ var stringifyPrimitive = function(v) {
|
|
|
|
|
default:
|
|
|
|
|
return "";
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var devShowUnknownError = function(error) {
|
|
|
|
|
/////////////////////////////////////////////////
|
|
|
|
|
// Show unexpected errors during development
|
|
|
|
|
//
|
|
|
|
|
function devShowUnknownError(error) {
|
|
|
|
|
if (errorHandler.devMode) {
|
|
|
|
|
// eslint-disable-next-line
|
|
|
|
|
console.log("apiutil::devShowUnknownError, error is:");
|
|
|
|
|
console.log("gzapi::devShowUnknownError, error is:");
|
|
|
|
|
// eslint-disable-next-line
|
|
|
|
|
console.log(error);
|
|
|
|
|
// eslint-disable-next-line
|
|
|
|
|
alert(
|
|
|
|
|
"DEV ERROR apiutil::devShowUnknownError - unexpected error during api operation see console "
|
|
|
|
|
"DEV ERROR gzapi::devShowUnknownError - unexpected error during api operation see console "
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
////////////////////////////////////////////
|
|
|
|
|
// Try to handle an api error
|
|
|
|
|
// return true if handled or false if not
|
|
|
|
|
//
|
|
|
|
|
function handleError(action, error, route, reject) {
|
|
|
|
|
var errorMessage =
|
|
|
|
|
"API error: " + action + " route =" + route + ", message =" + error.message;
|
|
|
|
|
store.commit("logItem", errorMessage);
|
|
|
|
|
|
|
|
|
|
if (error.message && error.message.includes("401")) {
|
|
|
|
|
store.commit("logItem", "User is not authorized, redirecting to login");
|
|
|
|
|
auth.logout();
|
|
|
|
|
router.push("/login");
|
|
|
|
|
return reject("Authorization required");
|
|
|
|
|
}
|
|
|
|
|
//is it a network error?
|
|
|
|
|
//https://medium.com/@vinhlh/how-to-handle-networkerror-when-using-fetch-ff2663220435
|
|
|
|
|
if (error instanceof TypeError) {
|
|
|
|
|
if (
|
|
|
|
|
error.message.includes("Failed to fetch") ||
|
|
|
|
|
error.message.includes("NetworkError") ||
|
|
|
|
|
error.message.includes("Network request failed")
|
|
|
|
|
) {
|
|
|
|
|
store.commit("logItem", "Network error");
|
|
|
|
|
return reject("Error: unable to contact server");
|
|
|
|
|
//throw "Error: unable to contact server";
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
//Ideally this should never get called because any issue should be addressed above
|
|
|
|
|
devShowUnknownError(error);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export default {
|
|
|
|
|
status(response) {
|
|
|
|
|
@@ -135,7 +170,7 @@ export default {
|
|
|
|
|
store.commit("setHelpURL", "http://localhost:7575/docs/");
|
|
|
|
|
store.commit(
|
|
|
|
|
"logItem",
|
|
|
|
|
"apiutil::APIUrl -> setting to dev. mode: " + store.state.apiUrl
|
|
|
|
|
"gzapi::APIUrl -> setting to dev. mode: " + store.state.apiUrl
|
|
|
|
|
);
|
|
|
|
|
} else {
|
|
|
|
|
//production location <protocol>//<hostname>:<port>/
|
|
|
|
|
@@ -149,7 +184,7 @@ export default {
|
|
|
|
|
);
|
|
|
|
|
store.commit(
|
|
|
|
|
"logItem",
|
|
|
|
|
"apiutil::APIUrl -> setting to: " + store.state.apiUrl
|
|
|
|
|
"gzapi::APIUrl -> setting to: " + store.state.apiUrl
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
@@ -204,23 +239,12 @@ export default {
|
|
|
|
|
})
|
|
|
|
|
.catch(function(error) {
|
|
|
|
|
//fundamental error, can't proceed with this call
|
|
|
|
|
|
|
|
|
|
var errorMessage =
|
|
|
|
|
"API error: GET route =" + route + ", message =" + error.message;
|
|
|
|
|
store.commit("logItem", errorMessage);
|
|
|
|
|
|
|
|
|
|
if (error.message && error.message.includes("401")) {
|
|
|
|
|
store.commit(
|
|
|
|
|
"logItem",
|
|
|
|
|
"User is not authorized, redirecting to login"
|
|
|
|
|
);
|
|
|
|
|
auth.logout();
|
|
|
|
|
router.push("/login");
|
|
|
|
|
} else {
|
|
|
|
|
//This should never get called because any issue should be addressed above in a proper error handler
|
|
|
|
|
devShowUnknownError(error);
|
|
|
|
|
reject(error);
|
|
|
|
|
}
|
|
|
|
|
handleError("GET", error, route, reject);
|
|
|
|
|
// {
|
|
|
|
|
// //Ideally this should never get called because any issue should be addressed above by errorHandler
|
|
|
|
|
// devShowUnknownError(error);
|
|
|
|
|
// reject(error);
|
|
|
|
|
// }
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
},
|
|
|
|
|
@@ -247,24 +271,13 @@ export default {
|
|
|
|
|
resolve(response);
|
|
|
|
|
})
|
|
|
|
|
.catch(function(error) {
|
|
|
|
|
handleError("UPSERT", error, route, reject);
|
|
|
|
|
//fundamental error, can't proceed with this call
|
|
|
|
|
|
|
|
|
|
var errorMessage =
|
|
|
|
|
"API error: UPSERT route =" + route + ", message =" + error.message;
|
|
|
|
|
store.commit("logItem", errorMessage);
|
|
|
|
|
|
|
|
|
|
if (error.message && error.message.includes("401")) {
|
|
|
|
|
store.commit(
|
|
|
|
|
"logItem",
|
|
|
|
|
"User is not authorized, redirecting to login"
|
|
|
|
|
);
|
|
|
|
|
auth.logout();
|
|
|
|
|
router.push("/login");
|
|
|
|
|
} else {
|
|
|
|
|
//This should never get called because any issue should be addressed above in a proper error handler
|
|
|
|
|
devShowUnknownError(error);
|
|
|
|
|
reject(error);
|
|
|
|
|
}
|
|
|
|
|
// if (!handleError("UPSERT", error, route)) {
|
|
|
|
|
// //This should (ideally) never get called because any issue should be addressed above by handleError
|
|
|
|
|
// devShowUnknownError(error);
|
|
|
|
|
// reject(error);
|
|
|
|
|
// }
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
}
|