Skip to content

Commit b698822

Browse files
Surface custom game store errors
1 parent a957394 commit b698822

2 files changed

Lines changed: 26 additions & 10 deletions

File tree

src/api/analysis.ts

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -358,11 +358,18 @@ export const storeCustomGame = async (data: {
358358
body: JSON.stringify(data),
359359
})
360360

361+
const bodyText = await res.text()
362+
361363
if (!res.ok) {
362-
console.error(`Failed to store custom game: ${await res.text()}`)
364+
console.error(`Failed to store custom game: ${bodyText}`)
365+
throw new Error(
366+
`Failed to store custom game (${res.status} ${res.statusText})${
367+
bodyText ? `: ${bodyText}` : ''
368+
}`,
369+
)
363370
}
364371

365-
return res.json()
372+
return JSON.parse(bodyText)
366373
}
367374

368375
export const deleteCustomGame = async (gameId: string): Promise<void> => {

src/pages/analysis/[...id].tsx

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -347,14 +347,23 @@ const Analysis: React.FC<Props> = ({
347347
const handleCustomAnalysis = useCallback(
348348
(type: 'fen' | 'pgn', data: string, name?: string) => {
349349
;(async () => {
350-
const { game_id } = await storeCustomGame({
351-
name: name,
352-
pgn: type === 'pgn' ? data : undefined,
353-
fen: type === 'fen' ? data : undefined,
354-
})
355-
356-
setShowCustomModal(false)
357-
router.push(`/analysis/${game_id}/custom`)
350+
try {
351+
const { game_id } = await storeCustomGame({
352+
name: name,
353+
pgn: type === 'pgn' ? data : undefined,
354+
fen: type === 'fen' ? data : undefined,
355+
})
356+
357+
setShowCustomModal(false)
358+
router.push(`/analysis/${game_id}/custom`)
359+
} catch (error) {
360+
const message =
361+
error instanceof Error
362+
? error.message
363+
: 'Failed to store custom game'
364+
console.error('Custom analysis import failed:', error)
365+
toast.error(message)
366+
}
358367
})()
359368
},
360369
[],

0 commit comments

Comments
 (0)