Files
recipe-app/frontend/lib/error-handler.ts
T
Nils-Johan Gynther d5b360fd62 Refactor logging in IcaRecipeParser and QuickImportService to use NestJS Logger
- Updated IcaRecipeParser to replace console.log statements with Logger for better logging practices.
- Enhanced QuickImportService with Logger for consistent logging and error handling.
- Changed quantity validation in CreateRecipeIngredientDto and CreateRecipeDto to allow zero.
- Removed CanonicalNameForm and NameForm components from the frontend.
- Updated EditProductForm to use a select dropdown for categories instead of a text input.
- Added validation for product name, canonical name, and category length in product update action.
- Refactored unit options into a separate file for better reusability across components.
- Removed unused API fetching functions and inventory types to clean up the codebase.
2026-04-16 18:18:27 +02:00

46 lines
1.3 KiB
TypeScript

/**
* Utility för att parse HTTP-responses och extrahera tydliga felmeddelanden
*/
export async function parseErrorResponse(response: Response): Promise<string> {
const status = response.status;
try {
const data = await response.json();
// Om backend skickade ett felmeddelande
if (data.message) {
return data.message;
}
if (data.error) {
return data.error;
}
if (data.details) {
return data.details;
}
} catch {
// Inte JSON, försök text
try {
const text = await response.text();
if (text && text.length < 200) {
return text;
}
} catch {
// Inget text-innehål
}
}
// Fallback baserat på HTTP-status
const defaultMessages: Record<number, string> = {
400: 'Ogiltiga data. Kontrollera dina inmatningar.',
401: 'Du är inte autentiserad. Logga in.',
403: 'Du har inte behörighet till detta.',
404: 'Resursen hittades inte.',
409: 'Konflikten med befintlig data.',
422: 'Valideringen misslyckades. Kontrollera dina inmatningar.',
500: 'Serverfel. Försök igen senare.',
503: 'Tjänsten är inte tillgänglig.',
};
return defaultMessages[status] || `Fel (${status}). Försök igen senare.`;
}