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
+14 -4
View File
@@ -58,7 +58,10 @@ func (h *GenerateHandler) Generate(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()
}
@@ -74,7 +77,14 @@ func (h *GenerateHandler) Generate(c *gin.Context) {
}
// Save analysis to DB
resultJSON, _ := json.Marshal(pr)
h.db.Exec(`INSERT INTO analyses (repo_id, type, base_ref, head_ref, result) VALUES (?, 'pr_description', ?, ?, ?)`,
id, req.Base, req.Head, string(resultJSON))
resultJSON, err := json.Marshal(pr)
if err != nil {
sendEvent("error", map[string]interface{}{"message": "marshal result: " + err.Error()})
return
}
if _, err := h.db.Exec(`INSERT INTO analyses (repo_id, type, base_ref, head_ref, result) VALUES (?, 'pr_description', ?, ?, ?)`,
id, req.Base, req.Head, string(resultJSON)); err != nil {
sendEvent("error", map[string]interface{}{"message": "save analysis: " + err.Error()})
return
}
}