From b033087a601a72b7a7bbe8dd8d4d5a568f67ef12 Mon Sep 17 00:00:00 2001 From: alma Date: Wed, 14 Jan 2026 12:45:49 +0100 Subject: [PATCH] corr add group --- app/missions/equipe/page.tsx | 72 ++++++++++++++++++++++++++---------- 1 file changed, 53 insertions(+), 19 deletions(-) diff --git a/app/missions/equipe/page.tsx b/app/missions/equipe/page.tsx index e9d470c..8275349 100644 --- a/app/missions/equipe/page.tsx +++ b/app/missions/equipe/page.tsx @@ -494,35 +494,69 @@ export default function EquipePage() { if (!response.ok) { // Try to parse error message from response let errorMessage = "Échec de la création du groupe"; + + // Provide status-specific default messages + if (response.status === 409) { + errorMessage = "Un groupe avec ce nom existe déjà"; + } else if (response.status === 400) { + errorMessage = "Nom de groupe invalide"; + } else if (response.status === 401) { + errorMessage = "Non autorisé"; + } else if (response.status === 500) { + errorMessage = "Erreur serveur lors de la création du groupe"; + } + try { // Check if response has content - const contentType = response.headers.get('content-type'); - const text = await response.text(); + const contentType = response.headers.get('content-type') || ''; + let text: string = ''; - if (text && contentType?.includes('application/json')) { - try { - const errorData = JSON.parse(text); - errorMessage = errorData.message || errorData.error || errorMessage; - } catch { - // If JSON parsing fails, use the text - errorMessage = text || errorMessage; + try { + text = await response.text(); + } catch (textError) { + console.error('Error reading response text:', textError); + // If we can't read the text, use the default error message and skip parsing + text = ''; + } + + // Only try to parse if we have text content + if (text && typeof text === 'string' && text.trim().length > 0) { + if (contentType.includes('application/json')) { + try { + const errorData = JSON.parse(text); + errorMessage = errorData.message || errorData.error || errorMessage; + } catch (parseError) { + // If JSON parsing fails, use the text (truncated if too long) + console.warn('Failed to parse error response as JSON:', parseError); + errorMessage = text.length > 200 ? text.substring(0, 200) + '...' : text; + } + } else { + // If there's text but not JSON, use it (truncated if too long) + errorMessage = text.length > 200 ? text.substring(0, 200) + '...' : text; } - } else if (text) { - // If there's text but not JSON, use it - errorMessage = text; - } else { - // If no content, use status text - errorMessage = response.statusText || errorMessage; } } catch (error) { - // If anything fails, use status text or default - console.error('Error parsing error response:', error); - errorMessage = response.statusText || errorMessage; + // If anything fails reading the response, use the default message + console.error('Error processing error response:', error); + // errorMessage already has a default value, so we keep it } + throw new Error(errorMessage); } - const newGroup = await response.json(); + // Only parse JSON if response is OK + let newGroup; + try { + const text = await response.text(); + if (!text || text.trim().length === 0) { + throw new Error("Réponse vide du serveur"); + } + newGroup = JSON.parse(text); + } catch (parseError) { + console.error("Error parsing success response:", parseError); + throw new Error("Erreur lors de la lecture de la réponse du serveur"); + } + setGroups(prev => [...prev, newGroup]); setNewGroupDialogOpen(false); setNewGroupName("");