fix: check response.ok before parsing JSON in fetch calls
Fix JSON parsing errors when server returns non-JSON responses: - Check response.ok before calling response.json() - Add fallback error messages using status text - Apply fix to all fetch calls across 12 components This prevents 'JSON.parse: unexpected character' errors when server returns HTML error pages or other non-JSON responses.
This commit is contained in:
@@ -56,10 +56,13 @@ export default function EditTournamentForm({ tournament }: EditTournamentFormPro
|
||||
}),
|
||||
})
|
||||
|
||||
const data = await response.json()
|
||||
|
||||
if (!response.ok) {
|
||||
setError(data.error || "Failed to update tournament")
|
||||
try {
|
||||
const data = await response.json()
|
||||
setError(data.error || "Failed to update tournament")
|
||||
} catch {
|
||||
setError(`Failed to update tournament: ${response.status} ${response.statusText}`)
|
||||
}
|
||||
setIsLoading(false)
|
||||
return
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user