From 548041378bae457d3d5e58f167570f4592f756ad Mon Sep 17 00:00:00 2001 From: Simon Ratner Date: Wed, 18 Nov 2015 17:29:08 -0800 Subject: [PATCH] Send default text/plain body if message is undefined Koa detects undefined response body and automatically sets the status to 204 if explicit status does not allow an empty body. See: https://github.com/koajs/koa/blob/master/lib/response.js#L135 --- index.js | 2 +- test/text.test.js | 19 +++++++++++++++++++ 2 files changed, 20 insertions(+), 1 deletion(-) diff --git a/index.js b/index.js index 7874273..9639934 100644 --- a/index.js +++ b/index.js @@ -122,7 +122,7 @@ function onerror(app, options) { this.res._headers = {}; this.body = isDev || err.expose - ? err.message + ? err.message || http.STATUS_CODES[this.status] : http.STATUS_CODES[this.status]; } diff --git a/test/text.test.js b/test/text.test.js index fd43da8..4607db5 100644 --- a/test/text.test.js +++ b/test/text.test.js @@ -42,6 +42,18 @@ describe('text.test.js', function () { .expect('this message will be expose', done); }); + it('should show default message if undefined', function (done) { + var app = koa(); + app.on('error', function () {}); + onerror(app); + app.use(exposeBlankError); + + request(app.callback()) + .get('/') + .set('Accept', 'text/plain') + .expect(500, done); + }); + it('should stream error ok', function (done) { var app = koa(); app.on('error', function () {}); @@ -80,6 +92,13 @@ function* exposeError() { throw err; } +function* exposeBlankError() { + var err = new Error(); + err.message = undefined; + err.expose = true; + throw err; +} + function* commonError() { foo(); }