2
0
mirror of https://github.com/frappe/books.git synced 2024-09-20 11:29:00 +00:00

fix: Add name in errors

This commit is contained in:
Faris Ansari 2019-11-15 13:14:45 +05:30
parent f29157fb8f
commit b769cd0b4a
3 changed files with 58 additions and 38 deletions

View File

@ -537,7 +537,7 @@ module.exports = class Database extends Observable {
, 'Read Only': 'text'
, 'File': 'text'
, 'Attach': 'text'
, 'Attach Image': 'text'
, 'AttachImage': 'text'
, 'Signature': 'text'
, 'Color': 'text'
, 'Barcode': 'text'

View File

@ -237,7 +237,8 @@ module.exports = class sqliteDatabase extends Database {
this.conn.run(query, params, (err) => {
if (err) {
console.error('Error in sql:', query);
reject(err);
let Error = this.getError(err.errno);
reject(new Error());
} else {
resolve();
}
@ -291,11 +292,17 @@ module.exports = class sqliteDatabase extends Database {
, 'Read Only': 'text'
, 'File': 'text'
, 'Attach': 'text'
, 'Attach Image': 'text'
, 'AttachImage': 'text'
, 'Signature': 'text'
, 'Color': 'text'
, 'Barcode': 'text'
, 'Geolocation': 'text'
}
}
getError(errCode) {
return {
19: frappe.errors.DuplicateEntryError
}[errCode] || Error;
}
}

View File

@ -1,53 +1,66 @@
const frappe = require('frappejs');
class BaseError extends Error {
constructor(statusCode, ...params) {
super(...params);
this.statusCode = statusCode;
}
constructor(statusCode, message) {
super(message);
this.name = 'BaseError';
this.statusCode = statusCode;
this.message = message;
}
}
class ValidationError extends BaseError {
constructor(...params) {
super(417, ...params);
}
constructor(message) {
super(417, message);
this.name = 'ValidationError';
}
}
class NotFound extends BaseError {
constructor(...params) {
super(404, ...params);
}
class NotFoundError extends BaseError {
constructor(message) {
super(404, message);
this.name = 'NotFoundError';
}
}
class Forbidden extends BaseError {
constructor(...params) {
super(403, ...params);
}
class ForbiddenError extends BaseError {
constructor(message) {
super(403, message);
this.name = 'ForbiddenError';
}
}
class ValueError extends ValidationError { }
class Conflict extends ValidationError { }
class DuplicateEntryError extends ValidationError {
constructor(message) {
super(message);
this.name = 'DuplicateEntryError';
}
}
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;
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 = {
ValidationError,
ValueError,
Conflict,
NotFound,
Forbidden,
throw: throwError
}
ValidationError,
ValueError,
Conflict,
NotFoundError,
ForbiddenError,
DuplicateEntryError,
throw: throwError
};