mirror of
https://github.com/frappe/books.git
synced 2024-11-08 14:50:56 +00:00
fix(ux): add should store to errors
- throw error if Round Off Account not found
This commit is contained in:
parent
fe35a043f8
commit
9136e5e647
@ -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';
|
||||
}
|
||||
}
|
||||
|
@ -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;
|
||||
}
|
||||
}
|
||||
|
@ -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<string, unknown>
|
||||
) {
|
||||
if (shouldLog) {
|
||||
if (logToConsole) {
|
||||
console.error(error);
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user