Browse Source

Support subError inspection in origin comparison

master
Sven Slootweg 10 months ago
parent
commit
1d5d7be21d
1 changed files with 21 additions and 9 deletions
  1. +21
    -9
      src/are-errors-from-same-origin.js

+ 21
- 9
src/are-errors-from-same-origin.js View File

@ -3,14 +3,26 @@
const areArraysEqual = require("./are-arrays-equal");
module.exports = function areErrorsFromSameOrigin(errors) {
let seenPath = null;
let referencePath = null;
return errors.every((error) => {
if (seenPath == null) {
seenPath = error.path;
return true;
} else {
return areArraysEqual(seenPath, error.path);
}
});
function checkErrors(errors, basePath = []) {
return errors.every((error) => {
let absolutePath = basePath.concat(error.path);
if (referencePath == null) {
// Whatever we encounter first is the reference path. However, we don't return `true` here; even if we assume the error's own path to be correct (since it's the reference path...), the error may still have subErrors with a different path!
referencePath = absolutePath;
}
let subErrorsEqual = (error.subErrors != null)
? checkErrors(error.subErrors, absolutePath)
: true;
let selfEqual = areArraysEqual(referencePath, absolutePath);
return (selfEqual && subErrorsEqual);
});
}
return checkErrors(errors);
};

Loading…
Cancel
Save