Tambah logging detail untuk debug Failed to fetch di lokal

This commit is contained in:
mwpn
2025-12-19 05:22:27 +07:00
parent 7d1e27fb0b
commit 801ea1bfcd
2 changed files with 35 additions and 2 deletions

View File

@@ -33,8 +33,11 @@ async function apiRequest(path, options = {}) {
body: options.body ? JSON.stringify(options.body) : null
};
console.log('[API] Request:', { method: config.method, url, headers: { ...headers, Authorization: token ? 'Bearer ***' : 'none' } });
try {
const res = await fetch(url, config);
console.log('[API] Response status:', res.status, res.statusText);
if (res.status === 401) {
// Unauthorized → clear token & redirect to login
@@ -77,7 +80,20 @@ async function apiRequest(path, options = {}) {
return json;
} catch (err) {
console.error('API error', { url, error: err });
console.error('[API] Request failed:', {
url,
error: err,
message: err.message,
stack: err.stack
});
// Tambahkan info lebih detail untuk "Failed to fetch"
if (err.message === 'Failed to fetch' || err.message.includes('fetch')) {
const detailedError = new Error(`Gagal terhubung ke API: ${url}. Pastikan backend API sudah running di ${API_BASE_URL}`);
detailedError.originalError = err;
throw detailedError;
}
throw err;
}
}