corr add group

This commit is contained in:
alma 2026-01-14 12:43:55 +01:00
parent faf6397f30
commit 44e6ccbb6c
2 changed files with 61 additions and 23 deletions

View File

@ -206,32 +206,66 @@ export async function POST(req: Request) {
if (!response.ok) { if (!response.ok) {
// Try to get error details from Keycloak // Try to get error details from Keycloak
let errorMessage = 'Échec de la création du groupe'; let errorMessage = 'Échec de la création du groupe';
const status = response.status;
const statusText = response.statusText;
try { 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(); const text = await response.text();
if (text.trim()) { logger.debug('Keycloak error response', {
// Try to parse as JSON status,
try { statusText,
const errorData = JSON.parse(text); contentType,
// Keycloak error format: { errorMessage: "..." } or { error: "..." } hasText: !!text,
errorMessage = errorData.errorMessage || errorData.error || errorData.message || errorMessage; textLength: text?.length || 0
} catch { });
// If not JSON, use the text as error message
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; errorMessage = text;
} }
} else { } else {
// Empty response, use status text // If no content, use status text or a status-specific message
errorMessage = response.statusText || errorMessage; 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( return NextResponse.json(
{ message: errorMessage }, { message: errorMessage },
{ status: response.status } { status }
); );
} }

View File

@ -495,24 +495,28 @@ export default function EquipePage() {
// 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";
try { 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(); const text = await response.text();
if (text.trim()) { if (text && contentType?.includes('application/json')) {
// Try to parse as JSON
try { try {
const errorData = JSON.parse(text); const errorData = JSON.parse(text);
errorMessage = errorData.message || errorData.error || errorMessage; errorMessage = errorData.message || errorData.error || errorMessage;
} catch { } catch {
// If not JSON, use the text as error message // If JSON parsing fails, use the text
errorMessage = text; errorMessage = text || errorMessage;
} }
} else if (text) {
// If there's text but not JSON, use it
errorMessage = text;
} else { } else {
// Empty response, use status text // If no content, use status text
errorMessage = response.statusText || errorMessage; errorMessage = response.statusText || errorMessage;
} }
} catch (readError) { } catch (error) {
// If reading response fails, use status text // If anything fails, use status text or default
console.error('Error parsing error response:', error);
errorMessage = response.statusText || errorMessage; errorMessage = response.statusText || errorMessage;
} }
throw new Error(errorMessage); throw new Error(errorMessage);