Start moving db code to use BoxError as well

This commit is contained in:
Girish Ramakrishnan
2019-10-24 11:13:48 -07:00
parent ec216d9828
commit a017af41c5
30 changed files with 396 additions and 447 deletions

View File

@@ -26,7 +26,6 @@ let assert = require('assert'),
BoxError = require('./boxerror.js'),
changelog = require('./changelog.js'),
custom = require('./custom.js'),
DatabaseError = require('./databaseerror.js'),
debug = require('debug')('box:notifications'),
eventlog = require('./eventlog.js'),
mailer = require('./mailer.js'),
@@ -50,8 +49,7 @@ function add(userId, eventId, title, message, callback) {
message: message,
acknowledged: false
}, function (error, result) {
if (error && error.reason === DatabaseError.NOT_FOUND) return callback(new BoxError(BoxError.NOT_FOUND, error.message));
if (error) return callback(new BoxError(BoxError.DATABASE_ERROR, error));
if (error) return callback(error);
callback(null, { id: result });
});
@@ -62,8 +60,7 @@ function get(id, callback) {
assert.strictEqual(typeof callback, 'function');
notificationdb.get(id, function (error, result) {
if (error && error.reason === DatabaseError.NOT_FOUND) return callback(new BoxError(BoxError.NOT_FOUND));
if (error) return callback(new BoxError(BoxError.DATABASE_ERROR, error));
if (error) return callback(error);
callback(null, result);
});
@@ -74,8 +71,7 @@ function ack(id, callback) {
assert.strictEqual(typeof callback, 'function');
notificationdb.update(id, { acknowledged: true }, function (error) {
if (error && error.reason === DatabaseError.NOT_FOUND) return callback(new BoxError(BoxError.NOT_FOUND));
if (error) return callback(new BoxError(BoxError.DATABASE_ERROR, error));
if (error) return callback(error);
callback(null);
});
@@ -310,15 +306,14 @@ function alert(id, title, message, callback) {
};
notificationdb.getByUserIdAndTitle(admin.id, title, function (error, result) {
if (error && error.reason !== DatabaseError.NOT_FOUND) return callback(new BoxError(BoxError.DATABASE_ERROR, error));
if (error && error.reason !== BoxError.NOT_FOUND) return callback(error);
if (!result && acknowledged) return callback(); // do not add acked alerts
let updateFunc = !result ? notificationdb.add.bind(null, data) : notificationdb.update.bind(null, result.id, data);
updateFunc(function (error) {
if (error && error.reason === DatabaseError.NOT_FOUND) return callback(new BoxError(BoxError.NOT_FOUND, error.message));
if (error) return callback(new BoxError(BoxError.DATABASE_ERROR, error));
if (error) return callback(error);
callback(null);
});