Skip to content

Commit

Permalink
add messages for command errors
Browse files Browse the repository at this point in the history
  • Loading branch information
jeffrifwald committed Jul 7, 2016
1 parent f018e9c commit 43c54f6
Show file tree
Hide file tree
Showing 6 changed files with 123 additions and 25 deletions.
6 changes: 3 additions & 3 deletions src/cli.js
Expand Up @@ -17,11 +17,11 @@ const main = () => {
}

if (cmd === "postversion") {
postversion.run().catch(errorHandler.onError);
postversion.run().catch(errorHandler.postversionError);
} else if (cmd === "postpublish") {
postpublish.run().catch(errorHandler.onError);
postpublish.run().catch(errorHandler.postpublishError);
} else if (cmd === "dry-run") {
dryRunner.run().catch(errorHandler.onError);
dryRunner.run().catch(errorHandler.dryRunnerError);
} else {
args.showHelp();
}
Expand Down
35 changes: 33 additions & 2 deletions src/error-handler.js
Expand Up @@ -2,8 +2,39 @@ import logger from "./logger";


const errorHandler = {
onError(err) {
logger.error(err.stack || err.toString());
defaultError(err) {
logger.enable();
logger.error(`${err.stack || err.toString()}\n`);
},

dryRunnerError(err) {
errorHandler.defaultError(err);

logger.info([
"Something unexpected happened during 'dry-run'.",
"Make sure to address the error above before attemping 'dry-run' again.\n"
].join("\n"));
},

postpublishError(err) {
errorHandler.postScriptError(err, "postpublish");
},

postversionError(err) {
errorHandler.postScriptError(err, "postversion");
},

postScriptError(err, script) {
errorHandler.defaultError(err);

logger.info([
`Something unexpected happened during '${script}'.`,
"Check the error above for more information.",
"Run 'git status' to see if anything looks out of place.",
"Check 'package.json' and files defined in the publishr config.",
"Each file may need to be manually checked out or deleted.",
`Make sure 'dry-run' passes before attempting '${script}' again.\n`
].join("\n"));
}
};

Expand Down
1 change: 1 addition & 0 deletions src/file-handler.js
@@ -1,3 +1,4 @@
import {Promise} from "es6-promise";
import fileUtils from "./file-utils";
import git from "./git";
import packageUtils from "./package-utils";
Expand Down
6 changes: 4 additions & 2 deletions src/logger.js
Expand Up @@ -14,7 +14,9 @@ const logger = {
},

error(message) {
console.error(chalk.red(message)); // eslint-disable-line no-console
if (logger.enabled) {
console.error(chalk.red(message)); // eslint-disable-line no-console
}
},

fail(message, err) {
Expand All @@ -26,7 +28,7 @@ const logger = {
},

info(message) {
logger.log(chalk.white(message));
logger.log(message);
},

log(...args) {
Expand Down
79 changes: 65 additions & 14 deletions test/spec/error-handler.spec.js
Expand Up @@ -8,31 +8,82 @@ describe("errorHandler", () => {

beforeEach(() => {
sandbox = sinon.sandbox.create();
sandbox.stub(logger, "enable");
sandbox.stub(logger, "error");
sandbox.stub(logger, "info");
});

afterEach(() => {
sandbox.restore();
});

it("should handle an error with a stack", () => {
const err = {stack: "mock stack"};
describe("defaultError", () => {
it("should handle an error with a stack", () => {
const err = {stack: "mock stack"};

errorHandler.onError(err);
expect(logger.error)
.to.have.callCount(1).and
.to.have.been.calledWith("mock stack");
errorHandler.defaultError(err);
expect(logger.error)
.to.have.callCount(1).and
.to.have.been.calledWith("mock stack\n");
});

it("should handle an error without a stack", () => {
const err = {toString: () => {}};

sandbox.stub(err, "toString", () => "mock error");

errorHandler.defaultError(err);
expect(logger.enable).to.have.callCount(1);
expect(logger.error)
.to.have.callCount(1).and
.to.have.been.calledWith("mock error\n");
expect(err.toString).to.have.callCount(1);
});
});

describe("dryRunnerError", () => {
it("should handle a dry-run error", () => {
sandbox.stub(errorHandler, "defaultError");

errorHandler.dryRunnerError("mock error");
expect(errorHandler.defaultError)
.to.have.callCount(1).and
.to.have.been.calledWith("mock error");
expect(logger.info).to.have.callCount(1);
});
});

it("should handle an error without a stack", () => {
const err = {toString: () => {}};
describe("postpublishError", () => {
it("should handle a postpublish error", () => {
sandbox.stub(errorHandler, "postScriptError");

errorHandler.postpublishError("mock error");
expect(errorHandler.postScriptError)
.to.have.callCount(1).and
.to.have.been.calledWith("mock error", "postpublish");
});
});

describe("postversionError", () => {
it("should handle a postversion error", () => {
sandbox.stub(errorHandler, "postScriptError");

errorHandler.postversionError("mock error");
expect(errorHandler.postScriptError)
.to.have.callCount(1).and
.to.have.been.calledWith("mock error", "postversion");
});
});

sandbox.stub(err, "toString", () => "mock error");
describe("postScriptError", () => {
it("should handle a post script error", () => {
sandbox.stub(errorHandler, "defaultError");

errorHandler.onError(err);
expect(logger.error)
.to.have.callCount(1).and
.to.have.been.calledWith("mock error");
expect(err.toString).to.have.callCount(1);
errorHandler.postScriptError("mock error");
expect(errorHandler.defaultError)
.to.have.callCount(1).and
.to.have.been.calledWith("mock error");
expect(logger.info).to.have.callCount(1);
});
});
});
21 changes: 17 additions & 4 deletions test/spec/logger.spec.js
Expand Up @@ -49,7 +49,7 @@ describe("logger", () => {
logger.info("mock message");
expect(logger.log)
.to.have.callCount(1).and
.to.have.been.calledWith(chalk.white("mock message"));
.to.have.been.calledWith("mock message");
});

it("should log when enabled", () => {
Expand All @@ -73,11 +73,24 @@ describe("logger", () => {
logger.disable();
});

it("should always call log on error", () => {
it("should error when enabled", () => {
sandbox.stub(console, "error");

logger.enable();
logger.error("mock message");
expect(console.error) // eslint-disable-line no-console
.to.have.callCount(1).and
.to.have.been.calledWith(chalk.red("mock message"));

logger.disable();
});

it("should not error when disabled", () => {
sandbox.stub(console, "error");

logger.error("mock message");
expect(console.error).to.have.callCount(0); // eslint-disable-line no-console

logger.disable();
logger.error("mock error");
expect(console.error).to.have.callCount(1); // eslint-disable-line no-console
});
});

0 comments on commit 43c54f6

Please sign in to comment.