refactor(api): 请求体解析收成 readJson / parseBody
54 处 handler 都手写同一段: const parsed = X.safeParse(await c.req.json().catch(() => null)) if (!parsed.success) return failure(c, 400, "invalid-request", ...) 漏写 .catch 就是一个畸形请求打出 500。 - http.ts 加 readJson(读 + 校验)和 parseBody(再加上 400)。parseBody 不给 message 时回 zod 的第一条 issue —— 后台表单原来就是这么写的,fallback 文案 「Invalid payload」/「参数错误」只在 issues 为空时才用得上,实际走不到 - 51 处换成 parseBody;另 3 处失败时带别的错误码或和路径参数一起判,换成 readJson - 错误码、状态码、文案、判断顺序都不变 实跑核过:非 JSON 请求体回原来的固定文案,公告标题为空回 zod issue。 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
c43da01ff2
commit
f2ca34fd4b
21 files changed
+234
-432
No files matched your search
@@ -23,7 +23,7 @@ import { Hono } from "hono"
|
||||
|
||||
import { requireAuth, requireSuperAdmin, type AppEnv } from "../auth/middleware"
|
||||
import { db, schema } from "../db"
|
||||
import { failure, success } from "../http"
|
||||
import { failure, parseBody, success } from "../http"
|
||||
import { JudgeStatus } from "../judge/status"
|
||||
import { objectValue, queryInteger, sampleUser } from "./helpers"
|
||||
|
||||
@@ -187,11 +187,12 @@ contentRoutes.get("/messages", requireAuth, async (c) => {
|
||||
*/
|
||||
contentRoutes.post("/messages", requireSuperAdmin, async (c) => {
|
||||
const user = c.get("user")!
|
||||
const parsed = createMessageRequestSchema.safeParse(
|
||||
await c.req.json().catch(() => null),
|
||||
const parsed = await parseBody(
|
||||
c,
|
||||
createMessageRequestSchema,
|
||||
"Invalid message payload",
|
||||
)
|
||||
if (!parsed.success)
|
||||
return failure(c, 400, "invalid-request", "Invalid message payload")
|
||||
if (!parsed.success) return parsed.response
|
||||
if (parsed.data.recipientId === user.id)
|
||||
return failure(
|
||||
c,
|
||||
@@ -263,11 +264,12 @@ contentRoutes.get("/problems/:id/reaction", requireAuth, async (c) => {
|
||||
|
||||
contentRoutes.post("/problems/:id/reaction", requireAuth, async (c) => {
|
||||
const problemId = queryInteger(c.req.param("id"), 0, { min: 1 })
|
||||
const parsed = setReactionRequestSchema.safeParse(
|
||||
await c.req.json().catch(() => null),
|
||||
const parsed = await parseBody(
|
||||
c,
|
||||
setReactionRequestSchema,
|
||||
"Invalid reaction",
|
||||
)
|
||||
if (!parsed.success)
|
||||
return failure(c, 400, "invalid-request", "Invalid reaction")
|
||||
if (!parsed.success) return parsed.response
|
||||
const user = c.get("user")!
|
||||
const [[problem], [solved]] = await Promise.all([
|
||||
db
|
||||
@@ -447,11 +449,12 @@ contentRoutes.get("/learn/progress", requireAuth, async (c) => {
|
||||
contentRoutes.post("/tutorials/:id/progress", requireAuth, async (c) => {
|
||||
const user = c.get("user")!
|
||||
const id = queryInteger(c.req.param("id"), 0, { min: 1 })
|
||||
const parsed = tutorialProgressPingSchema.safeParse(
|
||||
await c.req.json().catch(() => null),
|
||||
const parsed = await parseBody(
|
||||
c,
|
||||
tutorialProgressPingSchema,
|
||||
"Invalid progress payload",
|
||||
)
|
||||
if (!parsed.success)
|
||||
return failure(c, 400, "invalid-request", "Invalid progress payload")
|
||||
if (!parsed.success) return parsed.response
|
||||
const [tutorial] = await db
|
||||
.select({ id: schema.tutorial.id })
|
||||
.from(schema.tutorial)
|
||||
@@ -501,11 +504,12 @@ contentRoutes.post("/tutorials/:id/progress", requireAuth, async (c) => {
|
||||
contentRoutes.post("/exercises/:id/attempts", requireAuth, async (c) => {
|
||||
const user = c.get("user")!
|
||||
const id = queryInteger(c.req.param("id"), 0, { min: 1 })
|
||||
const parsed = exerciseAttemptRequestSchema.safeParse(
|
||||
await c.req.json().catch(() => null),
|
||||
const parsed = await parseBody(
|
||||
c,
|
||||
exerciseAttemptRequestSchema,
|
||||
"Invalid attempt payload",
|
||||
)
|
||||
if (!parsed.success)
|
||||
return failure(c, 400, "invalid-request", "Invalid attempt payload")
|
||||
if (!parsed.success) return parsed.response
|
||||
// 练习跟着教程走:教程没公开,它底下的练习也不该能上报
|
||||
const [exercise] = await db
|
||||
.select({ id: schema.exercise.id })
|
||||
|
||||
Reference in new issue
Block a user