'use strict'; const Promise = require("bluebird"); const fs = Promise.promisifyAll(require("fs")); let maxAttempts = 100; module.exports = function readError(errorFilePath, attempt = 0) { return Promise.try(() => { return fs.readFileAsync(errorFilePath); }).then((fileContents) => { return JSON.parse(fileContents); }).catch(SyntaxError, (err) => { if (attempt < maxAttempts) { /* The JSON hasn't been completely written yet. We'll retry in a bit. */ return Promise.delay(1000).then(() => { reportError(errorFilePath, attempt + 1); }); } else { throw new Error(`Could not parse error file ${errorFilePath}, and reached maximum attempts`); } }); };