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

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

View File

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