corr add group

This commit is contained in:
alma 2026-01-14 12:45:49 +01:00
parent 44e6ccbb6c
commit b033087a60

View File

@ -494,35 +494,69 @@ export default function EquipePage() {
if (!response.ok) { if (!response.ok) {
// Try to parse error message from response // Try to parse error message from response
let errorMessage = "Échec de la création du groupe"; 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 { try {
// Check if response has content // Check if response has content
const contentType = response.headers.get('content-type'); const contentType = response.headers.get('content-type') || '';
const text = await response.text(); let text: string = '';
if (text && contentType?.includes('application/json')) { try {
try { text = await response.text();
const errorData = JSON.parse(text); } catch (textError) {
errorMessage = errorData.message || errorData.error || errorMessage; console.error('Error reading response text:', textError);
} catch { // If we can't read the text, use the default error message and skip parsing
// If JSON parsing fails, use the text text = '';
errorMessage = text || errorMessage; }
// 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) { } catch (error) {
// If anything fails, use status text or default // If anything fails reading the response, use the default message
console.error('Error parsing error response:', error); console.error('Error processing error response:', error);
errorMessage = response.statusText || errorMessage; // errorMessage already has a default value, so we keep it
} }
throw new Error(errorMessage); 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]); setGroups(prev => [...prev, newGroup]);
setNewGroupDialogOpen(false); setNewGroupDialogOpen(false);
setNewGroupName(""); setNewGroupName("");