diff --git a/lib/middleware/debug.js b/lib/middleware/debug.js index 5167bc843..e30854fe8 100644 --- a/lib/middleware/debug.js +++ b/lib/middleware/debug.js @@ -14,9 +14,9 @@ module.exports = async (ctx, next) => { await next(); if (!ctx.debug.routes[ctx._matchedRoute]) { - ctx.debug.routes[ctx._matchedRoute] = 0; + ctx._matchedRoute && (ctx.debug.routes[ctx._matchedRoute] = 0); } - ctx.debug.routes[ctx._matchedRoute]++; + ctx._matchedRoute && ctx.debug.routes[ctx._matchedRoute]++; if (ctx.response.get('X-Koa-Redis-Cache') || ctx.response.get('X-Koa-Memory-Cache')) { ctx.debug.hitCache++; diff --git a/lib/middleware/onerror.js b/lib/middleware/onerror.js index 6d93bc4a2..161a7c331 100644 --- a/lib/middleware/onerror.js +++ b/lib/middleware/onerror.js @@ -51,15 +51,15 @@ module.exports = async (ctx, next) => { ctx.debug.errorPaths[ctx.request.path]++; if (!ctx.debug.errorRoutes[ctx._matchedRoute]) { - ctx.debug.errorRoutes[ctx._matchedRoute] = 0; + ctx._matchedRoute && (ctx.debug.errorRoutes[ctx._matchedRoute] = 0); } - ctx.debug.errorRoutes[ctx._matchedRoute]++; + ctx._matchedRoute && ctx.debug.errorRoutes[ctx._matchedRoute]++; if (!ctx.state.debuged) { if (!ctx.debug.routes[ctx._matchedRoute]) { - ctx.debug.routes[ctx._matchedRoute] = 0; + ctx._matchedRoute && (ctx.debug.routes[ctx._matchedRoute] = 0); } - ctx.debug.routes[ctx._matchedRoute]++; + ctx._matchedRoute && ctx.debug.routes[ctx._matchedRoute]++; if (ctx.response.get('X-Koa-Redis-Cache') || ctx.response.get('X-Koa-Memory-Cache')) { ctx.debug.hitCache++; diff --git a/test/middleware/debug.js b/test/middleware/debug.js index 46e0f23a2..dfca5f3e6 100644 --- a/test/middleware/debug.js +++ b/test/middleware/debug.js @@ -43,7 +43,7 @@ describe('debug', () => { expect(value).toBe('8'); break; case 'hot routes:': - expect(value).toBe('4 undefined
3 /test/:id
'); + expect(value).toBe('3 /test/:id
'); break; case 'hot paths:': expect(value).toBe('3 /test/1
2 /test/2
2 /test/empty
1 /
'); @@ -52,7 +52,7 @@ describe('debug', () => { expect(value).toBe('5 233.233.233.233
3 233.233.233.234
'); break; case 'hot error routes:': - expect(value).toBe('1 /test/:id
1 undefined
'); + expect(value).toBe('1 /test/:id
'); break; case 'hot error paths:': expect(value).toBe('2 /test/empty
');