From 8425cb894d4867d26fd5f7fae7862b0669f3c717 Mon Sep 17 00:00:00 2001 From: Chocobozzz Date: Fri, 5 Feb 2016 19:02:05 +0100 Subject: Error handling mini refractoring --- models/pods.js | 2 +- models/poolRequests.js | 19 +++++++++++++------ models/videos.js | 20 ++++++++++---------- 3 files changed, 24 insertions(+), 17 deletions(-) (limited to 'models') diff --git a/models/pods.js b/models/pods.js index 395b1e0b1..04fd042c3 100644 --- a/models/pods.js +++ b/models/pods.js @@ -61,7 +61,7 @@ function list (callback) { PodsDB.find(function (err, pods_list) { if (err) { - logger.error('Cannot get the list of the pods.', { error: err }) + logger.error('Cannot get the list of the pods.') return callback(err) } diff --git a/models/poolRequests.js b/models/poolRequests.js index 0f488ef04..962e75e6a 100644 --- a/models/poolRequests.js +++ b/models/poolRequests.js @@ -26,21 +26,28 @@ logger.debug('Add request to the pool requests.', { id: id, type: type, request: request }) PoolRequestsDB.findOne({ id: id }, function (err, entity) { - if (err) logger.error(err) + if (err) { + logger.error('Cannot find one pool request.', { error: err }) + return // Abort + } if (entity) { if (entity.type === type) { - logger.error(new Error('Cannot insert two same requests.')) - return + logger.error('Cannot insert two same requests.') + return // Abort } // Remove the request of the other type PoolRequestsDB.remove({ id: id }, function (err) { - if (err) logger.error(err) + if (err) { + logger.error('Cannot remove a pool request.', { error: err }) + return // Abort + } }) } else { PoolRequestsDB.create({ id: id, type: type, request: request }, function (err) { - if (err) logger.error(err) + logger.error('Cannot create a pool request.', { error: err }) + return // Abort }) } }) @@ -54,7 +61,7 @@ PoolRequestsDB.remove({ _id: { $in: ids } }, function (err) { if (err) { logger.error('Cannot remove requests from the pool requests database.', { error: err }) - return + return // Abort } logger.info('Pool requests flushed.') diff --git a/models/videos.js b/models/videos.js index 10abee6e7..6ea628373 100644 --- a/models/videos.js +++ b/models/videos.js @@ -50,7 +50,7 @@ VideosDB.create(params, function (err, video) { if (err) { - logger.error('Cannot insert this video into database.', { error: err }) + logger.error('Cannot insert this video into database.') return callback(err) } @@ -82,7 +82,7 @@ }, function () { VideosDB.create(to_add, function (err, videos) { if (err) { - logger.error('Cannot insert this remote video.', { error: err }) + logger.error('Cannot insert this remote video.') return callback(err) } @@ -94,7 +94,7 @@ function get (id, callback) { VideosDB.findById(id, function (err, video) { if (err) { - logger.error('Cannot get this video.', { error: err }) + logger.error('Cannot get this video.') return callback(err) } @@ -120,14 +120,14 @@ VideosDB.findById(id, function (err, video) { if (err || !video) { if (!err) err = new Error('Cannot find this video.') - logger.error('Cannot find this video.', { error: err }) + logger.error('Cannot find this video.') return callback(err) } if (video.namePath === null) { var error_string = 'Cannot remove the video of another pod.' logger.error(error_string) - return callback(null, false, video) + return callback(new Error(error_string), false, video) } callback(null, true, video) @@ -137,7 +137,7 @@ function list (callback) { VideosDB.find(function (err, videos_list) { if (err) { - logger.error('Cannot get list of the videos.', { error: err }) + logger.error('Cannot get the list of the videos.') return callback(err) } @@ -149,7 +149,7 @@ // If namePath is not null this is *our* video VideosDB.find({ namePath: { $ne: null } }, function (err, videos_list) { if (err) { - logger.error('Cannot get list of the videos.', { error: err }) + logger.error('Cannot get the list of owned videos.') return callback(err) } @@ -160,13 +160,13 @@ function removeOwned (id, callback) { VideosDB.findByIdAndRemove(id, function (err, video) { if (err) { - logger.error('Cannot remove the torrent.', { error: err }) + logger.error('Cannot remove the torrent.') return callback(err) } fs.unlink(uploadDir + video.namePath, function (err) { if (err) { - logger.error('Cannot remove this video file.', { error: err }) + logger.error('Cannot remove this video file.') return callback(err) } @@ -222,7 +222,7 @@ function search (name, callback) { VideosDB.find({ name: new RegExp(name) }, function (err, videos) { if (err) { - logger.error('Cannot search the videos.', { error: err }) + logger.error('Cannot search the videos.') return callback(err) } -- cgit v1.2.3