Files
raven-client/ayanova/src/api/errorhandler.js
2020-06-19 21:38:32 +00:00

161 lines
4.6 KiB
JavaScript

/* xeslint-disable */
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
//vm may be null here so check window gz for dev
if (!window.$gz.dev && msg.includes("??")) {
msg = msg.replace("??", "");
}
window.$gz.store.commit("logItem", msg);
if (window.$gz.dev) {
let errMsg = "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");
return;
}
//If a form instance was provided (vue instance)
//then put the error into it
if (vm) {
if (vm.$ay.dev) {
//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);
} else {
//popup if no place to display it elsewise
window.$gz.eventBus.$emit("notify-error", msg);
}
}
export default {
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 if (err.error) {
//it's an api error return object, translate and display correctly
err = err.error;
// {
// "code": "2002",
// "message": "See server log for details",
// "target": "Server internal error"
// }
let msg = "";
if (err.code) {
msg += err.code;
msg += " - ";
msg += vm.$ay.t("ErrorAPI" + err.code);
msg += "\n";
}
if (err.target) {
msg += err.target;
msg += "\n";
}
if (err.message) {
msg += err.message;
msg += "\n";
}
dealWithError(msg, vm);
} else if (err instanceof Response) {
let msg =
"http error: " +
err.statusText +
" - " +
err.status +
" Url: " +
err.url;
dealWithError(msg, vm);
} else {
//last resort
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
*/