2
0
mirror of https://github.com/frappe/books.git synced 2025-01-27 00:58:35 +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' , 'Read Only': 'text'
, 'File': 'text' , 'File': 'text'
, 'Attach': 'text' , 'Attach': 'text'
, 'Attach Image': 'text' , 'AttachImage': 'text'
, 'Signature': 'text' , 'Signature': 'text'
, 'Color': 'text' , 'Color': 'text'
, 'Barcode': 'text' , 'Barcode': 'text'

View File

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

View File

@ -1,53 +1,66 @@
const frappe = require('frappejs'); const frappe = require('frappejs');
class BaseError extends Error { class BaseError extends Error {
constructor(statusCode, ...params) { constructor(statusCode, message) {
super(...params); super(message);
this.statusCode = statusCode; this.name = 'BaseError';
} this.statusCode = statusCode;
this.message = message;
}
} }
class ValidationError extends BaseError { class ValidationError extends BaseError {
constructor(...params) { constructor(message) {
super(417, ...params); super(417, message);
} this.name = 'ValidationError';
}
} }
class NotFound extends BaseError { class NotFoundError extends BaseError {
constructor(...params) { constructor(message) {
super(404, ...params); super(404, message);
} this.name = 'NotFoundError';
}
} }
class Forbidden extends BaseError { class ForbiddenError extends BaseError {
constructor(...params) { constructor(message) {
super(403, ...params); super(403, message);
} this.name = 'ForbiddenError';
}
} }
class ValueError extends ValidationError { } class DuplicateEntryError extends ValidationError {
class Conflict extends ValidationError { } constructor(message) {
super(message);
this.name = 'DuplicateEntryError';
}
}
function throwError(message, error='ValidationError') { class ValueError extends ValidationError {}
const errorClass = { class Conflict extends ValidationError {}
'ValidationError': ValidationError,
'NotFound': NotFound, function throwError(message, error = 'ValidationError') {
'Forbidden': Forbidden, const errorClass = {
'ValueError': ValueError, ValidationError: ValidationError,
'Conflict': Conflict NotFoundError: NotFoundError,
}; ForbiddenError: ForbiddenError,
const err = new errorClass[error](message); ValueError: ValueError,
frappe.events.trigger('throw', { message, stackTrace: err.stack }); Conflict: Conflict
throw err; };
const err = new errorClass[error](message);
frappe.events.trigger('throw', { message, stackTrace: err.stack });
throw err;
} }
frappe.throw = throwError; frappe.throw = throwError;
module.exports = { module.exports = {
ValidationError, ValidationError,
ValueError, ValueError,
Conflict, Conflict,
NotFound, NotFoundError,
Forbidden, ForbiddenError,
throw: throwError DuplicateEntryError,
} throw: throwError
};