Skip to content

Commit

Permalink
Improvements to #562. One error at a time, better output, tests aroun…
Browse files Browse the repository at this point in the history
…d EleventyErrorHandler.
  • Loading branch information
zachleat committed Jun 8, 2019
1 parent 23c6493 commit dd64f23
Show file tree
Hide file tree
Showing 3 changed files with 96 additions and 9 deletions.
37 changes: 32 additions & 5 deletions src/EleventyErrorHandler.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,21 @@ const chalk = require("chalk");
const debug = require("debug")("Eleventy:EleventyErrorHandler");

class EleventyErrorHandler {
static get isChalkEnabled() {
if (this._isChalkEnabled !== undefined) {
return this._isChalkEnabled;
}
return true;
}

static set isChalkEnabled(enabled) {
this._isChalkEnabled = !!enabled;
}

static warn(e, msg) {
EleventyErrorHandler.initialMessage(msg, "warn", "yellow");
if (msg) {
EleventyErrorHandler.initialMessage(msg, "warn", "yellow");
}
EleventyErrorHandler.log(e, "warn");
}

Expand All @@ -13,7 +26,9 @@ class EleventyErrorHandler {
}

static error(e, msg) {
EleventyErrorHandler.initialMessage(msg, "error", "red");
if (msg) {
EleventyErrorHandler.initialMessage(msg, "error", "red");
}
EleventyErrorHandler.log(e, "error");
}

Expand All @@ -23,16 +38,28 @@ class EleventyErrorHandler {
let nextRef = ref.originalError;
EleventyErrorHandler.message(
(process.env.DEBUG ? "" : `${prefix} `) +
`${ref.message} (${ref.name})${!nextRef ? ":" : ""}`,
`${ref.message.trim()}
\`${ref.name}\` was thrown${!nextRef && ref.stack ? ":" : ""}`,
type
);

if (process.env.DEBUG) {
debug(`(${type} stack): ${ref.stack}`);
} else if (!nextRef) {
// last error in the loop
let prefix = " ";
// remove duplicate error messages if the stack contains the original message output above
//
let stackStr = ref.stack || "";
if (e.removeDuplicateErrorStringFromOutput) {
stackStr = stackStr.replace(
`${ref.name}: ${ref.message}`,
"(Repeated output has been truncated…)"
);
}
EleventyErrorHandler.message(
prefix + (ref.stack || "").split("\n").join("\n" + prefix)
prefix + stackStr.split("\n").join("\n" + prefix)
);
}
ref = nextRef;
Expand All @@ -54,7 +81,7 @@ class EleventyErrorHandler {
debug(message);
} else {
let logger = EleventyErrorHandler.logger || console;
if (chalkColor) {
if (chalkColor && EleventyErrorHandler.isChalkEnabled) {
logger[type](chalk[chalkColor](message));
} else {
logger[type](message);
Expand Down
12 changes: 8 additions & 4 deletions src/TemplateMap.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
const chalk = require("chalk");
const isPlainObject = require("lodash/isPlainObject");
const DependencyGraph = require("dependency-graph").DepGraph;
const TemplateCollection = require("./TemplateCollection");
Expand All @@ -9,7 +8,11 @@ const debug = require("debug")("Eleventy:TemplateMap");
const debugDev = require("debug")("Dev:Eleventy:TemplateMap");

const EleventyBaseError = require("./EleventyBaseError");
class DuplicatePermalinkOutputError extends EleventyBaseError {}
class DuplicatePermalinkOutputError extends EleventyBaseError {
get removeDuplicateErrorStringFromOutput() {
return true;
}
}

class TemplateMap {
constructor() {
Expand Down Expand Up @@ -503,7 +506,7 @@ class TemplateMap {
} else {
warnings[
page.outputPath
] = `Output conflict: multiple files are writing to \`${
] = `Output conflict: multiple input files are writing to \`${
page.outputPath
}\`. Use distinct \`permalink\` values to resolve this conflict.
1. ${entry.inputPath}
Expand All @@ -521,7 +524,8 @@ ${permalinks[page.url]

let warningList = Object.values(warnings);
if (warningList.length) {
throw new DuplicatePermalinkOutputError(warningList.join("\n"));
// throw one at a time
throw new DuplicatePermalinkOutputError(warningList[0]);
}
}

Expand Down
56 changes: 56 additions & 0 deletions test/EleventyErrorHandlerTest.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
import test from "ava";
import EleventyErrorHandler from "../src/EleventyErrorHandler";

let output = [];

test.beforeEach(t => {
output = [];

EleventyErrorHandler.isChalkEnabled = false;
EleventyErrorHandler.logger = {
log: function(str) {
output.push(str);
},
warn: function(str) {
output.push(str);
},
error: function(str) {
output.push(str);
}
};
});

test.afterEach(t => {
EleventyErrorHandler.isChalkEnabled = true;
EleventyErrorHandler.logger = null;
});

test("Disable chalk", t => {
EleventyErrorHandler.isChalkEnabled = false;
t.is(EleventyErrorHandler.isChalkEnabled, false);
});

test("Log a warning, error", t => {
EleventyErrorHandler.warn(new Error("Test warning"), "Hello");

let expected = `Hello: (more in DEBUG output)
> Test warning
\`Error\` was thrown:
Error: Test warning`;
t.is(output.join("\n").substr(0, expected.length), expected);

// normally this would be multiple tests but we do this garbage because
// tests run async and it doesn’t work with the static methods in
// EleventyErrorHandler
output = [];

EleventyErrorHandler.error(new Error("Test error"), "It’s me");

expected = `It’s me: (more in DEBUG output)
> Test error
\`Error\` was thrown:
Error: Test error`;
t.is(output.join("\n").substr(0, expected.length), expected);
});

0 comments on commit dd64f23

Please sign in to comment.