feat: set err.headerSent before app error event emit (#919)
This commit is contained in:
parent
0c28d1f57b
commit
e452b68bd9
2 changed files with 29 additions and 2 deletions
|
@ -106,14 +106,18 @@ const proto = module.exports = {
|
||||||
|
|
||||||
if (!(err instanceof Error)) err = new Error(`non-error thrown: ${err}`);
|
if (!(err instanceof Error)) err = new Error(`non-error thrown: ${err}`);
|
||||||
|
|
||||||
|
let headerSent = false;
|
||||||
|
if (this.headerSent || !this.writable) {
|
||||||
|
headerSent = err.headerSent = true;
|
||||||
|
}
|
||||||
|
|
||||||
// delegate
|
// delegate
|
||||||
this.app.emit('error', err, this);
|
this.app.emit('error', err, this);
|
||||||
|
|
||||||
// nothing we can do here other
|
// nothing we can do here other
|
||||||
// than delegate to the app-level
|
// than delegate to the app-level
|
||||||
// handler and log.
|
// handler and log.
|
||||||
if (this.headerSent || !this.writable) {
|
if (headerSent) {
|
||||||
err.headerSent = true;
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -86,6 +86,29 @@ describe('ctx.onerror(err)', () => {
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it('should ignore error after headerSent', done => {
|
||||||
|
const app = new Koa();
|
||||||
|
|
||||||
|
app.on('error', err => {
|
||||||
|
err.message.should.equal('mock error');
|
||||||
|
err.headerSent.should.equal(true);
|
||||||
|
done();
|
||||||
|
});
|
||||||
|
|
||||||
|
app.use(async ctx => {
|
||||||
|
ctx.status = 200;
|
||||||
|
ctx.set('X-Foo', 'Bar');
|
||||||
|
ctx.flushHeaders();
|
||||||
|
await Promise.reject(new Error('mock error'));
|
||||||
|
ctx.body = 'response';
|
||||||
|
});
|
||||||
|
|
||||||
|
request(app.listen())
|
||||||
|
.get('/')
|
||||||
|
.expect('X-Foo', 'Bar')
|
||||||
|
.expect(200, () => {});
|
||||||
|
});
|
||||||
|
|
||||||
describe('when invalid err.status', () => {
|
describe('when invalid err.status', () => {
|
||||||
describe('not number', () => {
|
describe('not number', () => {
|
||||||
it('should respond 500', done => {
|
it('should respond 500', done => {
|
||||||
|
|
Loading…
Reference in a new issue