2
0
mirror of https://github.com/frappe/books.git synced 2024-11-10 07:40:55 +00:00
books/fyo/utils/errors.ts
18alantom 3975eb3f64 fix(ux): display can't delete cause link
- update reports on ledger entries
- pull data until .plus({days: 1}) to account for all entries of the day
2022-05-30 17:04:25 +05:30

103 lines
2.2 KiB
TypeScript

export class BaseError extends Error {
message: string;
statusCode: number;
constructor(statusCode: number, message: string) {
super(message);
this.name = 'BaseError';
this.statusCode = statusCode;
this.message = message;
}
}
export class ValidationError extends BaseError {
constructor(message: string) {
super(417, message);
this.name = 'ValidationError';
}
}
export class NotFoundError extends BaseError {
constructor(message: string) {
super(404, message);
this.name = 'NotFoundError';
}
}
export class ForbiddenError extends BaseError {
constructor(message: string) {
super(403, message);
this.name = 'ForbiddenError';
}
}
export class DuplicateEntryError extends ValidationError {
constructor(message: string) {
super(message);
this.name = 'DuplicateEntryError';
}
}
export class LinkValidationError extends ValidationError {
constructor(message: string) {
super(message);
this.name = 'LinkValidationError';
}
}
export class MandatoryError extends ValidationError {
constructor(message: string) {
super(message);
this.name = 'MandatoryError';
}
}
export class DatabaseError extends BaseError {
constructor(message: string) {
super(500, message);
this.name = 'DatabaseError';
}
}
export class CannotCommitError extends DatabaseError {
constructor(message: string) {
super(message);
this.name = 'CannotCommitError';
}
}
export class NotImplemented extends BaseError {
constructor() {
super(501, '');
this.name = 'NotImplemented';
}
}
export class ValueError extends ValidationError {}
export class ConflictError extends ValidationError {}
export class InvalidFieldError extends ValidationError {}
export function getDbError(err: Error) {
if (!err.message) {
return DatabaseError;
}
if (err.message.includes('SQLITE_ERROR: no such table')) {
return NotFoundError;
}
if (err.message.includes('FOREIGN KEY')) {
return LinkValidationError;
}
if (err.message.includes('SQLITE_ERROR: cannot commit')) {
return CannotCommitError;
}
if (err.message.includes('SQLITE_CONSTRAINT: UNIQUE constraint failed:')) {
return DuplicateEntryError;
}
return DatabaseError;
}