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:
@@ -35,10 +35,16 @@ export default function EditUserForm({ user, availablePlayers }: EditUserFormPro
|
||||
body: JSON.stringify(formData),
|
||||
})
|
||||
|
||||
const data = await response.json()
|
||||
|
||||
if (!response.ok) {
|
||||
throw new Error(data.error || "Failed to update user")
|
||||
try {
|
||||
const errorData = await response.json()
|
||||
throw new Error(errorData.error || "Failed to update user")
|
||||
} catch (jsonError) {
|
||||
if (jsonError instanceof Error && jsonError.message !== "Failed to update user") {
|
||||
throw jsonError
|
||||
}
|
||||
throw new Error(`Failed to update user: ${response.status} ${response.statusText}`)
|
||||
}
|
||||
}
|
||||
|
||||
setSuccess("User updated successfully!")
|
||||
|
||||
Reference in New Issue
Block a user