2
0
mirror of https://github.com/frappe/books.git synced 2024-12-25 12:10:06 +00:00
books/src/errorHandling.js

115 lines
2.8 KiB
JavaScript
Raw Normal View History

import { ipcRenderer } from 'electron';
2022-01-24 06:44:11 +00:00
import frappe, { t } from 'frappe';
import { MandatoryError, ValidationError } from 'frappe/common/errors';
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);
}
function getToastProps(errorLogObj) {
const props = {
2022-01-24 06:44:11 +00:00
message: t`Error: ` + errorLogObj.name,
type: 'error',
};
if (!frappe.SystemSettings.autoReportErrors) {
Object.assign(props, {
2022-01-24 06:44:11 +00:00
actionText: t`Report Error`,
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-18 13:03:16 +00:00
frappe.errorLog.push(errorLogObj);
showToast(getToastProps(errorLogObj));
if (frappe.SystemSettings.autoReportErrors) {
reportError(errorLogObj);
}
2022-01-18 13:03:16 +00:00
}
export function getErrorMessage(e, doc) {
2022-01-24 06:44:11 +00:00
let errorMessage = e.message || t`An error occurred.`;
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.`;
} else if (e.type === frappe.errors.DuplicateEntryError && canElaborate) {
2022-01-24 06:44:11 +00:00
errorMessage = t`${doctype} ${name} already exists.`;
}
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.`;
await ipcRenderer.invoke(IPC_ACTIONS.SHOW_ERROR, { title, content });
}
// Wrapper Functions
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;
}
};
}