Skip to content

Commit abaca8c

Browse files
committed
Fixes #59 Print stack trace for errors
1 parent 20d2523 commit abaca8c

File tree

3 files changed

+27
-8
lines changed

3 files changed

+27
-8
lines changed

lib/index.js

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -232,12 +232,30 @@ internals.purdy.prototype.Array = function (array, path, depth) {
232232

233233
internals.purdy.prototype.Error = function (err) {
234234

235+
const stack = internals.formatStack(err.stack, this.indentLevel, this.settings.indent);
235236
if (Object.keys(err).length === 0) {
236-
return this.colorize('[' + err + ']', 'error');
237+
return this.colorize(stack, 'error');
237238
}
239+
const plain = this.settings.plain;
240+
this.settings.plain = true;
238241
const obj = this.Object(err, '', null);
239-
const message = err.message ? ': ' + err.message : '';
240-
return obj.replace(/^{/, '{ ' + this.colorize('[Error' + message + ']', 'error') );
242+
this.settings.plain = plain;
243+
return obj.replace(/^{/, '{ ' + this.colorize(stack, 'error') );
244+
};
245+
246+
247+
internals.formatStack = function (stack, indentLevel, indent) {
248+
249+
const spaces = internals.spaces((indentLevel) * indent);
250+
let stackArr = stack.split('\n');
251+
const firstLine = stackArr.shift();
252+
stackArr = stackArr.map(function (line) {
253+
254+
return spaces + line;
255+
});
256+
stackArr.unshift(firstLine);
257+
258+
return stackArr.join('\n');
241259
};
242260

243261

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "purdy",
3-
"version": "3.0.2",
3+
"version": "3.1.0",
44
"description": "Pretty print objects in real purdy colors. Allows clearer visualization of objects than you get from most pretty printers due to colors. It will also print out the complete path to an object, something that's extremly useful for debugging. Purdy will also print out the path to access a variable using Hoek format making it useful on accessing values.",
55
"main": "lib/index.js",
66
"scripts": {

test/purdy.js

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ describe('Purdy', () => {
2525

2626
const error = new Error('plain error');
2727
const out = Purdy.stringify(error);
28-
expect(out).to.equal('\u001b[31m[Error: plain error]\u001b[39m');
28+
expect(out).to.match(/\[31mError: plain error/);
2929
done();
3030
});
3131

@@ -34,7 +34,7 @@ describe('Purdy', () => {
3434
const error = new Error();
3535
error.murray = 'rothbard';
3636
const out = Purdy.stringify(error);
37-
expect(out).to.equal('{ \u001b[31m[Error]\u001b[39m\n \u001b[1m\u001b[37mmurray\u001b[39m\u001b[22m: \u001b[33m\'rothbard\'\u001b[39m\n}');
37+
expect(out).to.match(/31mError[^]*murray: 'rothbard'/);
3838
done();
3939
});
4040

@@ -43,19 +43,20 @@ describe('Purdy', () => {
4343
const error = new Error('error with properties');
4444
error.code = 'BAD';
4545
const out = Purdy.stringify(error);
46-
expect(out).to.equal('{ \u001b[31m[Error: error with properties]\u001b[39m\n \u001b[1m\u001b[37mcode\u001b[39m\u001b[22m: \u001b[33m\'BAD\'\u001b[39m\n}');
46+
expect(out).to.match(/31mError: error with properties[^]*code: 'BAD'/);
4747
done();
4848
});
4949

5050
it('should display an error with detail', (done) => {
5151

5252
const error = new Error('some bad, bad error');
5353
error.withKey = 'key';
54+
error.withMore = 'more';
5455
const obj = {
5556
theError: error
5657
};
5758
const out = Purdy.stringify(obj, { depth: null });
58-
expect(out).to.equal('{\n \u001b[1m\u001b[37mtheError\u001b[39m\u001b[22m: { \u001b[31m[Error: some bad, bad error]\u001b[39m\n \u001b[1m\u001b[37mwithKey\u001b[39m\u001b[22m: \u001b[33m\'key\'\u001b[39m\n }\n}');
59+
expect(out).to.match(/theError[^]*: { [^]*31mError: some bad, bad error[^]*withKey: 'key'[^]*withMore: 'more'/);
5960
done();
6061
});
6162
});

0 commit comments

Comments
 (0)