From 027120c39acfe92080c6dda562518ea6a25ad1b4 Mon Sep 17 00:00:00 2001 From: Joel Mukuthu Date: Thu, 3 Nov 2016 19:58:00 +0100 Subject: [PATCH 1/2] Log a thrown error's status when available --- src/index.js | 2 +- ...og_the_thrown_error_status_if_avaiable.log | 4 +++ test/index.js | 28 +++++++++++++++++++ 3 files changed, 33 insertions(+), 1 deletion(-) create mode 100644 test/fixtures/logger_should_log_the_thrown_error_status_if_avaiable.log diff --git a/src/index.js b/src/index.js index e4b61d6..4fa442f 100644 --- a/src/index.js +++ b/src/index.js @@ -360,7 +360,7 @@ export default function createLogger(options = {}) { let $status; if (exception) { - $status = colorize('fatal')('ERR'); + $status = colorize('fatal')(exception.status || 'ERR'); } else if (status >= 100 && status < 200) { $status = colorize('info')(status); } else if (status < 300) { diff --git a/test/fixtures/logger_should_log_the_thrown_error_status_if_avaiable.log b/test/fixtures/logger_should_log_the_thrown_error_status_if_avaiable.log new file mode 100644 index 0000000..7005325 --- /dev/null +++ b/test/fixtures/logger_should_log_the_thrown_error_status_if_avaiable.log @@ -0,0 +1,4 @@ +──‣ ┈┈┈┈┈ GET┈ ┬ / + ╎ { Error + ╎  at stack status: 504 } +504 ┈┈0ms GET┈ ┴ / diff --git a/test/index.js b/test/index.js index cf9e777..50a829b 100644 --- a/test/index.js +++ b/test/index.js @@ -300,6 +300,34 @@ describe('logger', () => { expect(this.output, 'to equal', fixtures[title]); }); + it('should log the thrown error status if avaiable', async function () { + const title = this.test.fullTitle(); + const createLogger = this.createLogger(title); + + const logger = createLogger(); + + const context = { + method: 'GET', + originalUrl: '/' + }; + + const next = () => { + const error = new Error('GatewayTimeout'); + error.status = 504; + error.stack = 'Error\n at stack'; + + throw error; + }; + + try { + await logger(context, next); + } catch (error) { + // Not interested in this one + } + + expect(this.output, 'to equal', fixtures[title]); + }); + it('should expose context.log method', async function() { const title = this.test.fullTitle(); const createLogger = this.createLogger(title); From dc99e6615feea97c15cb3dd24238387bf396d2d6 Mon Sep 17 00:00:00 2001 From: Joel Mukuthu Date: Thu, 3 Nov 2016 20:02:45 +0100 Subject: [PATCH 2/2] Combine error-related unit tests --- ..._error_is_thrown_should_log_the_error.log} | 0 ...ould_log_the_error_status_if_avaiable.log} | 0 test/index.js | 82 ++++++++++--------- 3 files changed, 42 insertions(+), 40 deletions(-) rename test/fixtures/{logger_should_log_an_unhandled_error.log => logger_when_an_error_is_thrown_should_log_the_error.log} (100%) rename test/fixtures/{logger_should_log_the_thrown_error_status_if_avaiable.log => logger_when_an_error_is_thrown_should_log_the_error_status_if_avaiable.log} (100%) diff --git a/test/fixtures/logger_should_log_an_unhandled_error.log b/test/fixtures/logger_when_an_error_is_thrown_should_log_the_error.log similarity index 100% rename from test/fixtures/logger_should_log_an_unhandled_error.log rename to test/fixtures/logger_when_an_error_is_thrown_should_log_the_error.log diff --git a/test/fixtures/logger_should_log_the_thrown_error_status_if_avaiable.log b/test/fixtures/logger_when_an_error_is_thrown_should_log_the_error_status_if_avaiable.log similarity index 100% rename from test/fixtures/logger_should_log_the_thrown_error_status_if_avaiable.log rename to test/fixtures/logger_when_an_error_is_thrown_should_log_the_error_status_if_avaiable.log diff --git a/test/index.js b/test/index.js index 50a829b..1a187cf 100644 --- a/test/index.js +++ b/test/index.js @@ -273,59 +273,61 @@ describe('logger', () => { expect(this.output, 'to equal', fixtures[title]); }); - it('should log an unhandled error', async function () { - const title = this.test.fullTitle(); - const createLogger = this.createLogger(title); + describe('when an error is thrown', () => { + it('should log the error', async function () { + const title = this.test.fullTitle(); + const createLogger = this.createLogger(title); - const logger = createLogger(); + const logger = createLogger(); - const context = { - method: 'GET', - originalUrl: '/' - }; + const context = { + method: 'GET', + originalUrl: '/' + }; - const next = () => { - const error = new Error(); - error.stack = 'Error\n at stack'; + const next = () => { + const error = new Error(); + error.stack = 'Error\n at stack'; - throw error; - }; + throw error; + }; - try { - await logger(context, next); - } catch (error) { - // Not interested in this one - } + try { + await logger(context, next); + } catch (error) { + // Not interested in this one + } - expect(this.output, 'to equal', fixtures[title]); - }); + expect(this.output, 'to equal', fixtures[title]); + }); - it('should log the thrown error status if avaiable', async function () { - const title = this.test.fullTitle(); - const createLogger = this.createLogger(title); + it('should log the error status if avaiable', async function () { + const title = this.test.fullTitle(); + const createLogger = this.createLogger(title); - const logger = createLogger(); + const logger = createLogger(); - const context = { - method: 'GET', - originalUrl: '/' - }; + const context = { + method: 'GET', + originalUrl: '/' + }; - const next = () => { - const error = new Error('GatewayTimeout'); - error.status = 504; - error.stack = 'Error\n at stack'; + const next = () => { + const error = new Error('GatewayTimeout'); + error.status = 504; + error.stack = 'Error\n at stack'; - throw error; - }; + throw error; + }; - try { - await logger(context, next); - } catch (error) { - // Not interested in this one - } + try { + await logger(context, next); + } catch (error) { + // Not interested in this one + } - expect(this.output, 'to equal', fixtures[title]); + expect(this.output, 'to equal', fixtures[title]); + }); }); it('should expose context.log method', async function() {