2
0
mirror of https://github.com/frappe/books.git synced 2024-12-25 20:11:15 +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 frappe = require('frappejs');
const sqlite3 = require('sqlite3').verbose(); const sqlite3 = require('sqlite3').verbose();
const Database = require('./database'); const Database = require('./database');
const errors = require('frappejs/common/errors');
const debug = false; const debug = false;
module.exports = class sqliteDatabase extends Database { class SqliteDatabase extends Database {
constructor({ dbPath }) { constructor({ dbPath }) {
super(); super();
this.dbPath = dbPath; this.dbPath = dbPath;
@ -237,7 +238,7 @@ 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);
let Error = this.getError(err.errno); let Error = this.getError(err);
reject(new Error()); reject(new Error());
} else { } else {
resolve(); resolve();
@ -262,7 +263,7 @@ module.exports = class sqliteDatabase extends Database {
try { try {
await this.run('commit'); await this.run('commit');
} catch (e) { } catch (e) {
if (e.errno !== 1) { if (e.name !== 'CannotCommitError') {
throw e; 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 { return {
19: frappe.errors.DuplicateEntryError 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 ValueError extends ValidationError {}
class Conflict extends ValidationError {} class Conflict extends ValidationError {}
@ -56,11 +70,14 @@ function throwError(message, error = 'ValidationError') {
frappe.throw = throwError; frappe.throw = throwError;
module.exports = { module.exports = {
BaseError,
ValidationError, ValidationError,
ValueError, ValueError,
Conflict, Conflict,
NotFoundError, NotFoundError,
ForbiddenError, ForbiddenError,
DuplicateEntryError, DuplicateEntryError,
LinkValidationError,
DatabaseError,
throw: throwError throw: throwError
}; };