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) {
// 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 }
);
}

View File

@ -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);