refactor(前端): 接口错误改成 ApiError 类,catch 不再写 any

拦截器原来 reject 一个裸对象 `{ error, data }`,没有类型也没有类型守卫,于是 46 处
`catch (err: any)` 直接读 `err.data` / `err.error` —— 代码 bug 抛出的 TypeError 也按
接口错误读,弹出来的是 undefined 或英文。

- utils/api.ts:reject 改成 `ApiError extends Error`,字段名沿用 error(错误码)/
  data(文案),存量读法语义不变;加 isApiError / errorCode / errorMessage。
  errorMessage 对非接口错误一律用 fallback,不把 TypeError 的英文甩给学生
- 46 处 catch 全部换掉 any(45 处接口错误 + pythonSyntaxCheck 里 Skulpt 的错误对象)
- 断网等没有响应体的情况,文案从英文 "Request failed" 换成「网络异常,请检查网络后重试」

浏览器实跑核过:登录密码错误仍显示「用户名或密码不正确」(错误码分支)、
接口错误 reject 出来是 ApiError、网络不通时拿到中文文案、非接口错误走 fallback。

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
xuyueandClaude Opus 5.5 committed 2026-09-27 15:12:40 +08:00
1 parent 6406794971
commit 7a390323fe
29 files changed
+167 -106

No files matched your search

@@ -1,4 +1,5 @@
<script setup lang="ts">
import { errorCode, errorMessage } from "utils/api"
import { Icon } from "@iconify/vue"
import { storeToRefs } from "pinia"
import { formatCode, getReaction, submitCode } from "oj/api"
@@ -133,10 +134,10 @@ async function submit() {
language: formatLang,
})
codeStore.setCode(res.code)
} catch (e: any) {
if (e?.error === "format-error") {
} catch (e) {
if (errorCode(e) === "format-error") {
// 仅 Python 会出现:代码本身存在语法错误
message.warning(`代码格式化失败:${e.data},请检查代码后重试`)
message.warning(`代码格式化失败:${errorMessage(e)},请检查代码后重试`)
return
}
// server-error / 网络异常:格式化工具问题,静默降级,提交原代码
@@ -1,4 +1,5 @@
<script lang="ts" setup>
import { errorCode } from "utils/api"
import { toRefs } from "vue"
// 工具函数
@@ -256,13 +257,13 @@ async function submitFlowchartData() {
}
message.success("流程图已提交,请耐心等待评分")
} catch (error: any) {
} catch (error) {
loading.value = false
// 按错误码分支(见 utils/api.ts 的约定)。限流是最容易撞上的一种:
// 只说「提交失败」的话,学生会以为是自己的图有问题,然后反复点,越点越久
if (error?.error === "too-many-submissions") {
if (errorCode(error) === "too-many-submissions") {
message.warning("提交太频繁了,缓一会儿再交")
} else if (error?.error === "flowchart-not-allowed") {
} else if (errorCode(error) === "flowchart-not-allowed") {
message.error("这道题不接受流程图提交")
} else {
message.error("流程图提交失败")
+3 -2
View File
@@ -1,4 +1,5 @@
<script setup lang="ts">
import { errorCode } from "utils/api"
import { getProblem } from "oj/api"
import { useBreakpoints } from "shared/composables/breakpoints"
import { storeToRefs } from "pinia"
@@ -120,9 +121,9 @@ async function init() {
try {
const res = await getProblem(problemID, contestID)
problem.value = res
} catch (err: any) {
} catch (err) {
problem.value = null
if (err.error === "contest-not-started") {
if (errorCode(err) === "contest-not-started") {
errMsg.value = "比赛还没有开始"
}
}
@@ -46,8 +46,10 @@ export async function checkPythonSyntax(
try {
Sk.compile(code, "prog.py", "exec")
return null
} catch (e: any) {
const line: number = e?.traceback?.[0]?.lineno ?? 1
} catch (e) {
// Skulpt 的语法错误对象,不是 Error 的子类
const traceback = (e as { traceback?: { lineno?: number }[] }).traceback
const line = traceback?.[0]?.lineno ?? 1
return { line }
}
}