fix: api helper voor extra error checks bij fetch dwengo api + schrijffout in controllers/themes

This commit is contained in:
Gabriellvl 2025-03-01 15:15:04 +01:00
parent 0c7f5791ea
commit 91eb374b7e
5 changed files with 45 additions and 8 deletions

View file

@ -0,0 +1,29 @@
import axios from 'axios';
// !!!! when logger is done -> change
/**
* Utility function to fetch data from an API endpoint with error handling.
* Logs errors but does NOT throw exceptions to keep the system running.
*
* @param url The API endpoint to fetch from.
* @param description A short description of what is being fetched (for logging).
* @returns The response data if successful, or null if an error occurs.
*/
export async function fetchWithLogging<T>(url: string, description: string): Promise<T | null> {
try {
const response = await axios.get<T>(url);
return response.data;
} catch (error: any) {
if (error.response) {
if (error.response.status === 404) {
console.error(`❌ ERROR: ${description} not found (404) at "${url}".`);
} else {
console.error(`❌ ERROR: Failed to fetch ${description}. Status: ${error.response.status} - ${error.response.statusText} (URL: "${url}")`);
}
} else {
console.error(`❌ ERROR: Network or unexpected error when fetching ${description}:`, error.message);
}
return null;
}
}