Fix proxy/projects pages: error handling when API unavailable

- proxy: .catch() shows 'service not ready' instead of stuck loading
- projects: shows 'engine not ready' on API error
- Both: show 'no items' placeholder instead of blank
This commit is contained in:
qinglong
2026-06-11 12:47:39 +08:00
parent a1dd401efc
commit 885929f3ff
2 changed files with 5 additions and 3 deletions
+1 -1
View File
@@ -38,7 +38,7 @@
var projects=[]; var projects=[];
function showTab(e,t){document.querySelectorAll("#tab-running,#tab-add,#tab-git").forEach(el=>el.style.display="none");document.getElementById("tab-"+t).style.display="block";document.querySelectorAll(".tab").forEach(el=>el.classList.remove("active"));e.target.classList.add("active")} function showTab(e,t){document.querySelectorAll("#tab-running,#tab-add,#tab-git").forEach(el=>el.style.display="none");document.getElementById("tab-"+t).style.display="block";document.querySelectorAll(".tab").forEach(el=>el.classList.remove("active"));e.target.classList.add("active")}
async function refresh(){ async function refresh(){
try{var r=await fetch("/SenSu/api/projects");var d=await r.json();projects=d.projects;var h="";for(var p of projects){var cls=p.status==="running"?"status-running":p.status==="error"?"status-error":"status-stopped";h+='<div class="mgmt-card"><div class="status-dot '+cls+'"></div><div class="info"><b>'+p.name+'</b><br><small>'+p.status+" PID:"+(p.pid||"-")+" :"+(p.port||"-")+" "+(p.uptime?p.uptime+"s":"")+(p.proxy?" → "+p.proxy:"")+'</small></div><div class="actions"><button class="btn btn-sm btn-danger" onclick="stopP(this,'+JSON.stringify(p.name)+')">停止</button><button class="btn btn-sm btn-outlined" onclick="logsP(this,'+JSON.stringify(p.name)+')">日志</button><button class="btn btn-sm btn-text" onclick="cmdP(this,'+JSON.stringify(p.name)+')">命令</button></div></div><div class="log-box" id="logs-'+p.name+'"></div>'}document.getElementById("project-list").innerHTML=h||"暂无项目"}catch(e){console.error(e)} try{var r=await fetch("/SenSu/api/projects");var d=await r.json();projects=d.projects;var h="";for(var p of projects){var cls=p.status==="running"?"status-running":p.status==="error"?"status-error":"status-stopped";h+='<div class="mgmt-card"><div class="status-dot '+cls+'"></div><div class="info"><b>'+p.name+'</b><br><small>'+p.status+" PID:"+(p.pid||"-")+" :"+(p.port||"-")+" "+(p.uptime?p.uptime+"s":"")+(p.proxy?" → "+p.proxy:"")+'</small></div><div class="actions"><button class="btn btn-sm btn-danger" onclick="stopP(this,'+JSON.stringify(p.name)+')">停止</button><button class="btn btn-sm btn-outlined" onclick="logsP(this,'+JSON.stringify(p.name)+')">日志</button><button class="btn btn-sm btn-text" onclick="cmdP(this,'+JSON.stringify(p.name)+')">命令</button></div></div><div class="log-box" id="logs-'+p.name+'"></div>'}document.getElementById("project-list").innerHTML=h||"<div style=\"color:var(--text-dim);text-align:center;padding:40px\">暂无项目</div>"}catch(e){document.getElementById("project-list").innerHTML="<div style=\"color:var(--error);text-align:center;padding:40px\">引擎未就绪 (API 错误)</div>"}
} }
async function addProject(){ async function addProject(){
var n=document.getElementById("proj-name").value,cmd=document.getElementById("proj-cmd").value; var n=document.getElementById("proj-name").value,cmd=document.getElementById("proj-cmd").value;
+4 -2
View File
@@ -14,12 +14,14 @@
<script> <script>
function refresh(){ function refresh(){
fetch("/SenSu/api/proxy").then(r=>r.json()).then(d=>{ fetch("/SenSu/api/proxy").then(r=>{if(!r.ok)throw new Error(r.status);return r.json()}).then(d=>{
var h=""; var h="";
for(var p of d.proxies||[]){ for(var p of d.proxies||[]){
h+='<div class="mgmt-card"><div class="status-dot status-running"></div><div class="info"><b>'+p.path+'</b><br><small>→ '+p.target+(p.description?" ("+p.description+")":"")+(p.external?" 🌐":"")+'</small></div><div class="actions"><button class="btn btn-sm btn-danger" onclick="delP(\"'+p.path+'\")">删除</button></div></div>'; h+='<div class="mgmt-card"><div class="status-dot status-running"></div><div class="info"><b>'+p.path+'</b><br><small>→ '+p.target+(p.description?" ("+p.description+")":"")+(p.external?" 🌐":"")+'</small></div><div class="actions"><button class="btn btn-sm btn-danger" onclick="delP(\"'+p.path+'\")">删除</button></div></div>';
} }
document.getElementById("proxy-list").innerHTML=h||"暂无代理" document.getElementById("proxy-list").innerHTML=h||"<div style=\"color:var(--text-dim);text-align:center;padding:40px\">暂无代理配置</div>"
}).catch(function(e){
document.getElementById("proxy-list").innerHTML="<div style=\"color:var(--error);text-align:center;padding:40px\">代理服务未就绪 (API 返回 "+e.message+")</div>"
}) })
} }
function addP(){ function addP(){