Files
raven-client/ayanova/src/api/errorhandler.js
2020-04-02 14:05:29 +00:00

132 lines
3.9 KiB
JavaScript

/* xeslint-disable */
let devModeShowErrors = false;
let lastMessageHash = 0;
////////////////////////////////////////////////////////
//
// translate, Log and optionally display errors
// return translated message in case caller needs it
function dealWithError(msg, vm) {
//Check if this is the same message again as last time to avoid
//repetitive loops of useless messages
let newHash = window.$gz.util.quickHash(msg);
if (newHash == lastMessageHash) {
return;
}
lastMessageHash = newHash;
//translate as necessary
msg = window.$gz.translation.translateString(msg);
//In some cases the error may not be translatable, if this is not a debug run then it should show without the ?? that translating puts in keys not found
//so it's not as weird looking to the user
if (!devModeShowErrors && msg.includes("??")) {
msg = msg.replace("??", "");
}
window.$gz.store.commit("logItem", msg);
if (devModeShowErrors) {
let errMsg =
"DEV ERROR errorHandler::devShowUnknownError - unexpected error: \r\n" +
msg;
// eslint-disable-next-line no-console
console.error(errMsg);
console.trace();
debugger;
window.$gz.eventBus.$emit("notify-error", "Dev error see log / console");
}
//If a form instance was provided (vue instance)
//then put the error into it
if (vm) {
if (window.$gz.errorHandler.devMode()) {
//make sure formState.appError is defined on data
if (!window.$gz._.has(vm, "formState.appError")) {
throw "DEV ERROR errorHandler::dealWithError -> formState.appError seems to be missing from form's vue data object";
}
}
vm.formState.appError = msg;
//TODO: What is this doing exactly?
//it's related to server errors but I'm setting appError above
//why two error properties?
window.$gz.form.setErrorBoxErrors(vm);
}
}
export default {
developmentModeShowErrorsImmediately(showErrorsImmediately) {
devModeShowErrors = showErrorsImmediately;
},
devMode() {
return devModeShowErrors;
},
handleGeneralError(message, source, lineno, colno, error) {
let 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) {
let msg = "Vue error: \n" + err;
if (err.fileName) {
msg += "\nfilename: " + err.fileName;
}
if (err.lineNumber) {
msg += "\nlineNumber: " + err.lineNumber;
}
if (info) {
msg += "\ninfo: " + info;
}
if (err.stack) {
msg += "\nSTACK:\n " + err.stack;
}
dealWithError(msg);
},
handleVueWarning(wmsg, vm, trace) {
let msg = "Vue warning: \n" + wmsg;
// if (vm) {
// msg += "\nvm present ";
// }
if (trace) {
msg += "\ntrace: " + trace;
}
dealWithError(msg);
},
/////////////////////////////////////////////////
// translate, log and return error
//
handleFormError(err, vm) {
//called inside forms when things go unexpectedly wrong
//returns the translated message in case the form wants to display it as well
if (err instanceof Error && err.message) {
dealWithError(err.message, vm);
} else {
//TODO: this is pretty bad
let msg = JSON.stringify(err);
dealWithError(msg, vm);
}
}
};
/*
ERROR CODES USED:
Client error codes are all in the range of E16 to E999
Server error codes are all in the range of E1000 to E1999
API specific (logic) error codes are all in the range of 2000 to 3000
CLIENT ERROR CODES:
E16 - ErrorUserNotAuthenticated
E17 - ErrorServerUnresponsive
E18 - Misc error without a translation key, unexpected throws etc or api error during server call, details in the message / Any error without a translation key defined basically
*/