-
-
Notifications
You must be signed in to change notification settings - Fork 3.2k
Improve checks for Error in onerror handlers #1468
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,3 @@ | ||
|
||
'use strict'; | ||
|
||
const assert = require('assert'); | ||
|
@@ -205,6 +204,40 @@ describe('ctx.onerror(err)', () => { | |
}); | ||
}); | ||
|
||
describe('when error from another scope thrown', () => { | ||
it('should handle it like a normal error', async() => { | ||
const ExternError = require('vm').runInNewContext('Error'); | ||
|
||
const app = new Koa(); | ||
const error = Object.assign(new ExternError('boom'), { | ||
status: 418, | ||
expose: true | ||
}); | ||
app.use((ctx, next) => { | ||
throw error; | ||
}); | ||
|
||
const server = app.listen(); | ||
|
||
const gotRightErrorPromise = new Promise((resolve, reject) => { | ||
app.on('error', receivedError => { | ||
try { | ||
assert.strictEqual(receivedError, error); | ||
resolve(); | ||
} catch (e) { | ||
reject(e); | ||
} | ||
}); | ||
}); | ||
|
||
await request(server) | ||
.get('/') | ||
.expect(418); | ||
|
||
await gotRightErrorPromise; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I checked that both assertions are failing in master. |
||
}); | ||
}); | ||
|
||
describe('when non-error thrown', () => { | ||
it('should response non-error thrown message', () => { | ||
const app = new Koa(); | ||
|
@@ -248,7 +281,7 @@ describe('ctx.onerror(err)', () => { | |
}); | ||
|
||
app.use(async ctx => { | ||
throw {key: 'value'}; // eslint-disable-line no-throw-literal | ||
throw { key: 'value' }; // eslint-disable-line no-throw-literal | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Looks like my editor fixed some lint warnings automatically, please tell me if you'd prefer that I revert that. |
||
}); | ||
|
||
request(app.callback()) | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I checked that this is failing in master.