This commit is contained in:
zjt 2024-05-28 15:40:04 +08:00
parent 019a6c433d
commit 8564747a82
2 changed files with 14 additions and 0 deletions

View File

@ -2,8 +2,10 @@ import Koa from "koa";
import router from "./routers/router";
import bodyParser from "koa-bodyparser";
import InitHandler from "./routers/handler";
import catchError from "./utils/catchError";
// 使用路由
const app = new Koa();
app.use(catchError);
app.use(bodyParser());

12
src/utils/catchError.ts Normal file
View File

@ -0,0 +1,12 @@
const catchError = async (ctx: any, next: any) => {
try {
await next();
} catch (error:any) {
if (error.errorCode) {
console.log("捕获到异常")
return ctx.body = error.toString();
}
}
}
export default catchError;