2
0
mirror of https://github.com/frappe/books.git synced 2024-12-24 11:55:46 +00:00

feat: start setting up error logging

This commit is contained in:
18alantom 2022-01-18 18:33:16 +05:30 committed by Alan
parent 23a8bb2a24
commit 7b61eec372
3 changed files with 69 additions and 1 deletions

24
src/errorHandling.js Normal file
View File

@ -0,0 +1,24 @@
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
}

View File

@ -4,9 +4,11 @@ import Vue from 'vue';
import models from '../models'; import models from '../models';
import App from './App'; import App from './App';
import FeatherIcon from './components/FeatherIcon'; import FeatherIcon from './components/FeatherIcon';
import { handleError } from './errorHandling';
import { IPC_MESSAGES } from './messages'; import { IPC_MESSAGES } from './messages';
import router from './router'; import router from './router';
import { outsideClickDirective } from './ui'; import { outsideClickDirective } from './ui';
import { stringifyCircular } from './utils';
(async () => { (async () => {
frappe.isServer = true; frappe.isServer = true;
@ -55,11 +57,22 @@ import { outsideClickDirective } from './ui';
}); });
Vue.config.errorHandler = (err, vm, info) => { Vue.config.errorHandler = (err, vm, info) => {
const { fullPath, params } = vm.$route;
const data = stringifyCircular(vm.$data, true, true);
const props = stringifyCircular(vm.$props, true, true);
handleError(false, err, {
fullPath,
params: stringifyCircular(params),
data,
props,
info,
});
console.error(err, vm, info); console.error(err, vm, info);
}; };
process.on('unhandledRejection', (error) => { process.on('unhandledRejection', (error) => {
console.error(error); handleError(true, error);
}); });
/* eslint-disable no-new */ /* eslint-disable no-new */

View File

@ -412,3 +412,34 @@ export function formatXLabels(label) {
return `${month} ${year}`; return `${month} ${year}`;
} }
export function stringifyCircular(
obj,
ignoreCircular = false,
convertBaseDocument = false
) {
const cacheKey = [];
const cacheValue = [];
return JSON.stringify(obj, (key, value) => {
if (typeof value !== 'object' || value === null) {
cacheKey.push(key);
cacheValue.push(value);
return value;
}
if (cacheValue.includes(value)) {
const circularKey = cacheKey[cacheValue.indexOf(value)] || '{self}';
return ignoreCircular ? undefined : `[Circular:${circularKey}]`;
}
cacheKey.push(key);
cacheValue.push(value);
if (convertBaseDocument && value instanceof frappe.BaseDocument) {
return value.getValidDict();
}
return value;
});
}