From b6aea969f88690cbf015a7e2d5f93fa5cc57944f Mon Sep 17 00:00:00 2001 From: dead_horse Date: Tue, 14 Apr 2015 10:30:19 +0800 Subject: [PATCH] non-error thrown, fixed #432 --- lib/context.js | 3 +-- test/context/onerror.js | 18 ++++++++++++++++++ 2 files changed, 19 insertions(+), 2 deletions(-) diff --git a/lib/context.js b/lib/context.js index e81077c..fa13a71 100644 --- a/lib/context.js +++ b/lib/context.js @@ -7,7 +7,6 @@ var createError = require('http-errors'); var httpAssert = require('http-assert'); var delegate = require('delegates'); var statuses = require('statuses'); -var assert = require('assert'); /** * Context prototype. @@ -103,7 +102,7 @@ var proto = module.exports = { // to node-style callbacks. if (null == err) return; - assert(err instanceof Error, 'non-error thrown: ' + err); + if (!(err instanceof Error)) err = new Error('non-error thrown: ' + err); // delegate this.app.emit('error', err, this); diff --git a/test/context/onerror.js b/test/context/onerror.js index b5d8132..9db2259 100644 --- a/test/context/onerror.js +++ b/test/context/onerror.js @@ -93,4 +93,22 @@ describe('ctx.onerror(err)', function(){ }) }) }) + + describe('when non-error thrown', function(){ + it('should response non-error thrown message', function(done){ + var app = koa(); + + app.use(function *(next){ + throw 'string error'; + }) + + var server = app.listen(); + + request(server) + .get('/') + .expect(500) + .expect('Content-Type', 'text/plain; charset=utf-8') + .expect('Internal Server Error', done); + }) + }) })