Skip to content

Commit

Permalink
incr: add logUnexpected
Browse files Browse the repository at this point in the history
- prevent undefined rootType, log it instead
  • Loading branch information
18alantom committed Sep 20, 2022
1 parent 69bf6a0 commit 8d3d0a2
Show file tree
Hide file tree
Showing 6 changed files with 61 additions and 7 deletions.
9 changes: 9 additions & 0 deletions reports/AccountReport.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import { Field } from 'schemas/types';
import { fyo } from 'src/initFyo';
import { getMapFromList } from 'utils';
import { QueryFilter } from 'utils/db/types';
import { logUnexpected } from 'utils/misc';

export const ACC_NAME_WIDTH = 2;
export const ACC_BAL_WIDTH = 1.25;
Expand Down Expand Up @@ -167,6 +168,14 @@ export abstract class AccountReport extends LedgerReport {
continue;
}

if (!accountMap[entry.account]) {
logUnexpected({
message: 'accountMap[entry.account] is undefined',
more: { entry },
});
continue;
}

const totalBalance = valueMap.get(key!)?.balance ?? 0;
const balance = (entry.debit ?? 0) - (entry.credit ?? 0);
const rootType = accountMap[entry.account].rootType;
Expand Down
11 changes: 8 additions & 3 deletions src/errorHandling.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ function shouldNotStore(error: Error) {
return !shouldLog;
}

async function reportError(errorLogObj: ErrorLog) {
export async function sendError(errorLogObj: ErrorLog) {
if (!errorLogObj.stack) {
return;
}
Expand All @@ -30,13 +30,14 @@ async function reportError(errorLogObj: ErrorLog) {
version: fyo.store.appVersion,
language: fyo.store.language,
instance_id: fyo.store.instanceId,
device_id: fyo.store.deviceId,
open_count: fyo.store.openCount,
country_code: fyo.singles.SystemSettings?.countryCode,
more: stringifyCircular(errorLogObj.more ?? {}),
};

if (fyo.store.isDevelopment) {
console.log('reportError', body);
console.log('sendError', body);
}

await ipcRenderer.invoke(IPC_ACTIONS.SEND_ERROR, JSON.stringify(body));
Expand Down Expand Up @@ -84,7 +85,7 @@ export async function handleError(

const errorLogObj = getErrorLogObject(error, more ?? {});

await reportError(errorLogObj);
await sendError(errorLogObj);
const toastProps = getToastProps(errorLogObj);
await showToast(toastProps);
}
Expand Down Expand Up @@ -250,5 +251,9 @@ function getErrorLabel(error: Error) {
return t`Error`;
}

if (name === 'ToDebugError') {
return t`Error`;
}

return t`Error`;
}
15 changes: 13 additions & 2 deletions src/renderer.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import { ipcRenderer } from 'electron';
import { ConfigKeys } from 'fyo/core/types';
import { DateTime } from 'luxon';
import { IPC_ACTIONS } from 'utils/messages';
import { CUSTOM_EVENTS, IPC_ACTIONS } from 'utils/messages';
import { UnexpectedLogObject } from 'utils/types';
import { App as VueApp, createApp } from 'vue';
import App from './App.vue';
import Badge from './components/Badge.vue';
import FeatherIcon from './components/FeatherIcon.vue';
import { getErrorHandled, handleError } from './errorHandling';
import { getErrorHandled, handleError, sendError } from './errorHandling';
import { fyo } from './initFyo';
import { outsideClickDirective } from './renderer/helpers';
import registerIpcRendererListeners from './renderer/registerIpcRendererListeners';
Expand Down Expand Up @@ -72,6 +73,16 @@ function setErrorHandlers(app: VueApp) {
handleError(true, error, { message, source, lineno, colno });
};

window.onunhandledrejection = (event: PromiseRejectionEvent) => {
const error = event.reason;
handleError(true, error).catch((err) => console.error(err));
};

window.addEventListener(CUSTOM_EVENTS.LOG_UNEXPECTED, (event) => {
const details = (event as CustomEvent)?.detail as UnexpectedLogObject;
sendError(details);
});

app.config.errorHandler = (err, vm, info) => {
const more: Record<string, unknown> = {
info,
Expand Down
1 change: 1 addition & 0 deletions utils/messages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,4 +46,5 @@ export enum DB_CONN_FAILURE {
// events
export enum CUSTOM_EVENTS {
MAIN_PROCESS_ERROR = 'main-process-error',
LOG_UNEXPECTED = 'log-unexpected',
}
25 changes: 23 additions & 2 deletions utils/misc.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import countryInfo from '../fixtures/countryInfo.json';
import { DateTime } from 'luxon';
import { CountryInfoMap } from './types';
import countryInfo from '../fixtures/countryInfo.json';
import { CUSTOM_EVENTS } from './messages';
import { CountryInfoMap, UnexpectedLogObject } from './types';

export function getCountryInfo(): CountryInfoMap {
// @ts-ignore
Expand Down Expand Up @@ -34,3 +35,23 @@ export function getFiscalYear(date: string, isStart: boolean) {
.plus({ year: [1, 2, 3].includes(today.month) ? 0 : 1 })
.toISODate();
}

export function logUnexpected(detail: Partial<UnexpectedLogObject>) {
/**
* Raises a custom event, it's lsitener is in renderer.ts
* used to log unexpected occurances as errors.
*/
if (!window?.CustomEvent) {
return;
}

detail.name ??= 'LogUnexpected';
detail.message ??= 'Logging an unexpected occurance';
detail.stack ??= new Error().stack;
detail.more ??= {};

const event = new window.CustomEvent(CUSTOM_EVENTS.LOG_UNEXPECTED, {
detail,
});
window.dispatchEvent(event);
}
7 changes: 7 additions & 0 deletions utils/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,10 @@ export interface VersionParts {
}

export type Creds = { errorLogUrl: string; telemetryUrl: string; tokenString: string };

export type UnexpectedLogObject = {
name: string;
message: string;
stack: string;
more: Record<string, unknown>;
}

0 comments on commit 8d3d0a2

Please sign in to comment.