feat: log all verification errors

This commit is contained in:
Pierre Vanduynslager
2018-01-27 13:24:39 -05:00
parent 03e117be10
commit cdb98f919f
6 changed files with 171 additions and 19 deletions
+19 -5
View File
@@ -1,19 +1,33 @@
const {identity} = require('lodash');
const pReflect = require('p-reflect');
const pReduce = require('p-reduce');
const AggregateError = require('aggregate-error');
module.exports = steps => async (input, getNextInput = identity) => {
module.exports = steps => async (input, settleAll = false, getNextInput = identity) => {
const results = [];
const errors = [];
await pReduce(
steps,
async (prevResult, nextStep) => {
// Call the next step with the input computed at the end of the previous iteration
const result = await nextStep(prevResult);
// Save intermediary result
results.push(result);
let result;
// Call the next step with the input computed at the end of the previous iteration and save intermediary result
if (settleAll) {
const {isFulfilled, value, reason} = await pReflect(nextStep(prevResult));
result = isFulfilled ? value : reason;
(isFulfilled ? results : errors).push(result);
} else {
result = await nextStep(prevResult);
results.push(result);
}
// Prepare input for next step, passing the result of the previous iteration and the current one
return getNextInput(prevResult, result);
},
input
);
if (errors.length > 0) {
throw new AggregateError(errors);
}
return results;
};