diff --git a/lib/application.js b/lib/application.js index 741fff9..d0ceba0 100644 --- a/lib/application.js +++ b/lib/application.js @@ -131,8 +131,9 @@ module.exports = class Application extends Emitter { return (req, res) => { res.statusCode = 404; const ctx = this.createContext(req, res); - onFinished(res, ctx.onerror); - fn(ctx).then(() => respond(ctx)).catch(ctx.onerror); + const onerror = err => ctx.onerror(err); + onFinished(res, onerror); + fn(ctx).then(() => respond(ctx)).catch(onerror); }; } @@ -152,7 +153,6 @@ module.exports = class Application extends Emitter { request.ctx = response.ctx = context; request.response = response; response.request = request; - context.onerror = context.onerror.bind(context); context.originalUrl = request.originalUrl = req.url; context.cookies = new Cookies(req, res, { keys: this.keys, diff --git a/lib/response.js b/lib/response.js index 01c5c1d..87ece39 100644 --- a/lib/response.js +++ b/lib/response.js @@ -164,7 +164,7 @@ module.exports = { // stream if ('function' == typeof val.pipe) { onFinish(this.res, destroy.bind(null, val)); - ensureErrorHandler(val, this.ctx.onerror); + ensureErrorHandler(val, err => this.ctx.onerror(err)); // overwriting if (null != original && original != val) this.remove('Content-Length');