fix: 在 handler 中检查错误,不再静默丢弃

This commit is contained in:
2026-06-20 00:06:00 +08:00
parent 4eaf89219d
commit 72c7d8e7b3
3 changed files with 88 additions and 21 deletions
+45 -9
View File
@@ -43,6 +43,10 @@ func (h *ReposHandler) ListRepos(c *gin.Context) {
})
}
}
if err := rows.Err(); err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
return
}
if repos == nil {
repos = []gin.H{}
}
@@ -61,9 +65,18 @@ func (h *ReposHandler) DeleteRepo(c *gin.Context) {
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
return
}
os.RemoveAll(localPath)
h.db.Exec(`DELETE FROM analyses WHERE repo_id = ?`, id)
h.db.Exec(`DELETE FROM repositories WHERE id = ?`, id)
if err := os.RemoveAll(localPath); err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": "remove repo dir: " + err.Error()})
return
}
if _, err := h.db.Exec(`DELETE FROM analyses WHERE repo_id = ?`, id); err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": "delete analyses: " + err.Error()})
return
}
if _, err := h.db.Exec(`DELETE FROM repositories WHERE id = ?`, id); err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": "delete repository: " + err.Error()})
return
}
c.JSON(http.StatusOK, gin.H{"ok": true})
}
@@ -80,17 +93,33 @@ func (h *ReposHandler) CleanupRepos(c *gin.Context) {
}
defer rows.Close()
var cleaned []int64
var errs []string
for rows.Next() {
var id int64
var localPath string
if rows.Scan(&id, &localPath) == nil {
os.RemoveAll(localPath)
h.db.Exec(`DELETE FROM analyses WHERE repo_id = ?`, id)
h.db.Exec(`DELETE FROM repositories WHERE id = ?`, id)
if err := os.RemoveAll(localPath); err != nil {
errs = append(errs, fmt.Sprintf("remove %d: %s", id, err.Error()))
continue
}
if _, err := h.db.Exec(`DELETE FROM analyses WHERE repo_id = ?`, id); err != nil {
errs = append(errs, fmt.Sprintf("delete analyses %d: %s", id, err.Error()))
}
if _, err := h.db.Exec(`DELETE FROM repositories WHERE id = ?`, id); err != nil {
errs = append(errs, fmt.Sprintf("delete repo %d: %s", id, err.Error()))
}
cleaned = append(cleaned, id)
}
}
c.JSON(http.StatusOK, gin.H{"cleaned": cleaned})
if err := rows.Err(); err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
return
}
result := gin.H{"cleaned": cleaned}
if len(errs) > 0 {
result["errors"] = errs
}
c.JSON(http.StatusOK, result)
}
// CloneRepo handles POST /api/repos with SSE progress events.
@@ -119,7 +148,10 @@ func (h *ReposHandler) CloneRepo(c *gin.Context) {
}
sendEvent := func(event string, data interface{}) {
jsonData, _ := json.Marshal(data)
jsonData, err := json.Marshal(data)
if err != nil {
jsonData = []byte(`{"error":"failed to marshal event data"}`)
}
fmt.Fprintf(c.Writer, "event: %s\ndata: %s\n\n", event, jsonData)
flusher.Flush()
}
@@ -155,7 +187,11 @@ func (h *ReposHandler) CloneRepo(c *gin.Context) {
sendEvent("error", map[string]interface{}{"message": "save to db: " + err.Error()})
return
}
repoID, _ := res.LastInsertId()
repoID, err := res.LastInsertId()
if err != nil {
sendEvent("error", map[string]interface{}{"message": "get repo id: " + err.Error()})
return
}
sendEvent("complete", map[string]interface{}{
"repo_id": repoID,