diff --git a/fyo/model/doc.ts b/fyo/model/doc.ts index 8cc6d0d4..c2bfb27e 100644 --- a/fyo/model/doc.ts +++ b/fyo/model/doc.ts @@ -291,7 +291,7 @@ export class Doc extends Observable { return childDoc; } - async _validateInsert() { + async _validateSync() { this._validateMandatory(); await this._validateFields(); } @@ -573,6 +573,7 @@ export class Doc extends Observable { async _preSync() { this._setChildDocsIdx(); await this._applyFormula(); + await this._validateSync(); await this.trigger('validate'); } @@ -580,7 +581,6 @@ export class Doc extends Observable { await setName(this, this.fyo); this._setBaseMetaValues(); await this._preSync(); - await this._validateInsert(); const oldName = this.name!; const validDict = this.getValidDict(); @@ -596,8 +596,8 @@ export class Doc extends Observable { async _update() { await this._validateDbNotModified(); - await this._preSync(); this._updateModifiedMetaValues(); + await this._preSync(); const data = this.getValidDict(); await this.fyo.db.update(this.schemaName, data); diff --git a/fyo/utils/errors.ts b/fyo/utils/errors.ts index 636f80db..9ca6cdad 100644 --- a/fyo/utils/errors.ts +++ b/fyo/utils/errors.ts @@ -1,9 +1,13 @@ +import { t } from './translation'; + export class BaseError extends Error { + label: string; message: string; statusCode: number; constructor(statusCode: number, message: string) { super(message); + this.label = t`Base Error`; this.name = 'BaseError'; this.statusCode = statusCode; this.message = message; @@ -13,6 +17,7 @@ export class BaseError extends Error { export class ValidationError extends BaseError { constructor(message: string) { super(417, message); + this.label = t`Validation Error`; this.name = 'ValidationError'; } } @@ -20,6 +25,7 @@ export class ValidationError extends BaseError { export class NotFoundError extends BaseError { constructor(message: string) { super(404, message); + this.label = t`Not Found Error`; this.name = 'NotFoundError'; } } @@ -27,6 +33,7 @@ export class NotFoundError extends BaseError { export class ForbiddenError extends BaseError { constructor(message: string) { super(403, message); + this.label = t`Forbidden Error`; this.name = 'ForbiddenError'; } } @@ -34,6 +41,7 @@ export class ForbiddenError extends BaseError { export class DuplicateEntryError extends ValidationError { constructor(message: string) { super(message); + this.label = t`Duplicate Entry Error`; this.name = 'DuplicateEntryError'; } } @@ -41,6 +49,7 @@ export class DuplicateEntryError extends ValidationError { export class LinkValidationError extends ValidationError { constructor(message: string) { super(message); + this.label = t`Link Validation Error`; this.name = 'LinkValidationError'; } } @@ -48,6 +57,7 @@ export class LinkValidationError extends ValidationError { export class MandatoryError extends ValidationError { constructor(message: string) { super(message); + this.label = t`Mandatory Error`; this.name = 'MandatoryError'; } } @@ -55,6 +65,7 @@ export class MandatoryError extends ValidationError { export class DatabaseError extends BaseError { constructor(message: string) { super(500, message); + this.label = t`Database Error`; this.name = 'DatabaseError'; } } @@ -62,6 +73,7 @@ export class DatabaseError extends BaseError { export class CannotCommitError extends DatabaseError { constructor(message: string) { super(message); + this.label = t`Cannot Commit Error`; this.name = 'CannotCommitError'; } } diff --git a/schemas/app/JournalEntry.json b/schemas/app/JournalEntry.json index a53ba3dc..ba2f372f 100644 --- a/schemas/app/JournalEntry.json +++ b/schemas/app/JournalEntry.json @@ -66,7 +66,8 @@ { "fieldname": "date", "label": "Date", - "fieldtype": "Date" + "fieldtype": "Date", + "required": true }, { "fieldname": "accounts", diff --git a/schemas/app/Payment.json b/schemas/app/Payment.json index f488786d..7048d310 100644 --- a/schemas/app/Payment.json +++ b/schemas/app/Payment.json @@ -16,12 +16,14 @@ "fieldname": "party", "label": "Party", "fieldtype": "Link", - "target": "Party" + "target": "Party", + "required": true }, { "fieldname": "date", "label": "Posting Date", - "fieldtype": "Date" + "fieldtype": "Date", + "required": true }, { "fieldname": "account", diff --git a/schemas/app/PurchaseInvoice.json b/schemas/app/PurchaseInvoice.json index 06433221..072406af 100644 --- a/schemas/app/PurchaseInvoice.json +++ b/schemas/app/PurchaseInvoice.json @@ -16,7 +16,8 @@ { "fieldname": "date", "label": "Date", - "fieldtype": "Date" + "fieldtype": "Date", + "required": true }, { "fieldname": "party", @@ -29,7 +30,8 @@ "fieldname": "account", "label": "Account", "fieldtype": "Link", - "target": "Account" + "target": "Account", + "required": true }, { "fieldname": "currency", diff --git a/schemas/app/SalesInvoice.json b/schemas/app/SalesInvoice.json index da0d778d..2bf1d585 100644 --- a/schemas/app/SalesInvoice.json +++ b/schemas/app/SalesInvoice.json @@ -16,7 +16,8 @@ { "fieldname": "date", "label": "Date", - "fieldtype": "Date" + "fieldtype": "Date", + "required": true }, { "fieldname": "party", @@ -29,7 +30,8 @@ "fieldname": "account", "label": "Account", "fieldtype": "Link", - "target": "Account" + "target": "Account", + "required": true }, { "fieldname": "currency", @@ -102,4 +104,4 @@ "default": "SINV-" } ] -} \ No newline at end of file +} diff --git a/src/errorHandling.ts b/src/errorHandling.ts index 9ecf7e41..ec6ca811 100644 --- a/src/errorHandling.ts +++ b/src/errorHandling.ts @@ -3,7 +3,7 @@ import { t } from 'fyo'; import { ConfigKeys } from 'fyo/core/types'; import { Doc } from 'fyo/model/doc'; import { TelemetrySetting } from 'fyo/telemetry/types'; -import { MandatoryError, ValidationError } from 'fyo/utils/errors'; +import { BaseError, MandatoryError, ValidationError } from 'fyo/utils/errors'; import { ErrorLog } from 'fyo/utils/types'; import { IPC_ACTIONS, IPC_MESSAGES } from 'utils/messages'; import { fyo } from './initFyo'; @@ -106,7 +106,8 @@ export function handleErrorWithDialog(error: Error, doc?: Doc) { const errorMessage = getErrorMessage(error, doc); handleError(false, error, { errorMessage, doc }); - showMessageDialog({ message: error.name, detail: errorMessage }); + const name = (error as BaseError).label ?? error.name; + showMessageDialog({ message: name, detail: errorMessage }); throw error; }