defaultError.js 850 Bytes
Newer Older
liang ce committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43
'use strict';

const concat = require('../utils').concat;
const formatTitle = require('../utils/colors').formatTitle;

function displayError(severity, error) {
  const baseError = formatTitle(severity, severity);

  return concat(
    `${baseError} ${removeLoaders(error.file)}`,
    '',
    error.message,
    (error.origin ? error.origin : undefined),
    '',
    error.infos
  );
}

function removeLoaders(file) {
  if (!file) {
    return "";
  }
  const split = file.split('!');
  const filePath = split[split.length - 1];
  return `in ${filePath}`;
}

function isDefaultError(error) {
  return !error.type;
}

/**
 * Format errors without a type
 */
function format(errors, type) {
  return errors
    .filter(isDefaultError)
    .reduce((accum, error) => (
      accum.concat(displayError(type, error))
    ), []);
}

module.exports = format;