2
0
mirror of https://github.com/frappe/books.git synced 2024-11-08 23:00:56 +00:00

fix(ux): add should store to errors

- throw error if Round Off Account not found
This commit is contained in:
18alantom 2022-07-28 13:25:48 +05:30
parent fe35a043f8
commit 9136e5e647
3 changed files with 39 additions and 32 deletions

View File

@ -1,74 +1,76 @@
export class BaseError extends Error { export class BaseError extends Error {
message: string; message: string;
statusCode: number; statusCode: number;
shouldStore: boolean;
constructor(statusCode: number, message: string) { constructor(statusCode: number, message: string, shouldStore: boolean = true) {
super(message); super(message);
this.name = 'BaseError'; this.name = 'BaseError';
this.statusCode = statusCode; this.statusCode = statusCode;
this.message = message; this.message = message;
this.shouldStore = shouldStore;
} }
} }
export class ValidationError extends BaseError { export class ValidationError extends BaseError {
constructor(message: string) { constructor(message: string, shouldStore: boolean = false) {
super(417, message); super(417, message, shouldStore);
this.name = 'ValidationError'; this.name = 'ValidationError';
} }
} }
export class NotFoundError extends BaseError { export class NotFoundError extends BaseError {
constructor(message: string) { constructor(message: string, shouldStore: boolean = true) {
super(404, message); super(404, message, shouldStore);
this.name = 'NotFoundError'; this.name = 'NotFoundError';
} }
} }
export class ForbiddenError extends BaseError { export class ForbiddenError extends BaseError {
constructor(message: string) { constructor(message: string, shouldStore: boolean = true) {
super(403, message); super(403, message, shouldStore);
this.name = 'ForbiddenError'; this.name = 'ForbiddenError';
} }
} }
export class DuplicateEntryError extends ValidationError { export class DuplicateEntryError extends ValidationError {
constructor(message: string) { constructor(message: string, shouldStore: boolean = true) {
super(message); super(message, shouldStore);
this.name = 'DuplicateEntryError'; this.name = 'DuplicateEntryError';
} }
} }
export class LinkValidationError extends ValidationError { export class LinkValidationError extends ValidationError {
constructor(message: string) { constructor(message: string, shouldStore: boolean = true) {
super(message); super(message, shouldStore);
this.name = 'LinkValidationError'; this.name = 'LinkValidationError';
} }
} }
export class MandatoryError extends ValidationError { export class MandatoryError extends ValidationError {
constructor(message: string) { constructor(message: string, shouldStore: boolean = true) {
super(message); super(message, shouldStore);
this.name = 'MandatoryError'; this.name = 'MandatoryError';
} }
} }
export class DatabaseError extends BaseError { export class DatabaseError extends BaseError {
constructor(message: string) { constructor(message: string, shouldStore: boolean = true) {
super(500, message); super(500, message, shouldStore);
this.name = 'DatabaseError'; this.name = 'DatabaseError';
} }
} }
export class CannotCommitError extends DatabaseError { export class CannotCommitError extends DatabaseError {
constructor(message: string) { constructor(message: string, shouldStore: boolean = true) {
super(message); super(message, shouldStore);
this.name = 'CannotCommitError'; this.name = 'CannotCommitError';
} }
} }
export class NotImplemented extends BaseError { export class NotImplemented extends BaseError {
constructor() { constructor(message: string = '', shouldStore: boolean = false) {
super(501, ''); super(501, message, shouldStore);
this.name = 'NotImplemented'; this.name = 'NotImplemented';
} }
} }

View File

@ -1,5 +1,5 @@
import { Fyo, t } from 'fyo'; 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 { Account } from 'models/baseModels/Account/Account';
import { AccountingLedgerEntry } from 'models/baseModels/AccountingLedgerEntry/AccountingLedgerEntry'; import { AccountingLedgerEntry } from 'models/baseModels/AccountingLedgerEntry/AccountingLedgerEntry';
import { ModelNameEnum } from 'models/types'; import { ModelNameEnum } from 'models/types';
@ -210,9 +210,20 @@ export class LedgerPosting {
} }
async _getRoundOffAccount() { async _getRoundOffAccount() {
return (await this.fyo.getValue( const roundOffAccount = (await this.fyo.getValue(
ModelNameEnum.AccountingSettings, ModelNameEnum.AccountingSettings,
'roundOffAccount' 'roundOffAccount'
)) as string; )) 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;
} }
} }

View File

@ -1,11 +1,7 @@
import { ipcRenderer } from 'electron'; import { ipcRenderer } from 'electron';
import { t } from 'fyo'; import { t } from 'fyo';
import { Doc } from 'fyo/model/doc'; import { Doc } from 'fyo/model/doc';
import { import { BaseError } from 'fyo/utils/errors';
MandatoryError,
NotFoundError,
ValidationError,
} from 'fyo/utils/errors';
import { ErrorLog } from 'fyo/utils/types'; import { ErrorLog } from 'fyo/utils/types';
import { truncate } from 'lodash'; import { truncate } from 'lodash';
import { IPC_ACTIONS, IPC_MESSAGES } from 'utils/messages'; import { IPC_ACTIONS, IPC_MESSAGES } from 'utils/messages';
@ -16,9 +12,8 @@ import { MessageDialogOptions, ToastOptions } from './utils/types';
import { showMessageDialog, showToast } from './utils/ui'; import { showMessageDialog, showToast } from './utils/ui';
function shouldNotStore(error: Error) { function shouldNotStore(error: Error) {
return [MandatoryError, ValidationError, NotFoundError].some( const shouldLog = (error as BaseError).shouldStore ?? true;
(errorClass) => error instanceof errorClass return !shouldLog;
);
} }
async function reportError(errorLogObj: ErrorLog) { async function reportError(errorLogObj: ErrorLog) {
@ -58,18 +53,17 @@ export function getErrorLogObject(
const { name, stack, message } = error; const { name, stack, message } = error;
const errorLogObj = { name, stack, message, more }; const errorLogObj = { name, stack, message, more };
// @ts-ignore
fyo.errorLog.push(errorLogObj); fyo.errorLog.push(errorLogObj);
return errorLogObj; return errorLogObj;
} }
export async function handleError( export async function handleError(
shouldLog: boolean, logToConsole: boolean,
error: Error, error: Error,
more?: Record<string, unknown> more?: Record<string, unknown>
) { ) {
if (shouldLog) { if (logToConsole) {
console.error(error); console.error(error);
} }