fix: 修复代码审查发现的所有 bug

Critical 级别:
- zhipu.ts: 添加 API 响应边界检查
- DictionaryClient.tsx: 添加 entries 数组边界检查
- subtitleParser.ts: 修复 getNearestIndex 逻辑错误

High 级别:
- text-speaker/page.tsx: 修复非空断言和 ref 检查
- folder-repository.ts: 添加 user 关系 null 检查

Medium 级别:
- InFolder.tsx: 修复 throw result.message 为 throw new Error()
- localStorageOperators.ts: 返回类型改为 T | null,添加 schema 验证
- SaveList.tsx: 处理 data 可能为 null 的情况
This commit is contained in:
2026-03-09 19:11:49 +08:00
parent 020744b353
commit c83aefabfa
8 changed files with 75 additions and 59 deletions

View File

@@ -47,7 +47,12 @@ async function getAnswer(prompt: string | Messages): Promise<string> {
: prompt;
const response = await callZhipuAPI(messages);
return response.choices[0].message.content.trim() as string;
if (!response.choices?.[0]?.message?.content) {
throw new Error("AI API 返回空响应");
}
return response.choices[0].message.content.trim();
}
export { getAnswer };

View File

@@ -1,7 +1,9 @@
"use client";
import { z } from "zod";
interface LocalStorageOperator<T> {
get: () => T;
get: () => T | null;
set: (value: T) => void;
}
@@ -9,22 +11,29 @@ export function getLocalStorageOperator<T extends z.ZodType>(
key: string,
schema: T
): LocalStorageOperator<z.infer<T>> {
const get = (): z.infer<T> => {
const get = (): z.infer<T> | null => {
if (typeof window === "undefined") {
return [] as unknown as z.infer<T>;
return null;
}
try {
const item = localStorage.getItem(key);
if (item === null) {
return [] as unknown as z.infer<T>;
return null;
}
const parsed = JSON.parse(item);
return schema.parse(parsed);
const result = schema.safeParse(parsed);
if (!result.success) {
console.warn(`[localStorage] Schema validation failed for key "${key}":`, result.error.message);
return null;
}
return result.data;
} catch (error) {
console.error(`Error reading from localStorage key "${key}":`, error);
return [] as unknown as z.infer<T>;
console.error(`[localStorage] Error reading key "${key}":`, error instanceof Error ? error.message : String(error));
return null;
}
};
@@ -36,7 +45,7 @@ export function getLocalStorageOperator<T extends z.ZodType>(
try {
localStorage.setItem(key, JSON.stringify(value));
} catch (error) {
console.error(`Error writing to localStorage key "${key}":`, error);
console.error(`[localStorage] Error writing key "${key}":`, error instanceof Error ? error.message : String(error));
}
};