Files
raven-client/ayanova/src/api/errorhandler.js
2019-04-23 19:41:55 +00:00

92 lines
2.5 KiB
JavaScript

/* Xeslint-disable */
import store from "../store";
import locale from "./locale";
var devModeShowErrors = false;
////////////////////////////////////////////////////////
//
// Localize, Log and optionally display errors
// return localized message in case caller needs it
function dealWithError(msg, form) {
msg = locale.translateString(msg);
//In some cases the error may not be localizable, if this is not a debug run then it should show without the ?? that localizing puts in keys not found
//so it's not as wierd looking to the user
if (!devModeShowErrors && msg.includes("??")) {
msg = msg.replace("??", "");
}
store.commit("logItem", msg);
if (devModeShowErrors) {
alert("~" + msg);
}
//If a form instance was provided (vue instance)
//then put the error into it
if (form) {
if (form.$gzdevmode()) {
//make sure formState.appError is defined on data
if (!form.$_.has(form, "formState.appError")) {
throw "DEV ERROR errorHandler::dealWithError -> formState.appError seems to be missing from form's vue data object";
}
}
form.formState.appError = msg;
form.$gzform.setErrorBoxErrors(form);
}
}
export default {
developmentModeShowErrorsImmediately(showErrorsImmediately) {
devModeShowErrors = showErrorsImmediately;
},
devMode() {
return devModeShowErrors;
},
handleGeneralError(message, source, lineno, colno, error) {
var msg = "General error: \n" + message;
if (source) {
msg += "\nsource: " + source;
}
if (lineno) {
msg += "\nlineno: " + lineno;
}
if (colno) {
msg += "\ncolno: " + colno;
}
if (error) {
msg += "\nerror: " + error;
}
dealWithError(msg);
},
handleVueError(err, vm, info) {
var msg = "Vue error: \n" + err;
// if (vm) {
// msg += "\nvm present ";
// }
if (info) {
msg += "\ninfo: " + info;
}
dealWithError(msg);
},
handleVueWarning(wmsg, vm, trace) {
var msg = "Vue warning: \n" + wmsg;
// if (vm) {
// msg += "\nvm present ";
// }
if (trace) {
msg += "\ntrace: " + trace;
}
dealWithError(msg);
},
/////////////////////////////////////////////////
// Localize, log and return error
//
handleFormError(err, form) {
//called inside forms when things go wrong
//returns the localized message in case the form wants to display it as well
if (err instanceof Error && err.message) {
dealWithError(err.message, form);
} else {
dealWithError(err.toString(), form);
}
}
};