From b769cd0b4aa8dbcbb7b627a0b8ee9554dac8be00 Mon Sep 17 00:00:00 2001 From: Faris Ansari Date: Fri, 15 Nov 2019 13:14:45 +0530 Subject: [PATCH] fix: Add name in errors --- backends/database.js | 2 +- backends/sqlite.js | 11 ++++-- common/errors.js | 83 +++++++++++++++++++++++++------------------- 3 files changed, 58 insertions(+), 38 deletions(-) diff --git a/backends/database.js b/backends/database.js index caef28fa..edcb5dd0 100644 --- a/backends/database.js +++ b/backends/database.js @@ -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' diff --git a/backends/sqlite.js b/backends/sqlite.js index c5472012..00e51c44 100644 --- a/backends/sqlite.js +++ b/backends/sqlite.js @@ -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; + } } diff --git a/common/errors.js b/common/errors.js index fdb06a0f..7daad6a2 100644 --- a/common/errors.js +++ b/common/errors.js @@ -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 +};