2
0
mirror of https://github.com/frappe/books.git synced 2024-11-10 15:50:56 +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

@ -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();
} }
@ -298,4 +299,10 @@ module.exports = class sqliteDatabase extends Database {
, 'Geolocation': 'text' , 'Geolocation': 'text'
} }
} }
getError(errCode) {
return {
19: frappe.errors.DuplicateEntryError
}[errCode] || Error;
}
} }

View File

@ -1,27 +1,39 @@
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.name = 'BaseError';
this.statusCode = statusCode; 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 DuplicateEntryError extends ValidationError {
constructor(message) {
super(message);
this.name = 'DuplicateEntryError';
} }
} }
@ -30,11 +42,11 @@ class Conflict extends ValidationError { }
function throwError(message, error = 'ValidationError') { function throwError(message, error = 'ValidationError') {
const errorClass = { const errorClass = {
'ValidationError': ValidationError, ValidationError: ValidationError,
'NotFound': NotFound, NotFoundError: NotFoundError,
'Forbidden': Forbidden, ForbiddenError: ForbiddenError,
'ValueError': ValueError, ValueError: ValueError,
'Conflict': Conflict Conflict: Conflict
}; };
const err = new errorClass[error](message); const err = new errorClass[error](message);
frappe.events.trigger('throw', { message, stackTrace: err.stack }); frappe.events.trigger('throw', { message, stackTrace: err.stack });
@ -47,7 +59,8 @@ module.exports = {
ValidationError, ValidationError,
ValueError, ValueError,
Conflict, Conflict,
NotFound, NotFoundError,
Forbidden, ForbiddenError,
DuplicateEntryError,
throw: throwError throw: throwError
} };