fix: error handler and debug info

This commit is contained in:
DIYgod
2024-01-21 15:41:42 +08:00
parent 8e0154552c
commit e7e3f689bd
9 changed files with 109 additions and 155 deletions

View File

@@ -1,43 +1,26 @@
import { MiddlewareHandler } from "hono";
import { getRouteNameFromPath } from '@/utils/helpers';
const debug = {
hitCache: 0,
request: 0,
etag: 0,
paths: [],
routes: [],
errorPaths: [],
errorRoutes: [],
}
import { getDebugInfo, setDebugInfo } from "@/utils/debug-info";
const middleware: MiddlewareHandler = async (ctx, next) => {
if (!debug.paths[ctx.req.path]) {
debug.paths[ctx.req.path] = 0;
{
const debug = getDebugInfo();
debug.request++;
setDebugInfo(debug);
}
debug.paths[ctx.req.path]++;
debug.request++;
await next();
const routeName = getRouteNameFromPath(ctx.req.path);
if (routeName) {
if (!debug.routes[routeName]) {
debug.routes[routeName] = 0;
{
const debug = getDebugInfo();
if (ctx.res.headers.get('X-Koa-Redis-Cache') || ctx.res.headers.get('X-Koa-Memory-Cache')) {
debug.hitCache++;
}
debug.routes[routeName]++;
}
if (ctx.res.headers.get('X-Koa-Redis-Cache') || ctx.res.headers.get('X-Koa-Memory-Cache')) {
debug.hitCache++;
}
if (ctx.res.status === 304) {
debug.etag++;
if (ctx.res.status === 304) {
debug.etag++;
}
setDebugInfo(debug);
}
};
export default middleware;
export const getDebugInfo = () => debug