mirror of
https://github.com/frappe/books.git
synced 2024-11-10 15:50:56 +00:00
84 lines
1.7 KiB
JavaScript
84 lines
1.7 KiB
JavaScript
const frappe = require('frappejs');
|
|
|
|
class BaseError extends Error {
|
|
constructor(statusCode, message) {
|
|
super(message);
|
|
this.name = 'BaseError';
|
|
this.statusCode = statusCode;
|
|
this.message = message;
|
|
}
|
|
}
|
|
|
|
class ValidationError extends BaseError {
|
|
constructor(message) {
|
|
super(417, message);
|
|
this.name = 'ValidationError';
|
|
}
|
|
}
|
|
|
|
class NotFoundError extends BaseError {
|
|
constructor(message) {
|
|
super(404, message);
|
|
this.name = 'NotFoundError';
|
|
}
|
|
}
|
|
|
|
class ForbiddenError extends BaseError {
|
|
constructor(message) {
|
|
super(403, message);
|
|
this.name = 'ForbiddenError';
|
|
}
|
|
}
|
|
|
|
class DuplicateEntryError extends ValidationError {
|
|
constructor(message) {
|
|
super(message);
|
|
this.name = 'DuplicateEntryError';
|
|
}
|
|
}
|
|
|
|
class LinkValidationError extends ValidationError {
|
|
constructor(message) {
|
|
super(message);
|
|
this.name = 'LinkValidationError';
|
|
}
|
|
}
|
|
|
|
class DatabaseError extends BaseError {
|
|
constructor(message) {
|
|
super(500, message);
|
|
this.name = 'DatabaseError';
|
|
}
|
|
}
|
|
|
|
class ValueError extends ValidationError {}
|
|
class Conflict extends ValidationError {}
|
|
|
|
function throwError(message, error = 'ValidationError') {
|
|
const errorClass = {
|
|
ValidationError: ValidationError,
|
|
NotFoundError: NotFoundError,
|
|
ForbiddenError: ForbiddenError,
|
|
ValueError: ValueError,
|
|
Conflict: Conflict
|
|
};
|
|
const err = new errorClass[error](message);
|
|
frappe.events.trigger('throw', { message, stackTrace: err.stack });
|
|
throw err;
|
|
}
|
|
|
|
frappe.throw = throwError;
|
|
|
|
module.exports = {
|
|
BaseError,
|
|
ValidationError,
|
|
ValueError,
|
|
Conflict,
|
|
NotFoundError,
|
|
ForbiddenError,
|
|
DuplicateEntryError,
|
|
LinkValidationError,
|
|
DatabaseError,
|
|
throw: throwError
|
|
};
|