Added include and exclude source arguments. Improved stream fetch failure handling and improved logging details.
This commit is contained in:
+13
-6
@@ -1,22 +1,29 @@
|
||||
'use strict';
|
||||
|
||||
const config = require('config');
|
||||
const fetch = require('node-fetch');
|
||||
|
||||
function fetchItem(url, attempt) {
|
||||
function fetchItem(url, attempt, post) {
|
||||
function retry(error) {
|
||||
console.log(error);
|
||||
console.log('\x1b[31m%s\x1b[0m', `Failed to fetch '${url}': ${error.message} (${post.permalink})`);
|
||||
|
||||
if(attempt < 3) {
|
||||
if(attempt < config.fetch.retries) {
|
||||
console.log('Retrying...');
|
||||
|
||||
return fetchItem(url, ++attempt);
|
||||
return fetchItem(url, ++attempt, post);
|
||||
}
|
||||
|
||||
return null;
|
||||
};
|
||||
|
||||
return fetch(url).then(res => {
|
||||
return res.ok ? res : Promise.reject(`Failed to fetch ${url}`);
|
||||
if(!res.ok) {
|
||||
throw new Error(`Response not OK for '${url}', HTTP code '${res.status}'`);
|
||||
}
|
||||
|
||||
return res;
|
||||
}).then(res => {
|
||||
console.log(`Fetched '${url}'`);
|
||||
console.log(`Fetched '${url}' (${post.permalink})`);
|
||||
|
||||
return res.body;
|
||||
}).catch(retry);
|
||||
|
||||
Reference in New Issue
Block a user