diff --git a/CHANGES.md b/CHANGES.md index ad5612d..311e4bf 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -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 diff --git a/bin/bunyan b/bin/bunyan index f1bd398..4387237 100755 --- a/bin/bunyan +++ b/bin/bunyan @@ -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('* * *'); diff --git a/test/cli.test.js b/test/cli.test.js index 99bdfb5..4542b2b 100644 --- a/test/cli.test.js +++ b/test/cli.test.js @@ -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=, 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(); + }); +}); + diff --git a/test/corpus/non-object-res.log b/test/corpus/non-object-res.log new file mode 100644 index 0000000..cf3bbd0 --- /dev/null +++ b/test/corpus/non-object-res.log @@ -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":"","res":"error: Unknown nic \"020820d753e0\"","msg":"got existing: 02:08:20:d7:53:e0","time":"2012-10-10T16:14:07.610Z","v":0}