Tambah logging detail untuk debug Failed to fetch di lokal
This commit is contained in:
@@ -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;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user