diff options
author | Chocobozzz <florian.bigard@gmail.com> | 2016-02-05 19:02:05 +0100 |
---|---|---|
committer | Chocobozzz <florian.bigard@gmail.com> | 2016-02-05 19:02:05 +0100 |
commit | 8425cb894d4867d26fd5f7fae7862b0669f3c717 (patch) | |
tree | 5e2f00a8219e198f2dd074ca13f3ae681174228f /lib | |
parent | dac0a5319ab1c52a0958647b1593f85339b77e29 (diff) | |
download | PeerTube-8425cb894d4867d26fd5f7fae7862b0669f3c717.tar.gz PeerTube-8425cb894d4867d26fd5f7fae7862b0669f3c717.tar.zst PeerTube-8425cb894d4867d26fd5f7fae7862b0669f3c717.zip |
Error handling mini refractoring
Diffstat (limited to 'lib')
-rw-r--r-- | lib/friends.js | 24 | ||||
-rw-r--r-- | lib/poolRequests.js | 43 | ||||
-rw-r--r-- | lib/videos.js | 4 | ||||
-rw-r--r-- | lib/webtorrent.js | 2 |
4 files changed, 47 insertions, 26 deletions
diff --git a/lib/friends.js b/lib/friends.js index b0086a38b..badf09c7d 100644 --- a/lib/friends.js +++ b/lib/friends.js | |||
@@ -30,8 +30,7 @@ | |||
30 | function addVideoToFriends (video) { | 30 | function addVideoToFriends (video) { |
31 | // To avoid duplicates | 31 | // To avoid duplicates |
32 | var id = video.name + video.magnetUri | 32 | var id = video.name + video.magnetUri |
33 | // namePath is null | 33 | // ensure namePath is null |
34 | // TODO | ||
35 | video.namePath = null | 34 | video.namePath = null |
36 | PoolRequests.addRequest(id, 'add', video) | 35 | PoolRequests.addRequest(id, 'add', video) |
37 | } | 36 | } |
@@ -51,13 +50,15 @@ | |||
51 | logger.info('Make friends!') | 50 | logger.info('Make friends!') |
52 | fs.readFile(peertubeCrypto.getCertDir() + 'peertube.pub', 'utf8', function (err, cert) { | 51 | fs.readFile(peertubeCrypto.getCertDir() + 'peertube.pub', 'utf8', function (err, cert) { |
53 | if (err) { | 52 | if (err) { |
54 | logger.error('Cannot read public cert.', { error: err }) | 53 | logger.error('Cannot read public cert.') |
55 | return callback(err) | 54 | return callback(err) |
56 | } | 55 | } |
57 | 56 | ||
58 | var urls = config.get('network.friends') | 57 | var urls = config.get('network.friends') |
59 | 58 | ||
60 | async.each(urls, computeForeignPodsList, function () { | 59 | async.each(urls, computeForeignPodsList, function (err) { |
60 | if (err) return callback(err) | ||
61 | |||
61 | logger.debug('Pods scores computed.', { pods_score: pods_score }) | 62 | logger.debug('Pods scores computed.', { pods_score: pods_score }) |
62 | var pods_list = computeWinningPods(urls, pods_score) | 63 | var pods_list = computeWinningPods(urls, pods_score) |
63 | logger.debug('Pods that we keep computed.', { pods_to_keep: pods_list }) | 64 | logger.debug('Pods that we keep computed.', { pods_to_keep: pods_list }) |
@@ -72,7 +73,8 @@ | |||
72 | // Let's give 1 point to the pod we ask the friends list | 73 | // Let's give 1 point to the pod we ask the friends list |
73 | pods_score[url] = 1 | 74 | pods_score[url] = 1 |
74 | 75 | ||
75 | getForeignPodsList(url, function (foreign_pods_list) { | 76 | getForeignPodsList(url, function (err, foreign_pods_list) { |
77 | if (err) return callback(err) | ||
76 | if (foreign_pods_list.length === 0) return callback() | 78 | if (foreign_pods_list.length === 0) return callback() |
77 | 79 | ||
78 | async.each(foreign_pods_list, function (foreign_pod, callback_each) { | 80 | async.each(foreign_pods_list, function (foreign_pod, callback_each) { |
@@ -108,7 +110,10 @@ | |||
108 | 110 | ||
109 | // Get the list of our videos to send to our new friends | 111 | // Get the list of our videos to send to our new friends |
110 | Videos.listOwned(function (err, videos_list) { | 112 | Videos.listOwned(function (err, videos_list) { |
111 | if (err) throw err | 113 | if (err) { |
114 | logger.error('Cannot get the list of videos we own.') | ||
115 | return callback(err) | ||
116 | } | ||
112 | 117 | ||
113 | var data = { | 118 | var data = { |
114 | url: http + '://' + host + ':' + port, | 119 | url: http + '://' + host + ':' + port, |
@@ -145,7 +150,7 @@ | |||
145 | poolRequests.activate() | 150 | poolRequests.activate() |
146 | 151 | ||
147 | if (err) { | 152 | if (err) { |
148 | logger.error('There was some errors when we wanted to make friends.', { error: err }) | 153 | logger.error('There was some errors when we wanted to make friends.') |
149 | return callback(err) | 154 | return callback(err) |
150 | } | 155 | } |
151 | 156 | ||
@@ -212,8 +217,9 @@ | |||
212 | var path = '/api/' + constants.API_VERSION + '/pods' | 217 | var path = '/api/' + constants.API_VERSION + '/pods' |
213 | 218 | ||
214 | request.get(url + path, function (err, response, body) { | 219 | request.get(url + path, function (err, response, body) { |
215 | if (err) throw err | 220 | if (err) return callback(err) |
216 | callback(JSON.parse(body)) | 221 | |
222 | callback(null, JSON.parse(body)) | ||
217 | }) | 223 | }) |
218 | } | 224 | } |
219 | })() | 225 | })() |
diff --git a/lib/poolRequests.js b/lib/poolRequests.js index f4ab434ad..ccc3489ab 100644 --- a/lib/poolRequests.js +++ b/lib/poolRequests.js | |||
@@ -44,7 +44,7 @@ | |||
44 | if (!callback) callback = function () {} | 44 | if (!callback) callback = function () {} |
45 | 45 | ||
46 | Pods.list(function (err, pods) { | 46 | Pods.list(function (err, pods) { |
47 | if (err) throw err | 47 | if (err) return callback(err) |
48 | 48 | ||
49 | var params = { | 49 | var params = { |
50 | encrypt: true, | 50 | encrypt: true, |
@@ -59,7 +59,7 @@ | |||
59 | } else if (type === 'remove') { | 59 | } else if (type === 'remove') { |
60 | params.path = '/api/' + constants.API_VERSION + '/remotevideos/remove' | 60 | params.path = '/api/' + constants.API_VERSION + '/remotevideos/remove' |
61 | } else { | 61 | } else { |
62 | throw new Error('Unkown pool request type.') | 62 | return callback(new Error('Unkown pool request type.')) |
63 | } | 63 | } |
64 | 64 | ||
65 | var bad_pods = [] | 65 | var bad_pods = [] |
@@ -91,7 +91,10 @@ | |||
91 | logger.info('Making pool requests to friends.') | 91 | logger.info('Making pool requests to friends.') |
92 | 92 | ||
93 | PoolRequests.list(function (err, pool_requests) { | 93 | PoolRequests.list(function (err, pool_requests) { |
94 | if (err) throw err | 94 | if (err) { |
95 | logger.error('Cannot get the list of pool requests.', { err: err }) | ||
96 | return // Abort | ||
97 | } | ||
95 | 98 | ||
96 | if (pool_requests.length === 0) return | 99 | if (pool_requests.length === 0) return |
97 | 100 | ||
@@ -114,7 +117,8 @@ | |||
114 | requests_to_make.remove.requests.push(pool_request.request) | 117 | requests_to_make.remove.requests.push(pool_request.request) |
115 | requests_to_make.remove.ids.push(pool_request._id) | 118 | requests_to_make.remove.ids.push(pool_request._id) |
116 | } else { | 119 | } else { |
117 | throw new Error('Unkown pool request type.') | 120 | logger.error('Unkown pool request type.', { request_type: pool_request.type }) |
121 | return // abort | ||
118 | } | 122 | } |
119 | 123 | ||
120 | callback_each() | 124 | callback_each() |
@@ -142,7 +146,10 @@ | |||
142 | 146 | ||
143 | function removeBadPods () { | 147 | function removeBadPods () { |
144 | Pods.findBadPods(function (err, pods) { | 148 | Pods.findBadPods(function (err, pods) { |
145 | if (err) throw err | 149 | if (err) { |
150 | logger.error('Cannot find bad pods.', { error: err }) | ||
151 | return // abort | ||
152 | } | ||
146 | 153 | ||
147 | if (pods.length === 0) return | 154 | if (pods.length === 0) return |
148 | 155 | ||
@@ -150,15 +157,20 @@ | |||
150 | var ids = pluck(pods, '_id') | 157 | var ids = pluck(pods, '_id') |
151 | 158 | ||
152 | Videos.removeAllRemotesOf(urls, function (err, r) { | 159 | Videos.removeAllRemotesOf(urls, function (err, r) { |
153 | if (err) logger.error('Cannot remove videos from a pod that we removing.', { error: err }) | 160 | if (err) { |
154 | var videos_removed = r.result.n | 161 | logger.error('Cannot remove videos from a pod that we removing.', { error: err }) |
155 | logger.info('Removed %d videos.', videos_removed) | 162 | } else { |
163 | var videos_removed = r.result.n | ||
164 | logger.info('Removed %d videos.', videos_removed) | ||
165 | } | ||
156 | 166 | ||
157 | Pods.removeAllByIds(ids, function (err, r) { | 167 | Pods.removeAllByIds(ids, function (err, r) { |
158 | if (err) logger.error('Cannot remove bad pods.', { error: err }) | 168 | if (err) { |
159 | 169 | logger.error('Cannot remove bad pods.', { error: err }) | |
160 | var pods_removed = r.result.n | 170 | } else { |
161 | logger.info('Removed %d pods.', pods_removed) | 171 | var pods_removed = r.result.n |
172 | logger.info('Removed %d pods.', pods_removed) | ||
173 | } | ||
162 | }) | 174 | }) |
163 | }) | 175 | }) |
164 | }) | 176 | }) |
@@ -167,9 +179,12 @@ | |||
167 | function updatePodsScore (good_pods, bad_pods) { | 179 | function updatePodsScore (good_pods, bad_pods) { |
168 | logger.info('Updating %d good pods and %d bad pods scores.', good_pods.length, bad_pods.length) | 180 | logger.info('Updating %d good pods and %d bad pods scores.', good_pods.length, bad_pods.length) |
169 | 181 | ||
170 | Pods.incrementScores(good_pods, constants.PODS_SCORE.BONUS) | 182 | Pods.incrementScores(good_pods, constants.PODS_SCORE.BONUS, function (err) { |
183 | if (err) logger.error('Cannot increment scores of good pods.') | ||
184 | }) | ||
185 | |||
171 | Pods.incrementScores(bad_pods, constants.PODS_SCORE.MALUS, function (err) { | 186 | Pods.incrementScores(bad_pods, constants.PODS_SCORE.MALUS, function (err) { |
172 | if (err) throw err | 187 | if (err) logger.error('Cannot increment scores of bad pods.') |
173 | removeBadPods() | 188 | removeBadPods() |
174 | }) | 189 | }) |
175 | } | 190 | } |
diff --git a/lib/videos.js b/lib/videos.js index 5d23070a7..0da7715c4 100644 --- a/lib/videos.js +++ b/lib/videos.js | |||
@@ -28,14 +28,14 @@ | |||
28 | function seedAllExisting (callback) { | 28 | function seedAllExisting (callback) { |
29 | Videos.listOwned(function (err, videos_list) { | 29 | Videos.listOwned(function (err, videos_list) { |
30 | if (err) { | 30 | if (err) { |
31 | logger.error('Cannot get list of the videos to seed.', { error: err }) | 31 | logger.error('Cannot get list of the videos to seed.') |
32 | return callback(err) | 32 | return callback(err) |
33 | } | 33 | } |
34 | 34 | ||
35 | async.each(videos_list, function (video, each_callback) { | 35 | async.each(videos_list, function (video, each_callback) { |
36 | seed(uploadDir + video.namePath, function (err) { | 36 | seed(uploadDir + video.namePath, function (err) { |
37 | if (err) { | 37 | if (err) { |
38 | logger.error('Cannot seed this video.', { error: err }) | 38 | logger.error('Cannot seed this video.') |
39 | return callback(err) | 39 | return callback(err) |
40 | } | 40 | } |
41 | 41 | ||
diff --git a/lib/webtorrent.js b/lib/webtorrent.js index d1ca3c9f2..d0db6e066 100644 --- a/lib/webtorrent.js +++ b/lib/webtorrent.js | |||
@@ -62,7 +62,7 @@ | |||
62 | try { | 62 | try { |
63 | wt.remove(magnetUri, callback) | 63 | wt.remove(magnetUri, callback) |
64 | } catch (err) { | 64 | } catch (err) { |
65 | console.log('Cannot remove the torrent from WebTorrent') | 65 | console.log('Cannot remove the torrent from WebTorrent.') |
66 | return callback(null) | 66 | return callback(null) |
67 | } | 67 | } |
68 | 68 | ||