fix: 房间加载失败时显示具体错误原因,便于线上排查

This commit is contained in:
2026-02-28 17:30:09 +08:00
parent 039887547d
commit 3e7bb51618
+10 -4
View File
@@ -179,7 +179,7 @@ export default function BlindboxLobbyPage() {
const [creating, setCreating] = useState(false); const [creating, setCreating] = useState(false);
const [joining, setJoining] = useState(false); const [joining, setJoining] = useState(false);
const [error, setError] = useState(""); const [error, setError] = useState("");
const [loadError, setLoadError] = useState(false); const [loadError, setLoadError] = useState<string | false>(false);
const [confirmDeleteId, setConfirmDeleteId] = useState<string | null>(null); const [confirmDeleteId, setConfirmDeleteId] = useState<string | null>(null);
const [deletingId, setDeletingId] = useState<string | null>(null); const [deletingId, setDeletingId] = useState<string | null>(null);
@@ -193,12 +193,15 @@ export default function BlindboxLobbyPage() {
} }
try { try {
const res = await fetch(`/api/blindbox/rooms?userId=${p.id}`, { cache: "no-store" }); const res = await fetch(`/api/blindbox/rooms?userId=${p.id}`, { cache: "no-store" });
if (!res.ok) throw new Error(); if (!res.ok) {
const body = await res.json().catch(() => null);
throw new Error(body?.error ?? `HTTP ${res.status}`);
}
const data = await res.json(); const data = await res.json();
setRooms(Array.isArray(data.rooms) ? data.rooms : []); setRooms(Array.isArray(data.rooms) ? data.rooms : []);
setLoadError(false); setLoadError(false);
} catch { } catch (e) {
if (!silent) setLoadError(true); if (!silent) setLoadError(e instanceof Error ? e.message : "未知错误");
} finally { } finally {
if (!silent) setLoading(false); if (!silent) setLoading(false);
} }
@@ -434,6 +437,9 @@ export default function BlindboxLobbyPage() {
> >
<Package size={36} className="text-purple-400/30" strokeWidth={1.5} /> <Package size={36} className="text-purple-400/30" strokeWidth={1.5} />
<p className="text-sm text-muted"></p> <p className="text-sm text-muted"></p>
{typeof loadError === "string" && (
<p className="max-w-xs text-center text-[11px] text-muted/60 break-all">{loadError}</p>
)}
<button <button
onClick={() => fetchRooms()} onClick={() => fetchRooms()}
className="mt-1 text-xs font-medium text-purple-400 active:text-purple-300" className="mt-1 text-xs font-medium text-purple-400 active:text-purple-300"