Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: prevent errors to be displayed as [Error]: [object Object] #478

Merged
merged 1 commit into from
Jun 22, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions lib/view/json-view.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ module.exports = {
*/
function toString(jsonObject) {
try {
if (typeof jsonObject === 'string') {
return jsonObject;
}
// handle issue when Error object serialized to {}
if (jsonObject instanceof Error) {
jsonObject = {message: jsonObject.message, stack: jsonObject.stack, detail: jsonObject};
Expand Down
4 changes: 2 additions & 2 deletions lib/view/messenger.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
const chalk = require("chalk");
const path = require("path");
const fs = require("fs");
const jsonViewer = require("./json-view");

// A map used to define message levels
// and set their display styles
Expand Down Expand Up @@ -167,8 +168,7 @@ class Messenger {
* @param {*} data the message to display
*/
error(data) {
const msg = data.constructor.name === "Error" ? data.message : data;

const msg = data.constructor.name === "Error" ? data.message : jsonViewer.toString(data);
const operation = "ERROR";
this.buffer({
time: Messenger.getTime(),
Expand Down
25 changes: 25 additions & 0 deletions test/unit/view/messenger-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,16 @@ const chalk = require("chalk");
const fs = require("fs");
const path = require("path");
const Messenger = require("../../../lib/view/messenger");
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_TIME = "TEST_TIME";
const TEST_ERROR_OBJ = new Error("TEST_ERROR");

Expand Down Expand Up @@ -147,6 +154,24 @@ 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", () => {
const stub = sinon.stub(console, "error");
const expectedError = jsonView.toString(TEST_ERROR_WITH_STACK);
const expectedItem = {
time: TEST_TIME,
operation: "ERROR",
level: 50,
msg: expectedError,
};

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

// verify
expect(Messenger.getInstance()._buffer[0]).deep.equal(expectedItem);
expect(stub.args[0][0]).equal(chalk`{bold.red [Error]: ${expectedError}}`);
});

it("| fatal function correctly push message to buffer and display", () => {
const stub = sinon.stub(console, "error");
const expectedItem = {
Expand Down