]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blobdiff - lib/friends.js
Error handling mini refractoring
[github/Chocobozzz/PeerTube.git] / lib / friends.js
index b0086a38b78e1fb2b1cc6e53139d0050afff7d05..badf09c7dd7e6d184978d2479c117315ec4c524a 100644 (file)
@@ -30,8 +30,7 @@
   function addVideoToFriends (video) {
     // To avoid duplicates
     var id = video.name + video.magnetUri
-    // namePath is null
-    // TODO
+    // ensure namePath is null
     video.namePath = null
     PoolRequests.addRequest(id, 'add', video)
   }
     logger.info('Make friends!')
     fs.readFile(peertubeCrypto.getCertDir() + 'peertube.pub', 'utf8', function (err, cert) {
       if (err) {
-        logger.error('Cannot read public cert.', { error: err })
+        logger.error('Cannot read public cert.')
         return callback(err)
       }
 
       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 })
         var pods_list = computeWinningPods(urls, pods_score)
         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
       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()
 
         async.each(foreign_pods_list, function (foreign_pod, callback_each) {
 
       // Get the list of our videos to send to our new friends
       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 = {
           url: http + '://' + host + ':' + port,
             poolRequests.activate()
 
             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)
             }
 
     var path = '/api/' + constants.API_VERSION + '/pods'
 
     request.get(url + path, function (err, response, body) {
-      if (err) throw err
-      callback(JSON.parse(body))
+      if (err) return callback(err)
+
+      callback(null, JSON.parse(body))
     })
   }
 })()