Error handling mini refractoring

This commit is contained in:
Chocobozzz 2016-02-05 19:02:05 +01:00
parent dac0a5319a
commit 8425cb894d
12 changed files with 86 additions and 58 deletions

View File

@ -39,13 +39,13 @@
fs.readFile(peertubeCrypto.getCertDir() + 'peertube.pub', 'utf8', function (err, cert) { fs.readFile(peertubeCrypto.getCertDir() + 'peertube.pub', 'utf8', function (err, cert) {
if (err) { if (err) {
logger.error('Cannot read cert file.', { error: err }) logger.error('Cannot read cert file.')
return next(err) return next(err)
} }
Videos.listOwned(function (err, videos_list) { Videos.listOwned(function (err, videos_list) {
if (err) { if (err) {
logger.error('Cannot get the list of owned videos.', { error: err }) logger.error('Cannot get the list of owned videos.')
return next(err) return next(err)
} }
@ -78,7 +78,8 @@
Videos.removeAllRemotesOf(url, function (err) { Videos.removeAllRemotesOf(url, function (err) {
if (err) logger.error('Cannot remove all remote videos of %s.', url) if (err) logger.error('Cannot remove all remote videos of %s.', url)
logger.info('%s pod removed.', url) else logger.info('%s pod removed.', url)
res.sendStatus(204) res.sendStatus(204)
}) })
}) })

View File

@ -56,7 +56,7 @@
videos.seed(video_file.path, function (err, torrent) { videos.seed(video_file.path, function (err, torrent) {
if (err) { if (err) {
logger.error('Cannot seed this video.', { error: err }) logger.error('Cannot seed this video.')
return next(err) return next(err)
} }
@ -70,7 +70,7 @@
Videos.add(video_data, function (err) { Videos.add(video_data, function (err) {
if (err) { if (err) {
// TODO unseed the video // TODO unseed the video
logger.error('Cannot insert this video in the database.', { error: err }) logger.error('Cannot insert this video in the database.')
return next(err) return next(err)
} }

View File

@ -101,7 +101,7 @@
logger.info('Generating a RSA key...') logger.info('Generating a RSA key...')
openssl.exec('genrsa', { 'out': certDir + 'peertube.key.pem', '2048': false }, function (err) { openssl.exec('genrsa', { 'out': certDir + 'peertube.key.pem', '2048': false }, function (err) {
if (err) { if (err) {
logger.error('Cannot create private key on this pod.', { error: err }) logger.error('Cannot create private key on this pod.')
return callback(err) return callback(err)
} }
logger.info('RSA key generated.') logger.info('RSA key generated.')
@ -109,7 +109,7 @@
logger.info('Manage public key...') logger.info('Manage public key...')
openssl.exec('rsa', { 'in': certDir + 'peertube.key.pem', 'pubout': true, 'out': certDir + 'peertube.pub' }, function (err) { openssl.exec('rsa', { 'in': certDir + 'peertube.key.pem', 'pubout': true, 'out': certDir + 'peertube.pub' }, function (err) {
if (err) { if (err) {
logger.error('Cannot create public key on this pod .', { error: err }) logger.error('Cannot create public key on this pod.')
return callback(err) return callback(err)
} }
@ -122,9 +122,7 @@
function generatePassword (callback) { function generatePassword (callback) {
crypto.randomBytes(32, function (err, buf) { crypto.randomBytes(32, function (err, buf) {
if (err) { if (err) return callback(err)
return callback(err)
}
callback(null, buf.toString('utf8')) callback(null, buf.toString('utf8'))
}) })
@ -139,9 +137,7 @@
function symetricEncrypt (text, callback) { function symetricEncrypt (text, callback) {
generatePassword(function (err, password) { generatePassword(function (err, password) {
if (err) { if (err) return callback(err)
return callback(err)
}
var cipher = crypto.createCipher(algorithm, password) var cipher = crypto.createCipher(algorithm, password)
var crypted = cipher.update(text, 'utf8', 'hex') var crypted = cipher.update(text, 'utf8', 'hex')

View File

@ -8,7 +8,7 @@
} }
function cleanForExit (webtorrent_process) { function cleanForExit (webtorrent_process) {
logger.info('Gracefully exiting') logger.info('Gracefully exiting.')
process.kill(-webtorrent_process.pid) process.kill(-webtorrent_process.pid)
} }

View File

@ -30,8 +30,7 @@
function addVideoToFriends (video) { function addVideoToFriends (video) {
// To avoid duplicates // To avoid duplicates
var id = video.name + video.magnetUri var id = video.name + video.magnetUri
// namePath is null // ensure namePath is null
// TODO
video.namePath = null video.namePath = null
PoolRequests.addRequest(id, 'add', video) PoolRequests.addRequest(id, 'add', video)
} }
@ -51,13 +50,15 @@
logger.info('Make friends!') logger.info('Make friends!')
fs.readFile(peertubeCrypto.getCertDir() + 'peertube.pub', 'utf8', function (err, cert) { fs.readFile(peertubeCrypto.getCertDir() + 'peertube.pub', 'utf8', function (err, cert) {
if (err) { if (err) {
logger.error('Cannot read public cert.', { error: err }) logger.error('Cannot read public cert.')
return callback(err) return callback(err)
} }
var urls = config.get('network.friends') var urls = config.get('network.friends')
async.each(urls, computeForeignPodsList, function () { async.each(urls, computeForeignPodsList, function (err) {
if (err) return callback(err)
logger.debug('Pods scores computed.', { pods_score: pods_score }) logger.debug('Pods scores computed.', { pods_score: pods_score })
var pods_list = computeWinningPods(urls, pods_score) var pods_list = computeWinningPods(urls, pods_score)
logger.debug('Pods that we keep computed.', { pods_to_keep: pods_list }) logger.debug('Pods that we keep computed.', { pods_to_keep: pods_list })
@ -72,7 +73,8 @@
// Let's give 1 point to the pod we ask the friends list // Let's give 1 point to the pod we ask the friends list
pods_score[url] = 1 pods_score[url] = 1
getForeignPodsList(url, function (foreign_pods_list) { getForeignPodsList(url, function (err, foreign_pods_list) {
if (err) return callback(err)
if (foreign_pods_list.length === 0) return callback() if (foreign_pods_list.length === 0) return callback()
async.each(foreign_pods_list, function (foreign_pod, callback_each) { async.each(foreign_pods_list, function (foreign_pod, callback_each) {
@ -108,7 +110,10 @@
// Get the list of our videos to send to our new friends // Get the list of our videos to send to our new friends
Videos.listOwned(function (err, videos_list) { Videos.listOwned(function (err, videos_list) {
if (err) throw err if (err) {
logger.error('Cannot get the list of videos we own.')
return callback(err)
}
var data = { var data = {
url: http + '://' + host + ':' + port, url: http + '://' + host + ':' + port,
@ -145,7 +150,7 @@
poolRequests.activate() poolRequests.activate()
if (err) { if (err) {
logger.error('There was some errors when we wanted to make friends.', { error: err }) logger.error('There was some errors when we wanted to make friends.')
return callback(err) return callback(err)
} }
@ -212,8 +217,9 @@
var path = '/api/' + constants.API_VERSION + '/pods' var path = '/api/' + constants.API_VERSION + '/pods'
request.get(url + path, function (err, response, body) { request.get(url + path, function (err, response, body) {
if (err) throw err if (err) return callback(err)
callback(JSON.parse(body))
callback(null, JSON.parse(body))
}) })
} }
})() })()

View File

@ -44,7 +44,7 @@
if (!callback) callback = function () {} if (!callback) callback = function () {}
Pods.list(function (err, pods) { Pods.list(function (err, pods) {
if (err) throw err if (err) return callback(err)
var params = { var params = {
encrypt: true, encrypt: true,
@ -59,7 +59,7 @@
} else if (type === 'remove') { } else if (type === 'remove') {
params.path = '/api/' + constants.API_VERSION + '/remotevideos/remove' params.path = '/api/' + constants.API_VERSION + '/remotevideos/remove'
} else { } else {
throw new Error('Unkown pool request type.') return callback(new Error('Unkown pool request type.'))
} }
var bad_pods = [] var bad_pods = []
@ -91,7 +91,10 @@
logger.info('Making pool requests to friends.') logger.info('Making pool requests to friends.')
PoolRequests.list(function (err, pool_requests) { PoolRequests.list(function (err, pool_requests) {
if (err) throw err if (err) {
logger.error('Cannot get the list of pool requests.', { err: err })
return // Abort
}
if (pool_requests.length === 0) return if (pool_requests.length === 0) return
@ -114,7 +117,8 @@
requests_to_make.remove.requests.push(pool_request.request) requests_to_make.remove.requests.push(pool_request.request)
requests_to_make.remove.ids.push(pool_request._id) requests_to_make.remove.ids.push(pool_request._id)
} else { } else {
throw new Error('Unkown pool request type.') logger.error('Unkown pool request type.', { request_type: pool_request.type })
return // abort
} }
callback_each() callback_each()
@ -142,7 +146,10 @@
function removeBadPods () { function removeBadPods () {
Pods.findBadPods(function (err, pods) { Pods.findBadPods(function (err, pods) {
if (err) throw err if (err) {
logger.error('Cannot find bad pods.', { error: err })
return // abort
}
if (pods.length === 0) return if (pods.length === 0) return
@ -150,15 +157,20 @@
var ids = pluck(pods, '_id') var ids = pluck(pods, '_id')
Videos.removeAllRemotesOf(urls, function (err, r) { Videos.removeAllRemotesOf(urls, function (err, r) {
if (err) logger.error('Cannot remove videos from a pod that we removing.', { error: err }) if (err) {
var videos_removed = r.result.n logger.error('Cannot remove videos from a pod that we removing.', { error: err })
logger.info('Removed %d videos.', videos_removed) } else {
var videos_removed = r.result.n
logger.info('Removed %d videos.', videos_removed)
}
Pods.removeAllByIds(ids, function (err, r) { Pods.removeAllByIds(ids, function (err, r) {
if (err) logger.error('Cannot remove bad pods.', { error: err }) if (err) {
logger.error('Cannot remove bad pods.', { error: err })
var pods_removed = r.result.n } else {
logger.info('Removed %d pods.', pods_removed) var pods_removed = r.result.n
logger.info('Removed %d pods.', pods_removed)
}
}) })
}) })
}) })
@ -167,9 +179,12 @@
function updatePodsScore (good_pods, bad_pods) { function updatePodsScore (good_pods, bad_pods) {
logger.info('Updating %d good pods and %d bad pods scores.', good_pods.length, bad_pods.length) logger.info('Updating %d good pods and %d bad pods scores.', good_pods.length, bad_pods.length)
Pods.incrementScores(good_pods, constants.PODS_SCORE.BONUS) Pods.incrementScores(good_pods, constants.PODS_SCORE.BONUS, function (err) {
if (err) logger.error('Cannot increment scores of good pods.')
})
Pods.incrementScores(bad_pods, constants.PODS_SCORE.MALUS, function (err) { Pods.incrementScores(bad_pods, constants.PODS_SCORE.MALUS, function (err) {
if (err) throw err if (err) logger.error('Cannot increment scores of bad pods.')
removeBadPods() removeBadPods()
}) })
} }

View File

@ -28,14 +28,14 @@
function seedAllExisting (callback) { function seedAllExisting (callback) {
Videos.listOwned(function (err, videos_list) { Videos.listOwned(function (err, videos_list) {
if (err) { if (err) {
logger.error('Cannot get list of the videos to seed.', { error: err }) logger.error('Cannot get list of the videos to seed.')
return callback(err) return callback(err)
} }
async.each(videos_list, function (video, each_callback) { async.each(videos_list, function (video, each_callback) {
seed(uploadDir + video.namePath, function (err) { seed(uploadDir + video.namePath, function (err) {
if (err) { if (err) {
logger.error('Cannot seed this video.', { error: err }) logger.error('Cannot seed this video.')
return callback(err) return callback(err)
} }

View File

@ -62,7 +62,7 @@
try { try {
wt.remove(magnetUri, callback) wt.remove(magnetUri, callback)
} catch (err) { } catch (err) {
console.log('Cannot remove the torrent from WebTorrent') console.log('Cannot remove the torrent from WebTorrent.')
return callback(null) return callback(null)
} }

View File

@ -12,7 +12,10 @@
function makeFriends (req, res, next) { function makeFriends (req, res, next) {
friends.hasFriends(function (err, has_friends) { friends.hasFriends(function (err, has_friends) {
if (err) return next(err) if (err) {
logger.error('Cannot know if we have friends.', { error: err })
res.sendStatus(500)
}
if (has_friends === true) { if (has_friends === true) {
// We need to quit our friends before make new ones // We need to quit our friends before make new ones

View File

@ -61,7 +61,7 @@
function list (callback) { function list (callback) {
PodsDB.find(function (err, pods_list) { PodsDB.find(function (err, pods_list) {
if (err) { 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) return callback(err)
} }

View File

@ -26,21 +26,28 @@
logger.debug('Add request to the pool requests.', { id: id, type: type, request: request }) logger.debug('Add request to the pool requests.', { id: id, type: type, request: request })
PoolRequestsDB.findOne({ id: id }, function (err, entity) { 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) {
if (entity.type === type) { if (entity.type === type) {
logger.error(new Error('Cannot insert two same requests.')) logger.error('Cannot insert two same requests.')
return return // Abort
} }
// Remove the request of the other type // Remove the request of the other type
PoolRequestsDB.remove({ id: id }, function (err) { 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 { } else {
PoolRequestsDB.create({ id: id, type: type, request: request }, function (err) { 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) { PoolRequestsDB.remove({ _id: { $in: ids } }, function (err) {
if (err) { if (err) {
logger.error('Cannot remove requests from the pool requests database.', { error: err }) logger.error('Cannot remove requests from the pool requests database.', { error: err })
return return // Abort
} }
logger.info('Pool requests flushed.') logger.info('Pool requests flushed.')

View File

@ -50,7 +50,7 @@
VideosDB.create(params, function (err, video) { VideosDB.create(params, function (err, video) {
if (err) { if (err) {
logger.error('Cannot insert this video into database.', { error: err }) logger.error('Cannot insert this video into database.')
return callback(err) return callback(err)
} }
@ -82,7 +82,7 @@
}, function () { }, function () {
VideosDB.create(to_add, function (err, videos) { VideosDB.create(to_add, function (err, videos) {
if (err) { if (err) {
logger.error('Cannot insert this remote video.', { error: err }) logger.error('Cannot insert this remote video.')
return callback(err) return callback(err)
} }
@ -94,7 +94,7 @@
function get (id, callback) { function get (id, callback) {
VideosDB.findById(id, function (err, video) { VideosDB.findById(id, function (err, video) {
if (err) { if (err) {
logger.error('Cannot get this video.', { error: err }) logger.error('Cannot get this video.')
return callback(err) return callback(err)
} }
@ -120,14 +120,14 @@
VideosDB.findById(id, function (err, video) { VideosDB.findById(id, function (err, video) {
if (err || !video) { if (err || !video) {
if (!err) err = new Error('Cannot find this 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) return callback(err)
} }
if (video.namePath === null) { if (video.namePath === null) {
var error_string = 'Cannot remove the video of another pod.' var error_string = 'Cannot remove the video of another pod.'
logger.error(error_string) logger.error(error_string)
return callback(null, false, video) return callback(new Error(error_string), false, video)
} }
callback(null, true, video) callback(null, true, video)
@ -137,7 +137,7 @@
function list (callback) { function list (callback) {
VideosDB.find(function (err, videos_list) { VideosDB.find(function (err, videos_list) {
if (err) { if (err) {
logger.error('Cannot get list of the videos.', { error: err }) logger.error('Cannot get the list of the videos.')
return callback(err) return callback(err)
} }
@ -149,7 +149,7 @@
// If namePath is not null this is *our* video // If namePath is not null this is *our* video
VideosDB.find({ namePath: { $ne: null } }, function (err, videos_list) { VideosDB.find({ namePath: { $ne: null } }, function (err, videos_list) {
if (err) { if (err) {
logger.error('Cannot get list of the videos.', { error: err }) logger.error('Cannot get the list of owned videos.')
return callback(err) return callback(err)
} }
@ -160,13 +160,13 @@
function removeOwned (id, callback) { function removeOwned (id, callback) {
VideosDB.findByIdAndRemove(id, function (err, video) { VideosDB.findByIdAndRemove(id, function (err, video) {
if (err) { if (err) {
logger.error('Cannot remove the torrent.', { error: err }) logger.error('Cannot remove the torrent.')
return callback(err) return callback(err)
} }
fs.unlink(uploadDir + video.namePath, function (err) { fs.unlink(uploadDir + video.namePath, function (err) {
if (err) { if (err) {
logger.error('Cannot remove this video file.', { error: err }) logger.error('Cannot remove this video file.')
return callback(err) return callback(err)
} }
@ -222,7 +222,7 @@
function search (name, callback) { function search (name, callback) {
VideosDB.find({ name: new RegExp(name) }, function (err, videos) { VideosDB.find({ name: new RegExp(name) }, function (err, videos) {
if (err) { if (err) {
logger.error('Cannot search the videos.', { error: err }) logger.error('Cannot search the videos.')
return callback(err) return callback(err)
} }