2
0
mirror of https://github.com/frappe/books.git synced 2024-12-26 12:28:12 +00:00
books/common/errors.js

102 lines
2.1 KiB
JavaScript
Raw Normal View History

const frappe = require('frappejs');
class BaseError extends Error {
2019-11-15 07:44:45 +00:00
constructor(statusCode, message) {
super(message);
this.name = 'BaseError';
this.statusCode = statusCode;
this.message = message;
}
}
class ValidationError extends BaseError {
2019-11-15 07:44:45 +00:00
constructor(message) {
super(417, message);
this.name = 'ValidationError';
}
2018-07-17 19:29:18 +00:00
}
2019-11-15 07:44:45 +00:00
class NotFoundError extends BaseError {
constructor(message) {
super(404, message);
this.name = 'NotFoundError';
}
}
2019-11-15 07:44:45 +00:00
class ForbiddenError extends BaseError {
constructor(message) {
super(403, message);
this.name = 'ForbiddenError';
}
2018-07-17 19:29:18 +00:00
}
2019-11-15 07:44:45 +00:00
class DuplicateEntryError extends ValidationError {
constructor(message) {
super(message);
this.name = 'DuplicateEntryError';
}
}
2019-11-20 09:38:32 +00:00
class LinkValidationError extends ValidationError {
constructor(message) {
super(message);
this.name = 'LinkValidationError';
}
}
2019-12-04 18:43:08 +00:00
class MandatoryError extends ValidationError {
constructor(message) {
super(message);
this.name = 'MandatoryError';
}
}
2019-11-20 09:38:32 +00:00
class DatabaseError extends BaseError {
constructor(message) {
super(500, message);
this.name = 'DatabaseError';
}
}
2019-12-09 19:57:26 +00:00
class CannotCommitError extends DatabaseError {
constructor(message) {
super(message);
this.name = 'CannotCommitError';
}
}
2019-11-15 07:44:45 +00:00
class ValueError extends ValidationError {}
class Conflict extends ValidationError {}
2019-12-20 06:22:16 +00:00
class InvalidFieldError extends ValidationError {}
2019-11-15 07:44:45 +00:00
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 = {
2019-11-20 09:38:32 +00:00
BaseError,
2019-11-15 07:44:45 +00:00
ValidationError,
ValueError,
Conflict,
NotFoundError,
ForbiddenError,
DuplicateEntryError,
2019-11-20 09:38:32 +00:00
LinkValidationError,
DatabaseError,
2019-12-09 19:57:26 +00:00
CannotCommitError,
2019-12-04 18:43:08 +00:00
MandatoryError,
2019-12-20 06:22:16 +00:00
InvalidFieldError,
2019-11-15 07:44:45 +00:00
throw: throwError
};