fix: API fetch 에러 핸들링 강화
- fetch redirect:manual로 리다이렉트 방지 - 401/리다이렉트 시 로그인 페이지로 이동 - JSON 파싱 실패 시 빈 배열 반환 (빈 목록 방지) Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
+12
-2
@@ -121,9 +121,19 @@ tr:hover td{background:rgba(255,255,255,.02)}
|
||||
<div class="toast" id="toast"></div>
|
||||
<script>
|
||||
function api(method, url, data) {
|
||||
const opts = { method, headers: { 'Content-Type': 'application/json' } };
|
||||
const opts = { method, headers: { 'Content-Type': 'application/json' }, redirect: 'manual' };
|
||||
if (data) opts.body = JSON.stringify(data);
|
||||
return fetch(url, opts).then(r => r.json());
|
||||
return fetch(url, opts).then(r => {
|
||||
if (r.status === 401 || r.type === 'opaqueredirect' || r.status === 0) {
|
||||
window.location.href = '/login?redirect=' + encodeURIComponent(window.location.pathname);
|
||||
return [];
|
||||
}
|
||||
if (!r.ok) return r.json().then(j => { throw new Error(j.error || 'API Error'); });
|
||||
return r.json();
|
||||
}).catch(err => {
|
||||
console.error('API Error:', err);
|
||||
return [];
|
||||
});
|
||||
}
|
||||
function toast(msg, type = 'success') {
|
||||
const el = document.getElementById('toast');
|
||||
|
||||
Reference in New Issue
Block a user