diff --git a/test/fixtures/scripts/log-string-exception.js b/test/fixtures/scripts/log-string-exception.js new file mode 100644 index 000000000..1015a9802 --- /dev/null +++ b/test/fixtures/scripts/log-string-exception.js @@ -0,0 +1,25 @@ +/* + * log-string-exceptions.js: A test fixture for logging string exceptions in winston. + * + * (C) 2011 Charlie Robbins + * MIT LICENCE + * + */ + +var path = require('path'), + winston = require('../../../lib/winston'); + +var logger = new (winston.Logger)({ + transports: [ + new (winston.transports.File)({ + filename: path.join(__dirname, '..', 'logs', 'string-exception.log'), + handleExceptions: true + }) + ] +}); + +logger.handleExceptions(); + +setTimeout(function () { + throw 'OMG NEVER DO THIS STRING EXCEPTIONS ARE AWFUL'; +}, 1000); \ No newline at end of file diff --git a/test/helpers.js b/test/helpers.js index 9747ac26a..079d08dbe 100644 --- a/test/helpers.js +++ b/test/helpers.js @@ -12,8 +12,8 @@ var assert = require('assert'), spawn = require('child_process').spawn, util = require('util'), vows = require('vows'), - winston = require('../lib/winston'); - + winston = require('../lib/winston'); + var helpers = exports; helpers.size = function (obj) { @@ -23,7 +23,7 @@ helpers.size = function (obj) { size++; } } - + return size; }; @@ -123,6 +123,9 @@ helpers.assertHandleExceptions = function (options) { helpers.assertProcessInfo(data.process); helpers.assertOsInfo(data.os); helpers.assertTrace(data.trace); + if (options.message) { + assert.equal('uncaughtException: ' + options.message, data.message); + } } } } @@ -137,24 +140,24 @@ helpers.testSyslogLevels = function (transport, assertMsg, assertFn) { helpers.testLevels = function (levels, transport, assertMsg, assertFn) { var tests = {}; - + Object.keys(levels).forEach(function (level) { var test = { topic: function () { transport.log(level, 'test message', {}, this.callback.bind(this, null)); } }; - + test[assertMsg] = assertFn; tests['with the ' + level + ' level'] = test; }); - + var metadatatest = { topic: function () { transport.log('info', 'test message', { metadata: true }, this.callback.bind(this, null)); } }; - + metadatatest[assertMsg] = assertFn; tests['when passed metadata'] = metadatatest; @@ -167,7 +170,7 @@ helpers.testLevels = function (levels, transport, assertMsg, assertFn) { primmetadatatest[assertMsg] = assertFn; tests['when passed primitive metadata'] = primmetadatatest; - var circmetadata = { }; + var circmetadata = { }; circmetadata['metadata'] = circmetadata; var circmetadatatest = { diff --git a/test/log-exception-test.js b/test/log-exception-test.js index 164dcdfdf..eafdf4091 100644 --- a/test/log-exception-test.js +++ b/test/log-exception-test.js @@ -26,11 +26,16 @@ vows.describe('winston/logger/exceptions').addBatch({ script: path.join(__dirname, 'fixtures', 'scripts', 'default-exceptions.js'), logfile: path.join(__dirname, 'fixtures', 'logs', 'default-exception.log') }), + "when strings are thrown as errors": helpers.assertHandleExceptions({ + script: path.join(__dirname, 'fixtures', 'scripts', 'log-string-exception.js'), + logfile: path.join(__dirname, 'fixtures', 'logs', 'string-exception.log'), + message: 'OMG NEVER DO THIS STRING EXCEPTIONS ARE AWFUL' + }), "when a custom exitOnError function is set": { topic: function () { var that = this, scriptDir = path.join(__dirname, 'fixtures', 'scripts'); - + that.child = spawn('node', [path.join(scriptDir, 'exit-on-error.js')]); setTimeout(this.callback.bind(this), 1500); }, @@ -45,7 +50,7 @@ vows.describe('winston/logger/exceptions').addBatch({ var that = this, child = spawn('node', [path.join(__dirname, 'fixtures', 'scripts', 'unhandle-exceptions.js')]), exception = path.join(__dirname, 'fixtures', 'logs', 'unhandle-exception.log'); - + helpers.tryUnlink(exception); child.on('exit', function () { exists(exception, that.callback.bind(this, null));