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;
}
}

View File

@@ -85,10 +85,18 @@
submitBtn.textContent = 'Memproses...';
try {
// Import config untuk cek API Base URL
const { API_CONFIG } = await import('./dashboard/js/config.js');
console.log('[Login] API Base URL:', API_CONFIG.BASE_URL);
const {
apiLogin
} = await import('./dashboard/js/api.js');
console.log('[Login] Attempting login for:', username);
const response = await apiLogin(username, password);
console.log('[Login] Response:', response);
if (response.token) {
Auth.saveToken(response.token);
Auth.saveUser(response.user);
@@ -98,7 +106,16 @@
throw new Error('Response tidak berisi token.');
}
} catch (error) {
errorDiv.textContent = error.message || 'Login gagal. Silakan coba lagi.';
console.error('[Login] Error:', error);
let errorMessage = error.message || 'Login gagal. Silakan coba lagi.';
// Tambahkan info lebih detail untuk debugging
if (error.message === 'Failed to fetch' || error.message.includes('fetch')) {
errorMessage = 'Gagal terhubung ke server API. Pastikan backend API sudah running di ' +
(await import('./dashboard/js/config.js')).then(m => m.API_CONFIG.BASE_URL).catch(() => 'http://localhost:8000');
}
errorDiv.textContent = errorMessage;
errorDiv.classList.remove('hidden');
submitBtn.disabled = false;
submitBtn.textContent = 'Login';