2022-03-21 05:47:26 +00:00
|
|
|
export class BaseError extends Error {
|
2022-08-29 12:35:33 +00:00
|
|
|
more: Record<string, unknown> = {};
|
2022-03-21 05:47:26 +00:00
|
|
|
message: string;
|
|
|
|
statusCode: number;
|
2022-07-28 07:55:48 +00:00
|
|
|
shouldStore: boolean;
|
2022-03-21 05:47:26 +00:00
|
|
|
|
2023-06-22 08:52:54 +00:00
|
|
|
constructor(statusCode: number, message: string, shouldStore = true) {
|
2022-03-21 05:47:26 +00:00
|
|
|
super(message);
|
|
|
|
this.name = 'BaseError';
|
|
|
|
this.statusCode = statusCode;
|
|
|
|
this.message = message;
|
2022-07-28 07:55:48 +00:00
|
|
|
this.shouldStore = shouldStore;
|
2022-03-21 05:47:26 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export class ValidationError extends BaseError {
|
2023-06-22 08:52:54 +00:00
|
|
|
constructor(message: string, shouldStore = false) {
|
2022-07-28 07:55:48 +00:00
|
|
|
super(417, message, shouldStore);
|
2022-03-21 05:47:26 +00:00
|
|
|
this.name = 'ValidationError';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export class NotFoundError extends BaseError {
|
2023-06-22 08:52:54 +00:00
|
|
|
constructor(message: string, shouldStore = true) {
|
2022-07-28 07:55:48 +00:00
|
|
|
super(404, message, shouldStore);
|
2022-03-21 05:47:26 +00:00
|
|
|
this.name = 'NotFoundError';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export class ForbiddenError extends BaseError {
|
2023-06-22 08:52:54 +00:00
|
|
|
constructor(message: string, shouldStore = true) {
|
2022-07-28 07:55:48 +00:00
|
|
|
super(403, message, shouldStore);
|
2022-03-21 05:47:26 +00:00
|
|
|
this.name = 'ForbiddenError';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export class DuplicateEntryError extends ValidationError {
|
2023-06-22 08:52:54 +00:00
|
|
|
constructor(message: string, shouldStore = false) {
|
2022-07-28 07:55:48 +00:00
|
|
|
super(message, shouldStore);
|
2022-03-21 05:47:26 +00:00
|
|
|
this.name = 'DuplicateEntryError';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export class LinkValidationError extends ValidationError {
|
2023-06-22 08:52:54 +00:00
|
|
|
constructor(message: string, shouldStore = false) {
|
2022-07-28 07:55:48 +00:00
|
|
|
super(message, shouldStore);
|
2022-03-21 05:47:26 +00:00
|
|
|
this.name = 'LinkValidationError';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export class MandatoryError extends ValidationError {
|
2023-06-22 08:52:54 +00:00
|
|
|
constructor(message: string, shouldStore = false) {
|
2022-07-28 07:55:48 +00:00
|
|
|
super(message, shouldStore);
|
2022-03-21 05:47:26 +00:00
|
|
|
this.name = 'MandatoryError';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export class DatabaseError extends BaseError {
|
2023-06-22 08:52:54 +00:00
|
|
|
constructor(message: string, shouldStore = true) {
|
2022-07-28 07:55:48 +00:00
|
|
|
super(500, message, shouldStore);
|
2022-03-21 05:47:26 +00:00
|
|
|
this.name = 'DatabaseError';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export class CannotCommitError extends DatabaseError {
|
2023-06-22 08:52:54 +00:00
|
|
|
constructor(message: string, shouldStore = true) {
|
2022-07-28 07:55:48 +00:00
|
|
|
super(message, shouldStore);
|
2022-03-21 05:47:26 +00:00
|
|
|
this.name = 'CannotCommitError';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-05-20 10:06:38 +00:00
|
|
|
export class NotImplemented extends BaseError {
|
2023-06-22 08:52:54 +00:00
|
|
|
constructor(message = '', shouldStore = false) {
|
2022-07-28 07:55:48 +00:00
|
|
|
super(501, message, shouldStore);
|
2022-05-20 10:06:38 +00:00
|
|
|
this.name = 'NotImplemented';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-21 05:47:26 +00:00
|
|
|
export class ValueError extends ValidationError {}
|
2022-04-25 06:33:31 +00:00
|
|
|
export class ConflictError extends ValidationError {}
|
2022-03-21 05:47:26 +00:00
|
|
|
export class InvalidFieldError extends ValidationError {}
|
2022-05-30 11:34:25 +00:00
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2022-08-29 12:35:33 +00:00
|
|
|
if (err.message.includes('UNIQUE constraint failed:')) {
|
2022-05-30 11:34:25 +00:00
|
|
|
return DuplicateEntryError;
|
|
|
|
}
|
|
|
|
|
|
|
|
return DatabaseError;
|
|
|
|
}
|