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:
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("流程图提交失败")
|
||||
|
||||
@@ -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 }
|
||||
}
|
||||
}
|
||||
@@ -1,4 +1,5 @@
|
||||
<script setup lang="ts">
|
||||
import { errorMessage } from "utils/api"
|
||||
import { h, computed, ref, onMounted, watch } from "vue"
|
||||
import { watchDebounced } from "@vueuse/core"
|
||||
import { parseTime } from "utils/functions"
|
||||
@@ -68,10 +69,10 @@ async function loadUserProgress() {
|
||||
if (res.problems) {
|
||||
allProblems.value = res.problems
|
||||
}
|
||||
} catch (err: any) {
|
||||
} catch (err) {
|
||||
// finally 里收掉 loading:以前 loading.value = false 写在 await 之后,
|
||||
// 请求一失败(403、断网)转圈就永远停不下来
|
||||
message.error("加载用户进度失败:" + (err.data || "未知错误"))
|
||||
message.error("加载用户进度失败:" + errorMessage(err, "未知错误"))
|
||||
} finally {
|
||||
loading.value = false
|
||||
}
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
<script setup lang="ts">
|
||||
import { errorMessage } from "utils/api"
|
||||
import {
|
||||
getProblemSetDetail,
|
||||
getProblemSetProblems,
|
||||
@@ -90,8 +91,8 @@ async function handleJoinProblemSet() {
|
||||
message.success("成功加入题单!")
|
||||
// 加入题单后加载用户徽章
|
||||
await loadUserBadges()
|
||||
} catch (err: any) {
|
||||
message.error("加入题单失败:" + (err.data || "未知错误"))
|
||||
} catch (err) {
|
||||
message.error("加入题单失败:" + errorMessage(err, "未知错误"))
|
||||
} finally {
|
||||
isJoining.value = false
|
||||
}
|
||||
|
||||
@@ -505,7 +505,7 @@ async function listMyClassRank() {
|
||||
if (myClassScope.value === "window") {
|
||||
myClassQuery.page = 1
|
||||
}
|
||||
} catch (err: any) {
|
||||
} catch (err) {
|
||||
console.error(err)
|
||||
}
|
||||
}
|
||||
@@ -517,7 +517,7 @@ async function listWeeklyRank() {
|
||||
weeklyMe.value = res.me
|
||||
weeklyTotal.value = res.total
|
||||
weeklyStart.value = res.start
|
||||
} catch (err: any) {
|
||||
} catch (err) {
|
||||
console.error(err)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
import { errorCode } from "utils/api"
|
||||
import { formatISO, getTime, parseISO } from "date-fns"
|
||||
import { useUserStore } from "shared/store/user"
|
||||
import { ContestStatus, ContestType } from "utils/constants"
|
||||
@@ -96,9 +97,9 @@ export const useContestStore = defineStore("contest", () => {
|
||||
_getProblems(contestID)
|
||||
}
|
||||
return null
|
||||
} catch (err: any) {
|
||||
} catch (err) {
|
||||
toggleAccess(false)
|
||||
return (err?.error as string | undefined) ?? "unknown"
|
||||
return errorCode(err) ?? "unknown"
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
<script setup lang="ts">
|
||||
import { errorCode } from "utils/api"
|
||||
import { NButton, NFlex, NTag, NText } from "naive-ui"
|
||||
import { useRouteQuery } from "@vueuse/router"
|
||||
import {
|
||||
@@ -225,8 +226,8 @@ async function retryFlowchart(submissionId: string) {
|
||||
}
|
||||
try {
|
||||
await retryFlowchartSubmission(submissionId)
|
||||
} catch (err: any) {
|
||||
message.error(retryTips[err?.error] ?? "重新评分失败")
|
||||
} catch (err) {
|
||||
message.error(retryTips[errorCode(err) ?? ""] ?? "重新评分失败")
|
||||
return
|
||||
}
|
||||
message.success("重新评分已提交")
|
||||
|
||||
Reference in new issue
Block a user