mirror of
https://github.com/frappe/books.git
synced 2024-12-23 19:39:07 +00:00
feat: start setting up error logging
This commit is contained in:
parent
23a8bb2a24
commit
7b61eec372
24
src/errorHandling.js
Normal file
24
src/errorHandling.js
Normal 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
|
||||
}
|
15
src/main.js
15
src/main.js
@ -4,9 +4,11 @@ import Vue from 'vue';
|
||||
import models from '../models';
|
||||
import App from './App';
|
||||
import FeatherIcon from './components/FeatherIcon';
|
||||
import { handleError } from './errorHandling';
|
||||
import { IPC_MESSAGES } from './messages';
|
||||
import router from './router';
|
||||
import { outsideClickDirective } from './ui';
|
||||
import { stringifyCircular } from './utils';
|
||||
|
||||
(async () => {
|
||||
frappe.isServer = true;
|
||||
@ -55,11 +57,22 @@ import { outsideClickDirective } from './ui';
|
||||
});
|
||||
|
||||
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);
|
||||
};
|
||||
|
||||
process.on('unhandledRejection', (error) => {
|
||||
console.error(error);
|
||||
handleError(true, error);
|
||||
});
|
||||
|
||||
/* eslint-disable no-new */
|
||||
|
31
src/utils.js
31
src/utils.js
@ -412,3 +412,34 @@ export function formatXLabels(label) {
|
||||
|
||||
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;
|
||||
});
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user