From 801ea1bfcd7921ae1e0bfb7566431353e8dfb253 Mon Sep 17 00:00:00 2001 From: mwpn Date: Fri, 19 Dec 2025 05:22:27 +0700 Subject: [PATCH] Tambah logging detail untuk debug Failed to fetch di lokal --- public/dashboard/js/api.js | 18 +++++++++++++++++- public/index.html | 19 ++++++++++++++++++- 2 files changed, 35 insertions(+), 2 deletions(-) diff --git a/public/dashboard/js/api.js b/public/dashboard/js/api.js index c168596..b4133c7 100644 --- a/public/dashboard/js/api.js +++ b/public/dashboard/js/api.js @@ -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; } } diff --git a/public/index.html b/public/index.html index 3fa3a44..e4ac2f4 100644 --- a/public/index.html +++ b/public/index.html @@ -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';