fix: better error handling for bulk sync, fix NaN% progress display

This commit is contained in:
2026-04-30 23:04:51 +08:00
parent 5f037481ec
commit e38a2fabe7
2 changed files with 30 additions and 11 deletions
+26 -11
View File
@@ -125,7 +125,8 @@ export function GamesClient() {
}) })
if (!res.ok) { if (!res.ok) {
throw new Error(`Sync failed: HTTP ${res.status}`) const errorData = await res.json().catch(() => null)
throw new Error(errorData?.error || `Sync failed: HTTP ${res.status}`)
} }
const data = await res.json() const data = await res.json()
@@ -133,15 +134,20 @@ export function GamesClient() {
setSyncProgress((prev) => ({ setSyncProgress((prev) => ({
...prev, ...prev,
isRunning: false, isRunning: false,
synced: data.synced, synced: data.synced || 0,
failed: data.failed, failed: data.failed || 0,
currentGame: null, currentGame: null,
})) }))
setSyncCompleted(true) setSyncCompleted(true)
setSelectedIds(new Set()) setSelectedIds(new Set())
} catch (error) { } catch (error) {
console.error("Sync selected failed:", error) console.error("Sync selected failed:", error)
alert("Sync failed. Check console for details.") alert(`Sync failed: ${error instanceof Error ? error.message : String(error)}`)
setSyncProgress((prev) => ({
...prev,
isRunning: false,
currentGame: null,
}))
} finally { } finally {
setSyncing(false) setSyncing(false)
} }
@@ -171,7 +177,8 @@ export function GamesClient() {
}) })
if (!res.ok) { if (!res.ok) {
throw new Error(`Sync failed: HTTP ${res.status}`) const errorData = await res.json().catch(() => null)
throw new Error(errorData?.error || `Sync failed: HTTP ${res.status}`)
} }
const data = await res.json() const data = await res.json()
@@ -179,15 +186,20 @@ export function GamesClient() {
setSyncProgress((prev) => ({ setSyncProgress((prev) => ({
...prev, ...prev,
isRunning: false, isRunning: false,
total: data.total, total: data.total || 0,
synced: data.synced, synced: data.synced || 0,
failed: data.failed, failed: data.failed || 0,
currentGame: null, currentGame: null,
})) }))
setSyncCompleted(true) setSyncCompleted(true)
} catch (error) { } catch (error) {
console.error("Sync all failed:", error) console.error("Sync all failed:", error)
alert("Sync failed. Check console for details.") alert(`Sync failed: ${error instanceof Error ? error.message : String(error)}`)
setSyncProgress((prev) => ({
...prev,
isRunning: false,
currentGame: null,
}))
} finally { } finally {
setSyncing(false) setSyncing(false)
} }
@@ -527,7 +539,10 @@ export function GamesClient() {
<div> <div>
<h3 id="sync-overlay-title" className="font-semibold text-text">Syncing Games</h3> <h3 id="sync-overlay-title" className="font-semibold text-text">Syncing Games</h3>
<p className="text-sm text-text/50"> <p className="text-sm text-text/50">
{syncProgress.current} of {syncProgress.total} games {syncProgress.total > 0
? `${syncProgress.current} of ${syncProgress.total} games`
: "Preparing to sync..."
}
</p> </p>
</div> </div>
</div> </div>
@@ -536,7 +551,7 @@ export function GamesClient() {
<div className="mb-4"> <div className="mb-4">
<div className="flex justify-between text-xs text-text/50 mb-1"> <div className="flex justify-between text-xs text-text/50 mb-1">
<span>Progress</span> <span>Progress</span>
<span>{Math.round((syncProgress.current / syncProgress.total) * 100)}%</span> <span>{syncProgress.total > 0 ? Math.round((syncProgress.current / syncProgress.total) * 100) : 0}%</span>
</div> </div>
<div className="h-2 bg-text/10 rounded-full overflow-hidden"> <div className="h-2 bg-text/10 rounded-full overflow-hidden">
<div <div
+4
View File
@@ -294,9 +294,13 @@ export const gameSyncRoutes = new Elysia({ prefix: "/games" })
return { total: 0, synced: 0, failed: 0, message: "No games to sync" }; return { total: 0, synced: 0, failed: 0, message: "No games to sync" };
} }
console.log(`[Bulk Sync] Starting sync of ${gamesToSync.length} games with concurrency ${SYNC_CONCURRENCY}`)
// Process syncs in parallel with controlled concurrency // Process syncs in parallel with controlled concurrency
const { synced, failed } = await syncInParallel(gamesToSync, SYNC_CONCURRENCY); const { synced, failed } = await syncInParallel(gamesToSync, SYNC_CONCURRENCY);
console.log(`[Bulk Sync] Complete: ${synced} synced, ${failed} failed`)
return { return {
total: gamesToSync.length, total: gamesToSync.length,
synced, synced,