diff --git a/ayanova/src/api/auth.js b/ayanova/src/api/auth.js index 21a498fd..66d59160 100644 --- a/ayanova/src/api/auth.js +++ b/ayanova/src/api/auth.js @@ -1,5 +1,5 @@ /* Xeslint-disable */ -import apiUtil from "./apiutil"; +import apiUtil from "./gzapi"; import { processLogin, processLogout } from "./authutil"; export default { diff --git a/ayanova/src/api/apiutil.js b/ayanova/src/api/gzapi.js similarity index 73% rename from ayanova/src/api/apiutil.js rename to ayanova/src/api/gzapi.js index 4834da2d..bf7ca024 100644 --- a/ayanova/src/api/apiutil.js +++ b/ayanova/src/api/gzapi.js @@ -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 //:/ @@ -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); + // } }); }); } diff --git a/ayanova/src/api/gzvalidate.js b/ayanova/src/api/gzvalidate.js index 788c718c..29928828 100644 --- a/ayanova/src/api/gzvalidate.js +++ b/ayanova/src/api/gzvalidate.js @@ -335,7 +335,6 @@ export default { // SetErrorBoxErrors // Gather server errors and set the appropriate keys SetErrorBoxErrors(v) { - **THIS** //maybe just put all the code in here and don't call geterrorboxerrors at all as no one else will need to call it anyway var errs = this.ServerErrors(v, "errorbox"); var ret = GetErrorBoxErrors(v, errs); diff --git a/ayanova/src/api/initialize.js b/ayanova/src/api/initialize.js index 138de4fa..3a9e3967 100644 --- a/ayanova/src/api/initialize.js +++ b/ayanova/src/api/initialize.js @@ -2,7 +2,7 @@ import store from "../store"; import roles from "./roles"; import locale from "./locale"; -import api from "./apiutil"; +import api from "./gzapi"; function addNavItem(title, icon, route) { store.commit("addNavItem", { diff --git a/ayanova/src/api/locale.js b/ayanova/src/api/locale.js index a8f93a8d..cabb947a 100644 --- a/ayanova/src/api/locale.js +++ b/ayanova/src/api/locale.js @@ -1,6 +1,6 @@ /* ZZeslint-disable */ import store from "../store"; -import apiUtil from "./apiutil"; +import apiUtil from "./gzapi"; import _ from "../libs/lodash.min.js"; export default { diff --git a/ayanova/src/components/HelloWorld.vue b/ayanova/src/components/HelloWorld.vue index ef360c3f..b5ff6c03 100644 --- a/ayanova/src/components/HelloWorld.vue +++ b/ayanova/src/components/HelloWorld.vue @@ -19,7 +19,7 @@ export default { .then(() => (this.formReady = true)) .catch(err => { this.formReady = true; - this.$gzerror(err); + this.$gzHandleFormError(err); }); }, data() { diff --git a/ayanova/src/components/inventorywidgetlist.vue b/ayanova/src/components/inventorywidgetlist.vue index 4847603a..78c90e1e 100644 --- a/ayanova/src/components/inventorywidgetlist.vue +++ b/ayanova/src/components/inventorywidgetlist.vue @@ -77,7 +77,7 @@ export default { .then(() => (this.formReady = true)) .catch(err => { this.formReady = true; //show the form anyway so we know what's what - this.$gzerror(err); + this.$gzHandleFormError(err); }); }, data() { diff --git a/ayanova/src/main.js b/ayanova/src/main.js index 11a01477..4cdea227 100644 --- a/ayanova/src/main.js +++ b/ayanova/src/main.js @@ -15,7 +15,7 @@ import lodash from "./libs/lodash.min.js"; //my libs import gzutil from "./api/gzutil"; import locale from "./api/locale"; -import gzapi from "./api/apiutil"; +import gzapi from "./api/gzapi"; import gzvalidate from "./api/gzvalidate"; import "@/assets/css/main.css"; @@ -31,7 +31,7 @@ Object.defineProperty(Vue.prototype, "$_", { value: lodash }); Object.defineProperty(Vue.prototype, "$gzlocale", { value: locale }); Object.defineProperty(Vue.prototype, "$gzapi", { value: gzapi }); Object.defineProperty(Vue.prototype, "$gzv", { value: gzvalidate }); -Object.defineProperty(Vue.prototype, "$gzerror", { +Object.defineProperty(Vue.prototype, "$gzHandleFormError", { value: errorHandler.handleFormError }); Object.defineProperty(Vue.prototype, "$gzdevmode", { diff --git a/ayanova/src/views/inventory-widget-edit.vue b/ayanova/src/views/inventory-widget-edit.vue index 8dc78f21..c1d4e172 100644 --- a/ayanova/src/views/inventory-widget-edit.vue +++ b/ayanova/src/views/inventory-widget-edit.vue @@ -158,12 +158,13 @@ export default { "WidgetCustom15", "WidgetCustom16" ]; + var that = this; this.$gzlocale .fetch(ltKeysRequired) .then(() => (this.formReady = true)) .catch(err => { this.formReady = true; - this.$gzerror(err); + that.$gzHandleFormError(err); }); }, created() { @@ -174,7 +175,7 @@ export default { return { obj: {}, serverError: {}, - errorBoxMessage:null, + errorBoxMessage: null, formReady: false }; }, @@ -184,9 +185,15 @@ export default { }, getDataFromApi() { var url = "Widget/" + this.$route.params.id; - this.$gzapi.get(url).then(res => { - this.obj = res.data; - }); + var that = this; + this.$gzapi + .get(url) + .then(res => { + this.obj = res.data; + }) + .catch(function(error) { + that.$gzHandleFormError(error); + }); }, submit() { //check if form is valid, as far as I know this is the way you're supposed to do it and in testing it does not force all fields to revalidate individually @@ -198,24 +205,26 @@ export default { this.$gzv.ClearServerErrors(this); //this.$gzutil.RemoveAllPropertiesFromObject(this.serverError); - this.$gzapi.upsert(url, this.obj).then(res => { - if (res.error) { - that.serverError = res.error; - that.$gzv.SetErrorBoxErrors(that); - } else { - //Logic for detecting if a post or put: if id then it was a post, if no id then it was a put - if (res.id) { - //Handle "post" of new record - that.obj = res.data; + this.$gzapi + .upsert(url, this.obj) + .then(res => { + if (res.error) { + that.serverError = res.error; + that.$gzv.SetErrorBoxErrors(that); } else { - //Handle "put" of an existing record - that.obj.concurrencyToken = res.data.concurrencyToken; + //Logic for detecting if a post or put: if id then it was a post, if no id then it was a put + if (res.id) { + //Handle "post" of new record + that.obj = res.data; + } else { + //Handle "put" of an existing record + that.obj.concurrencyToken = res.data.concurrencyToken; + } } - } - }); - //In theory all exceptions should be handled by the gzapi methods, so we should not need to deal with this in the form very often if at all - // .catch(function(error) { - // }); + }) + .catch(function(error) { + that.$gzHandleFormError(error); + }); } else { //say something so the user knows there is an issue alert("STUB: You can't do that, there are broken rules"); diff --git a/ayanova/src/views/inventory.vue b/ayanova/src/views/inventory.vue index 8df10233..4b5fbae6 100644 --- a/ayanova/src/views/inventory.vue +++ b/ayanova/src/views/inventory.vue @@ -44,7 +44,7 @@ export default { .then(() => (this.formReady = true)) .catch(err => { this.formReady = true; - this.$gzerror(err); + this.$gzHandleFormError(err); }); }, components: { diff --git a/ayanova/src/views/log.vue b/ayanova/src/views/log.vue index 10e02374..153a733e 100644 --- a/ayanova/src/views/log.vue +++ b/ayanova/src/views/log.vue @@ -21,7 +21,7 @@ export default { .then(() => (this.formReady = true)) .catch(err => { this.formReady = true; - this.$gzerror(err); + this.$gzHandleFormError(err); }); }, data() {