You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
24 lines
771 B
JavaScript
24 lines
771 B
JavaScript
8 years ago
|
'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`);
|
||
|
}
|
||
|
});
|
||
|
};
|