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

55 lines
1.1 KiB
JavaScript
Raw Normal View History

2022-01-18 13:03:16 +00:00
import frappe from 'frappejs';
import { MandatoryError, ValidationError } from 'frappejs/common/errors';
function shouldNotStore(error) {
return [MandatoryError, ValidationError].some(
(errorClass) => error instanceof errorClass
);
}
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 };
frappe.errorLog.push(errorLogObj);
// Do something cool
}
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;
}
};
}