This commit is contained in:
2019-04-08 17:09:35 +00:00
parent 3400b9de3f
commit fa360f03b3
11 changed files with 94 additions and 73 deletions

View File

@@ -1,5 +1,5 @@
/* Xeslint-disable */
import apiUtil from "./apiutil";
import apiUtil from "./gzapi";
import { processLogin, processLogout } from "./authutil";
export default {

View File

@@ -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);
// }
});
});
}

View File

@@ -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);

View File

@@ -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", {

View File

@@ -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 {

View File

@@ -19,7 +19,7 @@ export default {
.then(() => (this.formReady = true))
.catch(err => {
this.formReady = true;
this.$gzerror(err);
this.$gzHandleFormError(err);
});
},
data() {

View File

@@ -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() {

View File

@@ -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", {

View File

@@ -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");

View File

@@ -44,7 +44,7 @@ export default {
.then(() => (this.formReady = true))
.catch(err => {
this.formReady = true;
this.$gzerror(err);
this.$gzHandleFormError(err);
});
},
components: {

View File

@@ -21,7 +21,7 @@ export default {
.then(() => (this.formReady = true))
.catch(err => {
this.formReady = true;
this.$gzerror(err);
this.$gzHandleFormError(err);
});
},
data() {