2
0
mirror of https://github.com/frappe/books.git synced 2024-11-10 15:50:56 +00:00

feat: Throw better errors

This commit is contained in:
Faris Ansari 2019-11-20 15:08:32 +05:30
parent 03dee5913c
commit 72b64e818e
2 changed files with 38 additions and 5 deletions

View File

@ -1,9 +1,10 @@
const frappe = require('frappejs');
const sqlite3 = require('sqlite3').verbose();
const Database = require('./database');
const errors = require('frappejs/common/errors');
const debug = false;
module.exports = class sqliteDatabase extends Database {
class SqliteDatabase extends Database {
constructor({ dbPath }) {
super();
this.dbPath = dbPath;
@ -237,7 +238,7 @@ module.exports = class sqliteDatabase extends Database {
this.conn.run(query, params, (err) => {
if (err) {
console.error('Error in sql:', query);
let Error = this.getError(err.errno);
let Error = this.getError(err);
reject(new Error());
} else {
resolve();
@ -262,7 +263,7 @@ module.exports = class sqliteDatabase extends Database {
try {
await this.run('commit');
} catch (e) {
if (e.errno !== 1) {
if (e.name !== 'CannotCommitError') {
throw e;
}
}
@ -300,9 +301,24 @@ module.exports = class sqliteDatabase extends Database {
}
}
getError(errCode) {
getError(err) {
if (err.message.includes('FOREIGN KEY')) {
return frappe.errors.LinkValidationError;
}
if (err.message.includes('SQLITE_ERROR: cannot commit')) {
return SqliteDatabase.CannotCommitError;
}
return {
19: frappe.errors.DuplicateEntryError
}[errCode] || Error;
}[err.errno] || Error;
}
}
SqliteDatabase.CannotCommitError = class CannotCommitError extends errors.DatabaseError {
constructor(message) {
super(message);
this.name = 'CannotCommitError';
}
}
module.exports = SqliteDatabase;

View File

@ -37,6 +37,20 @@ class DuplicateEntryError extends ValidationError {
}
}
class LinkValidationError extends ValidationError {
constructor(message) {
super(message);
this.name = 'LinkValidationError';
}
}
class DatabaseError extends BaseError {
constructor(message) {
super(500, message);
this.name = 'DatabaseError';
}
}
class ValueError extends ValidationError {}
class Conflict extends ValidationError {}
@ -56,11 +70,14 @@ function throwError(message, error = 'ValidationError') {
frappe.throw = throwError;
module.exports = {
BaseError,
ValidationError,
ValueError,
Conflict,
NotFoundError,
ForbiddenError,
DuplicateEntryError,
LinkValidationError,
DatabaseError,
throw: throwError
};