onerror: Goes back to normal end request routine after onError finishes

This commit is contained in:
Jonatan Nilsson 2021-10-10 20:08:43 +00:00
parent a56513b772
commit f0454d11c2
2 changed files with 14 additions and 4 deletions

View file

@ -458,7 +458,6 @@ export class Flaska {
requestEnd(err, ctx) {
if (err) {
this._onerror(err, ctx)
return ctx.res.end()
}
if (ctx.req.complete) {
return

View file

@ -9,10 +9,21 @@ t.describe('#requestEnd()', function() {
t.test('calls onerror correctly on error', function(cb) {
const assertError = new Error('test')
const assertBody = { a: 1 }
let onFinish = function() {
const assertStatus = 501
// Calculated manually just in case
const assertBodyLength = 7
let onFinish = function(body) {
try {
assert.strictEqual(ctx.status, 501)
assert.strictEqual(ctx.status, assertStatus)
assert.strictEqual(ctx.body, assertBody)
assert.strictEqual(ctx.res.statusCode, assertStatus)
assert.strictEqual(ctx.res.setHeader.callCount, 2)
assert.strictEqual(ctx.res.setHeader.firstCall[0], 'Content-Type')
assert.strictEqual(ctx.res.setHeader.firstCall[1], 'application/json; charset=utf-8')
assert.strictEqual(ctx.res.setHeader.secondCall[0], 'Content-Length')
assert.strictEqual(ctx.res.setHeader.secondCall[1], assertBodyLength)
assert.ok(body)
assert.strictEqual(body, '{"a":1}')
cb()
} catch (err) { cb(err) }
}
@ -22,7 +33,7 @@ t.describe('#requestEnd()', function() {
flaska.onerror(function(err, inctx) {
assert.strictEqual(err, assertError)
assert.strictEqual(inctx, ctx)
inctx.status = 501
inctx.status = assertStatus
inctx.body = assertBody
})