From 44e6ccbb6cf9d4f16f1ebaaa9e41a53015ddea2c Mon Sep 17 00:00:00 2001 From: alma Date: Wed, 14 Jan 2026 12:43:55 +0100 Subject: [PATCH] corr add group --- app/api/groups/route.ts | 64 +++++++++++++++++++++++++++--------- app/missions/equipe/page.tsx | 20 ++++++----- 2 files changed, 61 insertions(+), 23 deletions(-) diff --git a/app/api/groups/route.ts b/app/api/groups/route.ts index f6377c3..49b9850 100644 --- a/app/api/groups/route.ts +++ b/app/api/groups/route.ts @@ -206,32 +206,66 @@ export async function POST(req: Request) { if (!response.ok) { // Try to get error details from Keycloak let errorMessage = 'Échec de la création du groupe'; + const status = response.status; + const statusText = response.statusText; + try { - // Read response as text first (can only read body once) + // Check if response has content before trying to parse + const contentType = response.headers.get('content-type') || ''; const text = await response.text(); - if (text.trim()) { - // Try to parse as JSON - try { - const errorData = JSON.parse(text); - // Keycloak error format: { errorMessage: "..." } or { error: "..." } - errorMessage = errorData.errorMessage || errorData.error || errorData.message || errorMessage; - } catch { - // If not JSON, use the text as error message + logger.debug('Keycloak error response', { + status, + statusText, + contentType, + hasText: !!text, + textLength: text?.length || 0 + }); + + if (text && text.trim().length > 0) { + if (contentType.includes('application/json')) { + try { + const errorData = JSON.parse(text); + // Keycloak error format: { errorMessage: "..." } or { error: "..." } + errorMessage = errorData.errorMessage || errorData.error || errorData.message || errorMessage; + } catch (parseError) { + // If JSON parsing fails, use the text as error message + logger.debug('Failed to parse error as JSON, using text', { + text: text.substring(0, 200) // Log first 200 chars + }); + errorMessage = text || errorMessage; + } + } else { + // If there's text but not JSON, use it errorMessage = text; } } else { - // Empty response, use status text - errorMessage = response.statusText || errorMessage; + // If no content, use status text or a status-specific message + if (status === 409) { + errorMessage = 'Un groupe avec ce nom existe déjà'; + } else if (status === 400) { + errorMessage = 'Nom de groupe invalide'; + } else { + errorMessage = statusText || errorMessage; + } + } + } catch (error) { + // If anything fails, use status text or default message + logger.error('Error parsing Keycloak error response', { + status, + statusText, + error: error instanceof Error ? error.message : String(error) + }); + if (status === 409) { + errorMessage = 'Un groupe avec ce nom existe déjà'; + } else { + errorMessage = statusText || errorMessage; } - } catch (readError) { - // If reading response fails, use status text - errorMessage = response.statusText || errorMessage; } return NextResponse.json( { message: errorMessage }, - { status: response.status } + { status } ); } diff --git a/app/missions/equipe/page.tsx b/app/missions/equipe/page.tsx index 3580b43..e9d470c 100644 --- a/app/missions/equipe/page.tsx +++ b/app/missions/equipe/page.tsx @@ -495,24 +495,28 @@ export default function EquipePage() { // Try to parse error message from response let errorMessage = "Échec de la création du groupe"; try { - // Read response as text first (can only read body once) + // Check if response has content + const contentType = response.headers.get('content-type'); const text = await response.text(); - if (text.trim()) { - // Try to parse as JSON + if (text && contentType?.includes('application/json')) { try { const errorData = JSON.parse(text); errorMessage = errorData.message || errorData.error || errorMessage; } catch { - // If not JSON, use the text as error message - errorMessage = text; + // If JSON parsing fails, use the text + errorMessage = text || errorMessage; } + } else if (text) { + // If there's text but not JSON, use it + errorMessage = text; } else { - // Empty response, use status text + // If no content, use status text errorMessage = response.statusText || errorMessage; } - } catch (readError) { - // If reading response fails, use status text + } catch (error) { + // If anything fails, use status text or default + console.error('Error parsing error response:', error); errorMessage = response.statusText || errorMessage; } throw new Error(errorMessage);