Reputation: 13
As part of my Gulp build process, I'm fetching an external RSS feed, converting it into JSON, and writing it into a file. Sometimes the RSS feed is unavailable, which causes the build process to return an error:
Error: socket hang up at connResetException (internal/errors.js:607:14)
at TLSSocket.socketOnEnd (_http_client.js:493:23)
at TLSSocket.emit (events.js:327:22)
at TLSSocket.EventEmitter.emit (domain.js:529:15)
at endReadableNT (internal/streams/readable.js:1327:12)
at processTicksAndRejections (internal/process/task_queues.js:80:21)
The task is run as part of a gulp.parallel
operation. It isn't essential to the rest of the process, and I'd like the build to continue even if this one task fails.
I've tried to implement error handling as follows:
// RSS to JSON
const { parse } = require('rss-to-json');
const fs = require('fs');
function parseRss(cb) {
try {
return parse('https://feeds.buzzsprout.com/1794525.rss')
.then(rss => {
fs.writeFileSync('./site/_data/podcast.json', JSON.stringify(rss, null, 3));
});
} catch(e) {
console.log(e);
}
}
But it appears to do nothing at all. How would I go about modifying the task above to make sure an error won't stop the entire operation?
Upvotes: 0
Views: 247
Reputation: 13
@matt123miller's answer was helpful, but didn't get me all the way there. It did however get me on the right track. After trying and failing to get the expect behavior using his solution, I tried making the function asynchronous and then adding the try / catch logic back in:
async function parseRss(cb) {
try {
let rss = await parse('https://feeds.buzzsprout.com/1794525.rssfg');
fs.writeFileSync('./site/_data/podcast.json', JSON.stringify(rss, null, 3));
} catch (err) {
console.log("The RSS feed couldn't be loaded or parsed");
return Promise.resolve();
}
}
That did the trick!
Upvotes: 1
Reputation: 191
Because gulp tasks need to return a Promise your attempt to catch the error will indeed surpress the error but your tasks won't continue. It should be as simple as returning a Promise from your catch block.
try {
// happy path
}
catch(e) {
console.log(e);
return Promise.resolve();
}
Your tasks should then continue.
Upvotes: 0