diff --git a/fyo/utils/errors.ts b/fyo/utils/errors.ts index e401a403..d974e4db 100644 --- a/fyo/utils/errors.ts +++ b/fyo/utils/errors.ts @@ -1,74 +1,76 @@ export class BaseError extends Error { message: string; statusCode: number; + shouldStore: boolean; - constructor(statusCode: number, message: string) { + constructor(statusCode: number, message: string, shouldStore: boolean = true) { super(message); this.name = 'BaseError'; this.statusCode = statusCode; this.message = message; + this.shouldStore = shouldStore; } } export class ValidationError extends BaseError { - constructor(message: string) { - super(417, message); + constructor(message: string, shouldStore: boolean = false) { + super(417, message, shouldStore); this.name = 'ValidationError'; } } export class NotFoundError extends BaseError { - constructor(message: string) { - super(404, message); + constructor(message: string, shouldStore: boolean = true) { + super(404, message, shouldStore); this.name = 'NotFoundError'; } } export class ForbiddenError extends BaseError { - constructor(message: string) { - super(403, message); + constructor(message: string, shouldStore: boolean = true) { + super(403, message, shouldStore); this.name = 'ForbiddenError'; } } export class DuplicateEntryError extends ValidationError { - constructor(message: string) { - super(message); + constructor(message: string, shouldStore: boolean = true) { + super(message, shouldStore); this.name = 'DuplicateEntryError'; } } export class LinkValidationError extends ValidationError { - constructor(message: string) { - super(message); + constructor(message: string, shouldStore: boolean = true) { + super(message, shouldStore); this.name = 'LinkValidationError'; } } export class MandatoryError extends ValidationError { - constructor(message: string) { - super(message); + constructor(message: string, shouldStore: boolean = true) { + super(message, shouldStore); this.name = 'MandatoryError'; } } export class DatabaseError extends BaseError { - constructor(message: string) { - super(500, message); + constructor(message: string, shouldStore: boolean = true) { + super(500, message, shouldStore); this.name = 'DatabaseError'; } } export class CannotCommitError extends DatabaseError { - constructor(message: string) { - super(message); + constructor(message: string, shouldStore: boolean = true) { + super(message, shouldStore); this.name = 'CannotCommitError'; } } export class NotImplemented extends BaseError { - constructor() { - super(501, ''); + constructor(message: string = '', shouldStore: boolean = false) { + super(501, message, shouldStore); this.name = 'NotImplemented'; } } diff --git a/models/Transactional/LedgerPosting.ts b/models/Transactional/LedgerPosting.ts index 904a906c..e22719e2 100644 --- a/models/Transactional/LedgerPosting.ts +++ b/models/Transactional/LedgerPosting.ts @@ -1,5 +1,5 @@ import { Fyo, t } from 'fyo'; -import { ValidationError } from 'fyo/utils/errors'; +import { NotFoundError, ValidationError } from 'fyo/utils/errors'; import { Account } from 'models/baseModels/Account/Account'; import { AccountingLedgerEntry } from 'models/baseModels/AccountingLedgerEntry/AccountingLedgerEntry'; import { ModelNameEnum } from 'models/types'; @@ -210,9 +210,20 @@ export class LedgerPosting { } async _getRoundOffAccount() { - return (await this.fyo.getValue( + const roundOffAccount = (await this.fyo.getValue( ModelNameEnum.AccountingSettings, 'roundOffAccount' )) as string; + + if (!roundOffAccount) { + const notFoundError = new NotFoundError( + t`Please set Round Off Account in the Settings.`, + false + ); + notFoundError.name = t`Round Off Account Not Found`; + throw notFoundError; + } + + return roundOffAccount; } } diff --git a/src/errorHandling.ts b/src/errorHandling.ts index f10a9f4a..7559e233 100644 --- a/src/errorHandling.ts +++ b/src/errorHandling.ts @@ -1,11 +1,7 @@ import { ipcRenderer } from 'electron'; import { t } from 'fyo'; import { Doc } from 'fyo/model/doc'; -import { - MandatoryError, - NotFoundError, - ValidationError, -} from 'fyo/utils/errors'; +import { BaseError } from 'fyo/utils/errors'; import { ErrorLog } from 'fyo/utils/types'; import { truncate } from 'lodash'; import { IPC_ACTIONS, IPC_MESSAGES } from 'utils/messages'; @@ -16,9 +12,8 @@ import { MessageDialogOptions, ToastOptions } from './utils/types'; import { showMessageDialog, showToast } from './utils/ui'; function shouldNotStore(error: Error) { - return [MandatoryError, ValidationError, NotFoundError].some( - (errorClass) => error instanceof errorClass - ); + const shouldLog = (error as BaseError).shouldStore ?? true; + return !shouldLog; } async function reportError(errorLogObj: ErrorLog) { @@ -58,18 +53,17 @@ export function getErrorLogObject( const { name, stack, message } = error; const errorLogObj = { name, stack, message, more }; - // @ts-ignore fyo.errorLog.push(errorLogObj); return errorLogObj; } export async function handleError( - shouldLog: boolean, + logToConsole: boolean, error: Error, more?: Record ) { - if (shouldLog) { + if (logToConsole) { console.error(error); }