Skip to content

Commit

Permalink
fix: display message property for all error instance types
Browse files Browse the repository at this point in the history
  • Loading branch information
jsetton committed Aug 9, 2023
1 parent cd44f72 commit a0d2f19
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 23 deletions.
2 changes: 1 addition & 1 deletion lib/controllers/skill-metadata-controller/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -463,7 +463,7 @@ module.exports = class SkillMetadataController {

if (shouldPrintWarning && importStatusResponse.warnings.length > 0) {
importStatusResponse.warnings.forEach((warning) => {
Messenger.getInstance().warn(warning);
Messenger.getInstance().warn(warning.message);
});
shouldPrintWarning = false;
}
Expand Down
6 changes: 3 additions & 3 deletions lib/view/messenger.js
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ class Messenger {
* @param {*} data the message to display
*/
warn(data) {
const msg = data.constructor.name === "Error" ? data.message : jsonViewer.toString(data);
const msg = data instanceof Error ? data.message : jsonViewer.toString(data);
const operation = "WARN";
this.buffer({
time: Messenger.getTime(),
Expand All @@ -169,7 +169,7 @@ class Messenger {
* @param {*} data the message to display
*/
error(data) {
const msg = data.constructor.name === "Error" ? data.message : jsonViewer.toString(data);
const msg = data instanceof Error ? data.message : jsonViewer.toString(data);
const operation = "ERROR";
this.buffer({
time: Messenger.getTime(),
Expand All @@ -186,7 +186,7 @@ class Messenger {
* @param {*} data the message to display
*/
fatal(data) {
const msg = data.constructor.name === "Error" ? data.stack.substring(7) : data;
const msg = data instanceof Error ? data.stack.substring(7) : data;
const operation = "FATAL";
this.buffer({
time: Messenger.getTime(),
Expand Down
33 changes: 14 additions & 19 deletions test/unit/view/messenger-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,9 @@ const jsonView = require("../../../lib/view/json-view");

describe("View test - messenger file test", () => {
const TEST_MESSAGE = "TEST_MESSAGE";
const TEST_STACK = "TEST_STACK";
const TEST_ERROR_WITH_STACK = {
message: TEST_MESSAGE,
stack: TEST_STACK,
foo: 2,
}
const TEST_OBJECT = { foo: 1, bar: 2 };
const TEST_ERROR = new Error("TEST_ERROR");
const TEST_TIME = "TEST_TIME";
const TEST_ERROR_OBJ = new Error("TEST_ERROR");

describe("# inspect correctness for constructor, getInstance and dispose", () => {
beforeEach(() => {
Expand Down Expand Up @@ -137,9 +132,9 @@ describe("View test - messenger file test", () => {
expect(stub.args[0][0]).equal(chalk`{bold.yellow [Warn]: ${TEST_MESSAGE}}`);
});

it("| warn function correctly push error objects to buffer and display", () => {
it("| warn function correctly push stringify object to buffer and display", () => {
const stub = sinon.stub(console, "warn");
const expectedMessage = jsonView.toString(TEST_ERROR_WITH_STACK);
const expectedMessage = jsonView.toString(TEST_OBJECT);
const expectedItem = {
time: TEST_TIME,
operation: "WARN",
Expand All @@ -148,7 +143,7 @@ describe("View test - messenger file test", () => {
};

// call
Messenger.getInstance().warn(TEST_ERROR_WITH_STACK);
Messenger.getInstance().warn(TEST_OBJECT);

// verify
expect(Messenger.getInstance()._buffer[0]).deep.equal(expectedItem);
Expand All @@ -157,7 +152,7 @@ describe("View test - messenger file test", () => {

it("| warn function correctly push error message to buffer and display", () => {
const stub = sinon.stub(console, "warn");
const expectedMessage = TEST_ERROR_OBJ.message;
const expectedMessage = TEST_ERROR.message;
const expectedItem = {
time: TEST_TIME,
operation: "WARN",
Expand All @@ -166,7 +161,7 @@ describe("View test - messenger file test", () => {
};

// call
Messenger.getInstance().warn(TEST_ERROR_OBJ);
Messenger.getInstance().warn(TEST_ERROR);

// verify
expect(Messenger.getInstance()._buffer[0]).deep.equal(expectedItem);
Expand All @@ -190,9 +185,9 @@ describe("View test - messenger file test", () => {
expect(stub.args[0][0]).equal(chalk`{bold.red [Error]: ${TEST_MESSAGE}}`);
});

it("| error function correctly push error objects to buffer and display", () => {
it("| error function correctly push stringify object to buffer and display", () => {
const stub = sinon.stub(console, "error");
const expectedMessage = jsonView.toString(TEST_ERROR_WITH_STACK);
const expectedMessage = jsonView.toString(TEST_OBJECT);
const expectedItem = {
time: TEST_TIME,
operation: "ERROR",
Expand All @@ -201,7 +196,7 @@ describe("View test - messenger file test", () => {
};

// call
Messenger.getInstance().error(TEST_ERROR_WITH_STACK);
Messenger.getInstance().error(TEST_OBJECT);

// verify
expect(Messenger.getInstance()._buffer[0]).deep.equal(expectedItem);
Expand All @@ -210,7 +205,7 @@ describe("View test - messenger file test", () => {

it("| error function correctly push error message to buffer and display", () => {
const stub = sinon.stub(console, "error");
const expectedMessage = TEST_ERROR_OBJ.message;
const expectedMessage = TEST_ERROR.message;
const expectedItem = {
time: TEST_TIME,
operation: "ERROR",
Expand All @@ -219,7 +214,7 @@ describe("View test - messenger file test", () => {
};

// call
Messenger.getInstance().error(TEST_ERROR_OBJ);
Messenger.getInstance().error(TEST_ERROR);

// verify
expect(Messenger.getInstance()._buffer[0]).deep.equal(expectedItem);
Expand All @@ -245,7 +240,7 @@ describe("View test - messenger file test", () => {

it("| fatal function correctly push error stack to buffer and display", () => {
const stub = sinon.stub(console, "error");
const expectedMessage = TEST_ERROR_OBJ.stack.substring(7);
const expectedMessage = TEST_ERROR.stack.substring(7);
const expectedItem = {
time: TEST_TIME,
operation: "FATAL",
Expand All @@ -254,7 +249,7 @@ describe("View test - messenger file test", () => {
};

// call
Messenger.getInstance().fatal(TEST_ERROR_OBJ);
Messenger.getInstance().fatal(TEST_ERROR);

// verify
expect(Messenger.getInstance()._buffer[0]).deep.equal(expectedItem);
Expand Down

0 comments on commit a0d2f19

Please sign in to comment.