Skip to content

Commit

Permalink
Send default text/plain body if message is undefined
Browse files Browse the repository at this point in the history
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
  • Loading branch information
simonratner committed Nov 19, 2015
1 parent ce7dbf3 commit 5480413
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 1 deletion.
2 changes: 1 addition & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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];
}

Expand Down
19 changes: 19 additions & 0 deletions test/text.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 () {});
Expand Down Expand Up @@ -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();
}
Expand Down

0 comments on commit 5480413

Please sign in to comment.