Skip to content

Commit

Permalink
Merge pull request #4538 from webpack/feature/error-emit-warning
Browse files Browse the repository at this point in the history
add stack to non-Error error emitted
  • Loading branch information
sokra committed Mar 22, 2017
2 parents ec46a0d + 751fd9b commit d1cf8b4
Show file tree
Hide file tree
Showing 7 changed files with 73 additions and 11 deletions.
31 changes: 31 additions & 0 deletions lib/ErrorHelpers.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/*
MIT License http://www.opensource.org/licenses/mit-license.php
Author Tobias Koppers @sokra
*/
"use strict";

const loaderFlag = "LOADER_EXECUTION";

exports.cutOffLoaderExecution = (stack) => {
stack = stack.split("\n");
for(let i = 0; i < stack.length; i++)
if(stack[i].indexOf(loaderFlag) >= 0)
stack.length = i;
return stack.join("\n");
};

exports.cutOffMessage = (stack, message) => {
const nextLine = stack.indexOf("\n");
if(nextLine === -1) {
return stack === message ? "" : stack;
} else {
const firstLine = stack.substr(0, nextLine);
return firstLine === message ? stack.substr(nextLine + 1) : stack;
}
};

exports.cleanUp = (stack, message) => {
stack = exports.cutOffLoaderExecution(stack);
stack = exports.cutOffMessage(stack, message);
return stack;
};
8 changes: 2 additions & 6 deletions lib/ModuleBuildError.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
*/
"use strict";

const loaderFlag = "LOADER_EXECUTION";
const cutOffLoaderExecution = require("./ErrorHelpers").cutOffLoaderExecution;

class ModuleBuildError extends Error {

Expand All @@ -14,11 +14,7 @@ class ModuleBuildError extends Error {
this.message = "Module build failed: ";
if(err !== null && typeof err === "object") {
if(typeof err.stack === "string" && err.stack) {
var stack = err.stack.split("\n");
for(var i = 0; i < stack.length; i++)
if(stack[i].indexOf(loaderFlag) >= 0)
stack.length = i;
stack = stack.join("\n");
var stack = cutOffLoaderExecution(err.stack);
if(!err.hideStack) {
this.message += stack;
} else {
Expand Down
5 changes: 4 additions & 1 deletion lib/ModuleError.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,18 @@
*/
"use strict";

const cleanUp = require("./ErrorHelpers").cleanUp;

class ModuleError extends Error {

constructor(module, err) {
super();

this.name = "ModuleError";
this.module = module;
this.message = err;
this.message = err && typeof err === "object" && err.message ? err.message : err;
this.error = err;
this.details = err && typeof err === "object" && err.stack ? cleanUp(err.stack, this.message) : undefined;

Error.captureStackTrace(this, this.constructor);
}
Expand Down
6 changes: 5 additions & 1 deletion lib/ModuleWarning.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,18 @@
*/
"use strict";

const cleanUp = require("./ErrorHelpers").cleanUp;

class ModuleWarning extends Error {

constructor(module, warning) {
super();

this.name = "ModuleWarning";
this.module = module;
this.message = warning;
this.message = warning && typeof warning === "object" && warning.message ? warning.message : warning;
this.warning = warning;
this.details = warning && typeof warning === "object" && warning.stack ? cleanUp(warning.stack, this.message) : undefined;

Error.captureStackTrace(this, this.constructor);
}
Expand Down
16 changes: 16 additions & 0 deletions lib/NormalModule.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,18 @@ function contextify(context, request) {
}).join("!");
}

class NonErrorEmittedError extends Error {

constructor(error) {
super();

this.name = "NonErrorEmittedError";
this.message = "(Emitted value instead of an instance of Error) " + error;

Error.captureStackTrace(this, this.constructor);
}
}

class NormalModule extends Module {

constructor(request, userRequest, rawRequest, loaders, resource, parser) {
Expand Down Expand Up @@ -98,9 +110,13 @@ class NormalModule extends Module {
const loaderContext = {
version: 2,
emitWarning: (warning) => {
if(!(warning instanceof Error))
warning = new NonErrorEmittedError(warning);
this.warnings.push(new ModuleWarning(this, warning));
},
emitError: (error) => {
if(!(error instanceof Error))
error = new NonErrorEmittedError(error);
this.errors.push(new ModuleError(this, error));
},
exec: (code, filename) => {
Expand Down
12 changes: 10 additions & 2 deletions test/cases/errors/loader-error-warning/errors.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,12 @@
module.exports = [
[/abc/],
[/def/]
[
/abc/,
/Emitted value instead of an instance of Error/,
/error-loader\.js/
],
[
/def/,
/Emitted value instead of an instance of Error/,
/error-loader\.js/
]
];
6 changes: 5 additions & 1 deletion test/cases/errors/loader-error-warning/warnings.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
module.exports = [
[/xyz/]
[
/xyz/,
/Emitted value instead of an instance of Error/,
/warning-loader\.js/
]
];

0 comments on commit d1cf8b4

Please sign in to comment.