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_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_when_an_error_is_thrown_should_log_the_error_status_if_avaiable.log b/test/fixtures/logger_when_an_error_is_thrown_should_log_the_error_status_if_avaiable.log new file mode 100644 index 0000000..7005325 --- /dev/null +++ b/test/fixtures/logger_when_an_error_is_thrown_should_log_the_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..1a187cf 100644 --- a/test/index.js +++ b/test/index.js @@ -273,31 +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 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() {