2022-01-19 07:39:39 +00:00
|
|
|
import { ipcRenderer } from 'electron';
|
2022-01-24 06:44:11 +00:00
|
|
|
import frappe, { t } from 'frappe';
|
|
|
|
import { MandatoryError, ValidationError } from 'frappe/common/errors';
|
2022-01-19 07:39:39 +00:00
|
|
|
import { IPC_ACTIONS } from './messages';
|
2022-01-19 10:59:13 +00:00
|
|
|
import { showMessageDialog, showToast } from './utils';
|
2022-01-18 13:03:16 +00:00
|
|
|
|
|
|
|
function shouldNotStore(error) {
|
|
|
|
return [MandatoryError, ValidationError].some(
|
|
|
|
(errorClass) => error instanceof errorClass
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2022-01-19 10:59:13 +00:00
|
|
|
function reportError(errorLogObj) {
|
|
|
|
// push errorlog to frappebooks.com
|
|
|
|
console.log(errorLogObj);
|
|
|
|
}
|
|
|
|
|
2022-01-19 11:13:50 +00:00
|
|
|
function getToastProps(errorLogObj) {
|
|
|
|
const props = {
|
2022-01-24 06:44:11 +00:00
|
|
|
message: t`Error: ` + errorLogObj.name,
|
2022-01-19 11:13:50 +00:00
|
|
|
type: 'error',
|
|
|
|
};
|
|
|
|
|
|
|
|
if (!frappe.SystemSettings.autoReportErrors) {
|
|
|
|
Object.assign(props, {
|
2022-01-24 06:44:11 +00:00
|
|
|
actionText: t`Report Error`,
|
2022-01-19 11:13:50 +00:00
|
|
|
action: () => {
|
|
|
|
reportError(errorLogObj);
|
|
|
|
},
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
return props;
|
|
|
|
}
|
|
|
|
|
2022-01-18 13:03:16 +00:00
|
|
|
export function handleError(shouldLog, error, more = {}) {
|
|
|
|
if (shouldLog) {
|
|
|
|
console.error(error);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (shouldNotStore(error)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const { name, stack, message } = error;
|
|
|
|
const errorLogObj = { name, stack, message, more };
|
2022-01-19 11:13:50 +00:00
|
|
|
|
2022-01-18 13:03:16 +00:00
|
|
|
frappe.errorLog.push(errorLogObj);
|
|
|
|
|
2022-01-19 11:13:50 +00:00
|
|
|
showToast(getToastProps(errorLogObj));
|
|
|
|
if (frappe.SystemSettings.autoReportErrors) {
|
|
|
|
reportError(errorLogObj);
|
|
|
|
}
|
2022-01-18 13:03:16 +00:00
|
|
|
}
|
2022-01-19 06:14:12 +00:00
|
|
|
|
2022-01-19 07:39:39 +00:00
|
|
|
export function getErrorMessage(e, doc) {
|
2022-01-24 06:44:11 +00:00
|
|
|
let errorMessage = e.message || t`An error occurred.`;
|
2022-01-19 07:39:39 +00:00
|
|
|
const { doctype, name } = doc;
|
|
|
|
const canElaborate = doctype && name;
|
|
|
|
if (e.type === frappe.errors.LinkValidationError && canElaborate) {
|
2022-01-24 06:44:11 +00:00
|
|
|
errorMessage = t`${doctype} ${name} is linked with existing records.`;
|
2022-01-19 07:39:39 +00:00
|
|
|
} else if (e.type === frappe.errors.DuplicateEntryError && canElaborate) {
|
2022-01-24 06:44:11 +00:00
|
|
|
errorMessage = t`${doctype} ${name} already exists.`;
|
2022-01-19 07:39:39 +00:00
|
|
|
}
|
|
|
|
return errorMessage;
|
|
|
|
}
|
|
|
|
|
|
|
|
export function handleErrorWithDialog(error, doc = {}) {
|
|
|
|
let errorMessage = getErrorMessage(error, doc);
|
|
|
|
handleError(false, error, { errorMessage, doc });
|
|
|
|
|
|
|
|
showMessageDialog({ message: errorMessage });
|
|
|
|
throw error;
|
|
|
|
}
|
|
|
|
|
|
|
|
export async function showErrorDialog({ title, content }) {
|
|
|
|
// To be used for show stopper errors
|
2022-01-24 06:44:11 +00:00
|
|
|
title ??= t`Error`;
|
|
|
|
content ??= t`Something has gone terribly wrong. Please check the console and raise an issue.`;
|
2022-01-19 07:39:39 +00:00
|
|
|
|
|
|
|
await ipcRenderer.invoke(IPC_ACTIONS.SHOW_ERROR, { title, content });
|
|
|
|
}
|
|
|
|
|
|
|
|
// Wrapper Functions
|
|
|
|
|
2022-01-19 06:14:12 +00:00
|
|
|
export function getErrorHandled(func) {
|
|
|
|
return async function errorHandled(...args) {
|
|
|
|
try {
|
|
|
|
return await func(...args);
|
|
|
|
} catch (error) {
|
|
|
|
handleError(false, error, {
|
|
|
|
functionName: func.name,
|
|
|
|
functionArgs: args,
|
|
|
|
});
|
|
|
|
|
|
|
|
throw error;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
export function getErrorHandledSync(func) {
|
|
|
|
return function errorHandledSync(...args) {
|
|
|
|
try {
|
|
|
|
return func(...args);
|
|
|
|
} catch (error) {
|
|
|
|
handleError(false, error, {
|
|
|
|
functionName: func.name,
|
|
|
|
functionArgs: args,
|
|
|
|
});
|
|
|
|
|
|
|
|
throw error;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|