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

54 lines
1.1 KiB
JavaScript
Raw Normal View History

const frappe = require('frappejs');
class BaseError extends Error {
2018-07-17 19:29:18 +00:00
constructor(statusCode, ...params) {
2018-02-21 09:43:21 +00:00
super(...params);
2018-07-17 19:29:18 +00:00
this.statusCode = statusCode;
2018-02-21 09:43:21 +00:00
}
}
class ValidationError extends BaseError {
2018-07-17 19:29:18 +00:00
constructor(...params) {
super(417, ...params);
}
}
class NotFound extends BaseError {
constructor(...params) {
super(404, ...params);
}
}
2018-07-17 19:29:18 +00:00
class Forbidden extends BaseError {
constructor(...params) {
super(403, ...params);
}
}
class ValueError extends ValidationError { }
class Conflict extends ValidationError { }
function throwError(message, error='ValidationError') {
const errorClass = {
'ValidationError': ValidationError,
'NotFound': NotFound,
'Forbidden': Forbidden,
'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 = {
2018-07-17 19:29:18 +00:00
ValidationError,
ValueError,
Conflict,
NotFound,
Forbidden,
throw: throwError
}