Skip to content

Commit

Permalink
✨ Allow debug logs to handle error objects like error logs
Browse files Browse the repository at this point in the history
  • Loading branch information
wwilsman committed Sep 2, 2020
1 parent 924c806 commit bce4fa1
Show file tree
Hide file tree
Showing 6 changed files with 25 additions and 15 deletions.
2 changes: 1 addition & 1 deletion packages/cli-exec/src/commands/exec/ping.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ export class Ping extends Command {
log.info('Percy is running');
} catch (err) {
log.error('Percy is not running');
log.debug(err.toString());
log.debug(err);
this.exit(1);
}
}
Expand Down
2 changes: 1 addition & 1 deletion packages/cli-exec/src/commands/exec/stop.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ export class Stop extends Command {
log.info('Percy has stopped');
} catch (err) {
log.error('Percy is not running');
log.debug(err.toString());
log.debug(err);
this.exit(1);
}
}
Expand Down
2 changes: 1 addition & 1 deletion packages/config/src/load.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ export default function load({
}
} catch (error) {
log.debug('Failed to load or parse config file');
log.debug(error.toString());
log.debug(error);
}
}

Expand Down
2 changes: 1 addition & 1 deletion packages/config/test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -282,7 +282,7 @@ describe('PercyConfig', () => {
expect(stdio[2]).toHaveLength(0);
expect(stdio[1]).toEqual([
'[percy] Failed to load or parse config file\n',
'[percy] Error: test\n'
expect.stringContaining('[percy] Error: test')
]);
});

Expand Down
24 changes: 13 additions & 11 deletions packages/logger/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,18 +66,20 @@ logger.loglevel = function loglevel(level, flags = {}) {
this.transports[0].level = level;
};

// Patch the error method to handle error objects. Winston accepts objects with
// messages and meta as an argument but will fail to log real error instances.
logger.error = function(message) {
// libraries may also throw errors which are not technically Error instances
if (typeof message === 'object') {
// get the stack trace for debug (no ternary to always fallback to string)
message = (this.loglevel() === 'debug' && message.stack) || message.toString();
}
// Patch the error and debug methods to handle error objects. Winston accepts objects
// with messages and meta as an argument but will fail to log real error instances.
for (let method of ['error', 'debug']) {
logger[method] = function(message) {
// libraries may also throw errors which are not technically Error instances
if (typeof message === 'object') {
// get the stack trace for debug (no ternary to always fallback to string)
message = (this.loglevel() === 'debug' && message.stack) || message.toString();
}

// return super.error(message)
return this.constructor.prototype.error.call(this, message);
};
// return super[method](message)
return this.constructor.prototype[method].call(this, message);
};
}

// Patch the query method to return a promise, query the file transport only,
// and allow filtering logs with a filter function.
Expand Down
8 changes: 8 additions & 0 deletions packages/logger/test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,14 @@ describe('logger', () => {
});
});

describe('#debug()', () => {
it('is patched to log error instance strings', () => {
let err = { toString: () => 'error' };
stdio.capture(() => log.debug(err));
expect(stdio[1]).toEqual(['[percy] error\n']);
});
});

describe('#query()', () => {
beforeEach(() => {
stdio.capture(() => {
Expand Down

0 comments on commit bce4fa1

Please sign in to comment.