From ee1a933096ee31f7ae70b6b2bfa581d30477f783 Mon Sep 17 00:00:00 2001 From: initial-wu Date: Wed, 6 Jun 2018 12:55:20 +0800 Subject: [PATCH] fix: Throw a TypeError instead of a AssertionError (#1199) --- lib/application.js | 4 ++-- test/application/onerror.js | 3 +-- 2 files changed, 3 insertions(+), 4 deletions(-) diff --git a/lib/application.js b/lib/application.js index a9b76fb..49a2b8c 100644 --- a/lib/application.js +++ b/lib/application.js @@ -17,7 +17,7 @@ const statuses = require('statuses'); const Cookies = require('cookies'); const accepts = require('accepts'); const Emitter = require('events'); -const assert = require('assert'); +const util = require('util'); const Stream = require('stream'); const http = require('http'); const only = require('only'); @@ -185,7 +185,7 @@ module.exports = class Application extends Emitter { */ onerror(err) { - assert(err instanceof Error, `non-error thrown: ${err}`); + if (!(err instanceof Error)) throw new TypeError(util.format('non-error thrown: %j', err)); if (404 == err.status || err.expose) return; if (this.silent) return; diff --git a/test/application/onerror.js b/test/application/onerror.js index aaf0547..ee3d396 100644 --- a/test/application/onerror.js +++ b/test/application/onerror.js @@ -3,7 +3,6 @@ const assert = require('assert'); const Koa = require('../..'); -const AssertionError = require('assert').AssertionError; describe('app.onerror(err)', () => { beforeEach(() => { @@ -19,7 +18,7 @@ describe('app.onerror(err)', () => { assert.throws(() => { app.onerror('foo'); - }, AssertionError, 'non-error thrown: foo'); + }, TypeError, 'non-error thrown: foo'); }); it('should do nothing if status is 404', () => {