Skip to content

Commit

Permalink
fix: Improve the runtime error printing
Browse files Browse the repository at this point in the history
Perform JSON.stringify only if the error object has no message property;
otherwise just print the message. If the message contains both error message
and stacktrace, print just the latter.
  • Loading branch information
prantlf committed May 20, 2018
1 parent 39ab120 commit 2601ea2
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 7 deletions.
15 changes: 14 additions & 1 deletion lib/util/printers.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ exports.printRuntimeErrors =
write('\n');
write(clc.red(errorObj.browser.name));
write('\n');
write(clc.red(JSON.stringify(errorObj.error, undefined, 2)));
write(clc.red(getErrorMessage(errorObj.error)));
write('\n');
});
write('\n');
Expand All @@ -39,6 +39,19 @@ exports.printRuntimeErrors =
}
};

function getErrorMessage(error) {
if (typeof error !== 'string') {
if (error.message) {
// If there are both error message and stacktrace inside the error message
error = error.message.replace(/^(?:.|\r|\n)+\r?\n\r?\n(\w*Error:)/, '$1')
} else {
// An unrecognized object
error = JSON.stringify(error, undefined, 2)
}
}
return error
}

/**
* printTestFailures - utility method
*
Expand Down
28 changes: 22 additions & 6 deletions test/printers.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,16 @@ describe('printers.js test suite', function() {
'browser': {
'name': 'browser2'
},
'error': 'error2'
'error': {
'message': 'error2'
}
},{
'browser': {
'name': 'browser3'
},
'error': {
'code': 'error3'
}
}];

out = '#,#,#,#,#,#,#,#,#,#,\n,' +
Expand All @@ -82,12 +91,17 @@ describe('printers.js test suite', function() {
'\n,' +
runtimeErrors[0].browser.name + ',' +
'\n,' +
JSON.stringify(runtimeErrors[0].error, undefined, 2) + ',' +
runtimeErrors[0].error + ',' +
'\n,' +
'\n,' +
runtimeErrors[1].browser.name + ',' +
'\n,' +
JSON.stringify(runtimeErrors[1].error, undefined, 2) + ',' +
runtimeErrors[1].error.message + ',' +
'\n,' +
'\n,' +
runtimeErrors[2].browser.name + ',' +
'\n,' +
JSON.stringify(runtimeErrors[2].error, undefined, 2) + ',' +
'\n,' +
'\n,' +
'#,\n,' +
Expand Down Expand Up @@ -137,11 +151,13 @@ describe('printers.js test suite', function() {
eq(hashCount, rainbowifyFake.callCount);
eq(total, writeFake.callCount);

eq(4, clcFake.red.callCount);
eq(6, clcFake.red.callCount);
ok(clcFake.red.getCall(0).calledWithExactly(runtimeErrors[0].browser.name));
ok(clcFake.red.getCall(1).calledWithExactly(JSON.stringify(runtimeErrors[0].error, undefined, 2)));
ok(clcFake.red.getCall(1).calledWithExactly(runtimeErrors[0].error));
ok(clcFake.red.getCall(2).calledWithExactly(runtimeErrors[1].browser.name));
ok(clcFake.red.getCall(3).calledWithExactly(JSON.stringify(runtimeErrors[1].error, undefined, 2)));
ok(clcFake.red.getCall(3).calledWithExactly(runtimeErrors[1].error.message));
ok(clcFake.red.getCall(4).calledWithExactly(runtimeErrors[2].browser.name));
ok(clcFake.red.getCall(5).calledWithExactly(JSON.stringify(runtimeErrors[2].error, undefined, 2)));
});
});

Expand Down

0 comments on commit 2601ea2

Please sign in to comment.