debug: add console logging and 10s timeout to extension API calls
- apiFetch() now logs method+URL on start and status code on response - Added 10s AbortController timeout to prevent infinite hangs - Added try/catch wrapper in saveSettings() around login() call - This helps diagnose the "stuck on connecting" issue Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
parent
7c2aa0d739
commit
8bb9cf7ce9
1 changed files with 28 additions and 10 deletions
|
|
@ -119,9 +119,14 @@ async function saveSettings() {
|
||||||
document.getElementById('conn-server').textContent = serverUrl.replace(/https?:\/\//, '');
|
document.getElementById('conn-server').textContent = serverUrl.replace(/https?:\/\//, '');
|
||||||
|
|
||||||
showSettingsStatus('Saved — connecting…', 'loading');
|
showSettingsStatus('Saved — connecting…', 'loading');
|
||||||
await login();
|
try {
|
||||||
if (connected) {
|
await login();
|
||||||
showSettingsStatus('✅ Connected successfully!', 'success');
|
if (connected) {
|
||||||
|
showSettingsStatus('✅ Connected successfully!', 'success');
|
||||||
|
}
|
||||||
|
} catch (e) {
|
||||||
|
showSettingsStatus(`❌ ${e.message || 'Unknown error'}`, 'error');
|
||||||
|
setConnStatus('red', e.message || 'Connection failed');
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -308,17 +313,30 @@ async function uploadUDP(item, idx, prefix) {
|
||||||
// ==================== HELPERS ====================
|
// ==================== HELPERS ====================
|
||||||
async function apiFetch(method, path, body) {
|
async function apiFetch(method, path, body) {
|
||||||
const url = config.serverUrl.replace(/\/$/, '') + path;
|
const url = config.serverUrl.replace(/\/$/, '') + path;
|
||||||
|
console.log(`[DW] ${method} ${url}`);
|
||||||
const opts = { method, headers: { 'Content-Type': 'application/json' } };
|
const opts = { method, headers: { 'Content-Type': 'application/json' } };
|
||||||
if (authToken) opts.headers['x-auth-token'] = authToken;
|
if (authToken) opts.headers['x-auth-token'] = authToken;
|
||||||
if (body) opts.body = JSON.stringify(body);
|
if (body) opts.body = JSON.stringify(body);
|
||||||
const r = await fetch(url, opts);
|
const controller = new AbortController();
|
||||||
// Don't treat 401 on /api/login as session expiry — it's just bad credentials
|
const timeout = setTimeout(() => controller.abort(), 10000);
|
||||||
if (r.status === 401 && !path.includes('/api/login')) {
|
opts.signal = controller.signal;
|
||||||
authToken = null;
|
try {
|
||||||
await chrome.storage.local.remove('token');
|
const r = await fetch(url, opts);
|
||||||
throw new Error('Session expired');
|
clearTimeout(timeout);
|
||||||
|
console.log(`[DW] ${method} ${path} → ${r.status}`);
|
||||||
|
// Don't treat 401 on /api/login as session expiry — it's just bad credentials
|
||||||
|
if (r.status === 401 && !path.includes('/api/login')) {
|
||||||
|
authToken = null;
|
||||||
|
await chrome.storage.local.remove('token');
|
||||||
|
throw new Error('Session expired');
|
||||||
|
}
|
||||||
|
return r.json();
|
||||||
|
} catch (e) {
|
||||||
|
clearTimeout(timeout);
|
||||||
|
if (e.name === 'AbortError') throw new Error('Request timed out — check server URL');
|
||||||
|
console.error(`[DW] ${method} ${path} failed:`, e);
|
||||||
|
throw e;
|
||||||
}
|
}
|
||||||
return r.json();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
function showStatus(msg, type) {
|
function showStatus(msg, type) {
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue