Skip to content

Commit

Permalink
优化错误处理,返回更详细的错误信息
Browse files Browse the repository at this point in the history
  • Loading branch information
SunWuyuan committed Jan 27, 2025
1 parent 47cb480 commit 43221a4
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 33 deletions.
42 changes: 25 additions & 17 deletions routes/router_project.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,10 @@ router.post("/", needlogin, async (req, res, next) => {
});
} catch (err) {
logger.error("Error creating new project:", err);
next(err);
res.status(500).send({
status: "error",
message: "创建新作品时出错",
});
}
});

Expand Down Expand Up @@ -110,6 +113,10 @@ router.post("/savefile", needlogin, async (req, res, next) => {
logger.debug("File already exists, skipping.");
} else {
logger.error(err);
return res.status(500).send({
status: "error",
message: "保存源代码时出错",
});
}
});

Expand All @@ -124,7 +131,10 @@ router.post("/savefile", needlogin, async (req, res, next) => {
});
} catch (err) {
logger.error("Error saving source code:", err);
next(err);
res.status(500).send({
status: "error",
message: "保存源代码时出错",
});
}
});

Expand Down Expand Up @@ -240,7 +250,10 @@ router.put("/commit/id/:id", needlogin, async (req, res, next) => {
});
} catch (err) {
logger.error("Error saving source code:", err);
next(err);
res.status(500).send({
status: "error",
message: "提交代码时出错",
});
}
});

Expand Down Expand Up @@ -286,15 +299,18 @@ router.post("/batch", async (req, res, next) => {
});
} catch (err) {
logger.error("Error fetching batch project information:", err);
next(err);
res.status(500).send({
status: "error",
message: "批量获取项目信息时出错",
});
}
});

router.get("/branches/:id", async (req, res, next) => {
router.get("/branches", async (req, res, next) => {
try {
const { id } = req.params;
const { projectid } = req.query;
const hasPermission = await hasProjectPermission(
id,
projectid,
res.locals.userid,
"read"
);
Expand All @@ -306,7 +322,7 @@ router.get("/branches/:id", async (req, res, next) => {
});
}
const result = await prisma.ow_projects_branch.findMany({
where: { projectid: Number(id) },
where: { projectid: Number(projectid) },
});
res.status(200).send({
status: "success",
Expand Down Expand Up @@ -733,20 +749,12 @@ router.get("/:id/:branch/:ref", async (req, res, next) => {
});
}

const accessFileToken = await generateFileAccessToken(
defaultSource,
userId
);

return res.status(200).send({
status: "error",
message: "未初始化的作品",
message: "无有效提交",
commit: {
commit_file: defaultSource,
commit_message: "NoFirstCommit",
commit_date: new Date(),
},
accessFileToken,
});
}

Expand Down
48 changes: 32 additions & 16 deletions routes/router_projectlist.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@ router.post("/star", needlogin, async (req, res, next) => {
await starProject(res.locals.userid, req.body.projectid);
res.status(200).send({ status: "success", message: "收藏成功", star: 1 });
} catch (err) {
next(err);
logger.error("Error starring project:", err);
res.status(500).send({ status: "error", message: "收藏项目时出错" });
}
});

Expand All @@ -38,7 +39,8 @@ router.post("/unstar", needlogin, async (req, res, next) => {
await unstarProject(res.locals.userid, req.body.projectid);
res.status(200).send({ status: "success", message: "取消收藏成功", star: 0 });
} catch (err) {
next(err);
logger.error("Error unstarring project:", err);
res.status(500).send({ status: "error", message: "取消收藏项目时出错" });
}
});

Expand All @@ -52,7 +54,8 @@ router.get("/checkstar", async (req, res, next) => {
const count = await getProjectStars(req.query.projectid);
res.status(200).send({ status: "success", message: "获取成功", star: status ,count: count});
} catch (err) {
next(err);
logger.error("Error checking star status:", err);
res.status(500).send({ status: "error", message: "检查收藏状态时出错" });
}
});
router.get("/listid/:id", async (req, res, next) => {
Expand All @@ -66,7 +69,8 @@ router.get("/listid/:id", async (req, res, next) => {

res.status(200).send({ status: "success", message: "获取成功", data: list });
} catch (err) {
next(err);
logger.error("Error getting project list:", err);
res.status(500).send({ status: "error", message: "获取项目列表时出错" });
}
});

Expand All @@ -76,7 +80,8 @@ router.get("/userid/:id/public", async (req, res, next) => {
const list = await getUserListInfoPublic(req.params.id, res.locals.userid);
res.status(200).send({ status: "success", message: "获取成功", data: list });
} catch (err) {
next(err);
logger.error("Error getting public user list info:", err);
res.status(500).send({ status: "error", message: "获取公共用户列表信息时出错" });
}
});
router.get("/my", async (req, res, next) => {
Expand All @@ -85,31 +90,39 @@ router.get("/my", async (req, res, next) => {
const list = await getUserListInfo(res.locals.userid);
res.status(200).send({ status: "success", message: "获取成功", data: list });
} catch (err) {
next(err);
logger.error("Error getting my list info:", err);
res.status(500).send({ status: "error", message: "获取我的列表信息时出错" });
}
});
router.get("/check", async (req, res, next) => {
var result = await getUserListInfoAndCheak(
res.locals.userid,
req.query.projectid
);
res.status(200).send({ status: "success", message: "获取成功", data: result });
try {
var result = await getUserListInfoAndCheak(
res.locals.userid,
req.query.projectid
);
res.status(200).send({ status: "success", message: "获取成功", data: result });
} catch (err) {
logger.error("Error checking user list info:", err);
res.status(500).send({ status: "error", message: "检查用户列表信息时出错" });
}
});

router.post("/create", needlogin, async (req, res, next) => {
try {
const list = await createList(res.locals.userid, req.body.name);
res.status(200).send({ status: "success", message: "创建成功", data: list });
} catch (err) {
next(err);
logger.error("Error creating list:", err);
res.status(500).send({ status: "error", message: "创建列表时出错" });
}
});
router.post("/delete", needlogin, async (req, res, next) => {
try {
const list = await deleteList(res.locals.userid, req.body.id);
res.status(200).send({ status: "success", message: "删除成功", data: list });
} catch (err) {
next(err);
logger.error("Error deleting list:", err);
res.status(500).send({ status: "error", message: "删除列表时出错" });
}
});
router.post("/add", needlogin, async (req, res, next) => {
Expand All @@ -121,7 +134,8 @@ router.post("/add", needlogin, async (req, res, next) => {
);
res.status(200).send({ status: "success", message: "添加成功", data: list });
} catch (err) {
next(err);
logger.error("Error adding project to list:", err);
res.status(500).send({ status: "error", message: "添加项目到列表时出错" });
}
});
router.post("/remove", needlogin, async (req, res, next) => {
Expand All @@ -133,7 +147,8 @@ router.post("/remove", needlogin, async (req, res, next) => {
);
res.status(200).send({ status: "success", message: "删除成功", data: list });
} catch (err) {
next(err);
logger.error("Error removing project from list:", err);
res.status(500).send({ status: "error", message: "从列表中删除项目时出错" });
}
});
// 修改列表名称,简介,状态
Expand All @@ -142,7 +157,8 @@ router.post("/update/:id", needlogin, async (req, res, next) => {
const list = await updateList(res.locals.userid,req.params.id, req.body);
res.status(200).send({ status: "success", message: "修改成功", data: list });
} catch (err) {
next(err);
logger.error("Error updating list:", err);
res.status(500).send({ status: "error", message: "修改列表时出错" });
}
})
export default router;

0 comments on commit 43221a4

Please sign in to comment.