0

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?

2 Answers2

1

@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!

  • Glad you found a solution! It's weird that the async/await verison works whereas normal promises doesn't but I have found Gulp sometimes has some dodgy behaviour when tracking whether async tasks have finished. See my recent question and answer https://stackoverflow.com/questions/69016358/gulp-4-tasks-wont-finish-when-using-rollup-stream-and-es6-target-in-tsconfig-j – matt123miller Sep 13 '21 at 14:37
0

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.

matt123miller
  • 181
  • 10