npm and error messages

This commit is contained in:
2018-10-27 03:51:47 -05:00
parent 692ab70565
commit 025a403027
29601 changed files with 2759363 additions and 14 deletions

View File

@@ -0,0 +1,65 @@
'use strict';
const ErrorStackParser = require('error-stack-parser');
const RequestShortener = require("webpack/lib/RequestShortener");
// TODO: allow the location to be customized in options
const requestShortener = new RequestShortener(process.cwd());
/*
This logic is mostly duplicated from webpack/lib/Stats.js#toJson()
See: https://github.com/webpack/webpack/blob/2f618e733aab4755deb42e9d8e859609005607c0/lib/Stats.js#L89
*/
function extractError (e) {
return {
message: e.message,
file: getFile(e),
origin: getOrigin(e),
name: e.name,
severity: 0,
webpackError: e,
originalStack: getOriginalErrorStack(e)
};
}
function getOriginalErrorStack(e) {
while (e.error != null) {
e = e.error;
}
if (e.stack) {
return ErrorStackParser.parse(e);
}
return [];
}
function getFile (e) {
if (e.file) {
return e.file;
} else if (e.module && e.module.readableIdentifier && typeof e.module.readableIdentifier === "function") {
return e.module.readableIdentifier(requestShortener);
}
}
function getOrigin (e) {
let origin = '';
if (e.dependencies && e.origin) {
origin += '\n @ ' + e.origin.readableIdentifier(requestShortener);
e.dependencies.forEach(function (dep) {
if (!dep.loc) return;
if (typeof dep.loc === "string") return;
if (!dep.loc.start) return;
if (!dep.loc.end) return;
origin += ' ' + dep.loc.start.line + ':' + dep.loc.start.column + '-' +
(dep.loc.start.line !== dep.loc.end.line ? dep.loc.end.line + ':' : '') + dep.loc.end.column;
});
var current = e.origin;
while (current.issuer && typeof current.issuer.readableIdentifier === 'function') {
current = current.issuer;
origin += '\n @ ' + current.readableIdentifier(requestShortener);
}
}
return origin;
}
module.exports = extractError;

View File

@@ -0,0 +1,18 @@
'use strict';
/**
* Applies formatters to all AnnotatedErrors.
*
* A formatter has the following signature: FormattedError => Array<String>.
* It takes a formatted error produced by a transformer and returns a list
* of log statements to print.
*
*/
function formatErrors(errors, formatters, errorType) {
const format = (formatter) => formatter(errors, errorType) || [];
const flatten = (accum, curr) => accum.concat(curr);
return formatters.map(format).reduce(flatten, [])
}
module.exports = formatErrors;

View File

@@ -0,0 +1,34 @@
'use strict';
const extractError = require('./extractWebpackError');
/**
* Applies all transformers to all errors and returns "annotated"
* errors.
*
* Each transformer should have the following signature WebpackError => AnnotatedError
*
* A WebpackError has the following fields:
* - message
* - file
* - origin
* - name
* - severity
* - webpackError (original error)
*
* An AnnotatedError should be an extension (Object.assign) of the WebpackError
* and add whatever information is convenient for formatting.
* In particular, they should have a 'priority' field.
*
* The plugin will only display errors having maximum priority at the same time.
*
* If they don't have a 'type' field, the will be handled by the default formatter.
*/
function processErrors (errors, transformers) {
const transform = (error, transformer) => transformer(error);
const applyTransformations = (error) => transformers.reduce(transform, error);
return errors.map(extractError).map(applyTransformations);
}
module.exports = processErrors;