Skip to content

Commit 7977eb7

Browse files
committed
Add support for headers in errors
1 parent 93c356a commit 7977eb7

2 files changed

Lines changed: 36 additions & 1 deletion

File tree

lib/context.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,8 +117,9 @@ var proto = module.exports = {
117117
return;
118118
}
119119

120-
// unset all headers
120+
// unset all headers, and set those specified
121121
this.res._headers = {};
122+
this.set(err.headers);
122123

123124
// force text/plain
124125
this.type = 'text';

test/context/onerror.js

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,40 @@ describe('ctx.onerror(err)', function(){
5252
})
5353
})
5454

55+
it('should keep headers specified in the error', function(done){
56+
var app = koa();
57+
58+
app.use(function *(next){
59+
this.set('Vary', 'Accept-Encoding');
60+
this.set('X-CSRF-Token', 'asdf');
61+
this.body = 'response';
62+
63+
throw Object.assign(new Error('boom'), {
64+
status: 418,
65+
expose: true,
66+
headers: {
67+
'X-New-Header': 'Value'
68+
}
69+
})
70+
})
71+
72+
var server = app.listen();
73+
74+
request(server)
75+
.get('/')
76+
.expect(418)
77+
.expect('Content-Type', 'text/plain; charset=utf-8')
78+
.expect('X-New-Header', 'Value')
79+
.end(function(err, res){
80+
if (err) return done(err);
81+
82+
res.headers.should.not.have.property('vary');
83+
res.headers.should.not.have.property('x-csrf-token');
84+
85+
done();
86+
})
87+
})
88+
5589
describe('when invalid err.status', function(){
5690
describe('not number', function(){
5791
it('should respond 500', function(done){

0 commit comments

Comments
 (0)