You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

29 lines
841 B

4 years ago
  1. 'use strict';
  2. var getError = require('./get-error');
  3. /**
  4. * Where the given list is non-null and contains error instances then consolidate and throw
  5. * @throws Error
  6. * @param {string} resourcePath The path to the resource being processed
  7. * @param {null|Array} candidates A possible Array with possible error elements
  8. */
  9. function throwErrors(resourcePath, candidates) {
  10. var errors = !!candidates && candidates
  11. .filter(testIsError)
  12. .map(getMessage);
  13. var hasError = !!errors && errors.length;
  14. if (hasError) {
  15. throw getError(['For resource: ' + resourcePath].concat(errors).join('\n'));
  16. }
  17. function testIsError(candidate) {
  18. return !!candidate && (typeof candidate === 'object') && (candidate instanceof Error);
  19. }
  20. function getMessage(error) {
  21. return error.message;
  22. }
  23. }
  24. module.exports = throwErrors;