From 49790775624c422f67057f7bb936f35df920e391 Mon Sep 17 00:00:00 2001 From: dead_horse Date: Fri, 23 May 2014 21:15:03 +0800 Subject: [PATCH] assert non-error obj pass to app.onerror, fixed #287 --- lib/application.js | 7 ++++++- lib/context.js | 7 +++---- 2 files changed, 9 insertions(+), 5 deletions(-) diff --git a/lib/application.js b/lib/application.js index a265a0d..f5599dd 100644 --- a/lib/application.js +++ b/lib/application.js @@ -155,10 +155,15 @@ app.createContext = function(req, res){ */ app.onerror = function(err){ + if (!err) return; + assert(err instanceof Error, 'non-error thrown: ' + err); + if (404 == err.status) return; if ('test' == this.env) return; + + var msg = err.stack || err.toString(); console.error(); - console.error(err.stack.replace(/^/gm, ' ')); + console.error(msg.replace(/^/gm, ' ')); console.error(); }; diff --git a/lib/context.js b/lib/context.js index 1e2f0b7..7b182cd 100644 --- a/lib/context.js +++ b/lib/context.js @@ -91,18 +91,17 @@ var proto = module.exports = { assert(err instanceof Error, 'non-error thrown: ' + err); + // delegate + this.app.emit('error', err, this); + // nothing we can do here other // than delegate to the app-level // handler and log. if (this.headerSent || !this.writable) { err.headerSent = true; - this.app.emit('error', err, this); return; } - // delegate - this.app.emit('error', err, this); - // unset all headers this.res._headers = {};