[issue #45] Fix bunyan CLI (default output mode) to not crash on a 'res' field that isn't a response object, but a string.

This commit is contained in:
Trent Mick 2012-10-10 11:39:29 -07:00
parent 337d7d977c
commit 8a7d743152
4 changed files with 21 additions and 9 deletions

View file

@ -2,7 +2,8 @@
## bunyan 0.14.2 (not yet released)
(nothing yet)
- [issue #45] Fix bunyan CLI (default output mode) to not crash on a 'res'
field that isn't a response object, but a string.
## bunyan 0.14.1

View file

@ -612,8 +612,9 @@ function emitRecord(rec, line, opts, stylize) {
}
delete rec.msg;
if (rec.req) {
if (rec.req && typeof(rec.req) === 'object') {
var req = rec.req;
delete rec.req;
var headers = req.headers;
var s = format("%s %s HTTP/%s%s", req.method,
req.url,
@ -644,10 +645,10 @@ function emitRecord(rec, line, opts, stylize) {
rec["req." + k] = req[k];
})
}
delete rec.req;
if (rec.client_req) {
if (rec.client_req && typeof(rec.client_req) === 'object') {
var client_req = rec.client_req;
delete rec.client_req;
var headers = client_req.headers;
var hostHeaderLine = '';
var s = '';
@ -682,10 +683,10 @@ function emitRecord(rec, line, opts, stylize) {
})
details.push(indent(s));
}
delete rec.client_req;
if (rec.res) {
if (rec.res && typeof(rec.res) === 'object') {
var res = rec.res;
delete rec.res;
var s = '';
if (res.header) {
s += res.header.trimRight();
@ -717,9 +718,8 @@ function emitRecord(rec, line, opts, stylize) {
// This *does* have the potential to stomp on a literal 'res.foo' key.
Object.keys(res).forEach(function (k) {
rec["res." + k] = res[k];
})
});
}
delete rec.res;
if (rec.err && rec.err.stack) {
details.push(indent(rec.err.stack));
@ -978,7 +978,7 @@ process.on('uncaughtException', function (err) {
console.error('* * *');
console.error('* node version:', process.version);
console.error('* bunyan version:', getVersion());
console.error('* argv:', process.argv);
console.error('* argv: %j', process.argv);
console.error('* stack:');
console.error(indent(err.stack));
console.error('* * *');

View file

@ -316,3 +316,13 @@ test('robust req handling', function (t) {
t.end();
});
});
test('non-object res field', function (t) {
var expect = '[2012-10-10T16:14:07.610Z] INFO: cnapi.get_existing_nics/24440 on 710c784f-6aa5-428c-9074-e046c3af884e: got existing: 02:08:20:d7:53:e0 (job_uuid=3499b13e-dbca-4331-b13a-f164c0da320a, nic=<unknown>, res="error: Unknown nic \\"020820d753e0\\"")\n';
exec(BUNYAN + ' corpus/non-object-res.log', function (err, stdout, stderr) {
t.error(err);
t.equal(stdout, expect);
t.end();
});
});

View file

@ -0,0 +1 @@
{"name":"cnapi.get_existing_nics","job_uuid":"3499b13e-dbca-4331-b13a-f164c0da320a","hostname":"710c784f-6aa5-428c-9074-e046c3af884e","pid":24440,"level":30,"nic":"<unknown>","res":"error: Unknown nic \"020820d753e0\"","msg":"got existing: 02:08:20:d7:53:e0","time":"2012-10-10T16:14:07.610Z","v":0}