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

+5 -4
View File
@@ -1,4 +1,5 @@
<script setup lang="ts">
import { errorCode } from "utils/api"
import { STORAGE_KEY } from "utils/constants"
import storage from "utils/storage"
import { getClassUsernames, login } from "../api"
@@ -124,13 +125,13 @@ function submit() {
merged.username = form.value.class + merged.username
}
await login(merged)
} catch (err: any) {
} catch (err) {
// 判错误码而不是错误文案:文案在后端,改一个字这里就静默掉进「无法登录」
if (err.error === "account-disabled") {
if (errorCode(err) === "account-disabled") {
authStore.setLoginError("此账号已被封禁")
} else if (err.error === "invalid-credentials") {
} else if (errorCode(err) === "invalid-credentials") {
authStore.setLoginError("用户名或密码不正确")
} else if (err.error === "too-many-login-attempts") {
} else if (errorCode(err) === "too-many-login-attempts") {
authStore.setLoginError("密码错误次数太多,请 15 分钟后再试")
} else {
authStore.setLoginError("无法登录")
+5 -4
View File
@@ -1,4 +1,5 @@
<script setup lang="ts">
import { errorCode } from "utils/api"
import { signup } from "../api"
import { storeToRefs } from "pinia"
import { useAuthModalStore } from "../store/authModal"
@@ -47,12 +48,12 @@ function submit() {
email: form.value.email,
password: form.value.password,
})
} catch (err: any) {
if (err.error === "username-exists") {
} catch (err) {
if (errorCode(err) === "username-exists") {
authStore.setSignupError("用户名已存在")
} else if (err.error === "email-exists") {
} else if (errorCode(err) === "email-exists") {
authStore.setSignupError("邮箱已存在")
} else if (err.error === "too-many-registrations") {
} else if (errorCode(err) === "too-many-registrations") {
authStore.setSignupError("注册太频繁,请稍后再试")
} else {
authStore.setSignupError("无法注册")