Move related errors into subErrors where reliably possible

master
Sven Slootweg 4 years ago
parent f6e92a461c
commit d10cb7ae8f

@ -47,18 +47,23 @@ module.exports = function wrapError(message, rules, options = {}) {
if (!preserveOriginalErrors) { if (!preserveOriginalErrors) {
return [ new ValidationError(message) ]; return [ new ValidationError(message) ];
} else { } else {
let newRootErrors = (hasRootValidationErrors) // If possible, we try to move any subpath errors into the subErrors of an (optionally newly-created) root error. Otherwise, it can become difficult for the user to correlate together the root and subpath errors that are related, when we start changing the messages of the root errors.
? errorsByType.validationRoot.map((error) => { if (errorsByType.validationRoot.length === 0) {
// TODO: Currently we cannot set `originalError` due to a bug in `create-error`; switch to a better error implementation return [ new ValidationError(message, { subErrors: errorsByType.validationPath }) ];
// return new ValidationError(`${message} (${error.message})`, { originalError: error }) } else if (errorsByType.validationRoot.length === 1) {
return new ValidationError(`${message} (${error.message})`) let error = errorsByType.validationRoot[0];
})
: [ new ValidationError(message) ];
return concat([ // TODO: Currently we cannot set `originalError` due to a bug in `create-error`; switch to a better error implementation
newRootErrors, return [ new ValidationError(`${message} (${error.message})`, { subErrors: errorsByType.validationPath }) ];
errorsByType.validationPath } else {
]); // If there are multiple root errors, we cannot determine which root error the subpath errors belong to, so we'll just provide them as a flat list
return concat([
errorsByType.validationRoot.map((error) => {
return new ValidationError(`${message} (${error.message})`)
}),
errorsByType.validationPath
]);
}
} }
} else { } else {
return []; return [];

Loading…
Cancel
Save