Enhance SSE error handling in handleGenerateTitle method

- Added support for Server-Sent Events (SSE) in the handleGenerateTitle function to send error messages in the appropriate SSE format when invalid request bodies are encountered.
- Updated response headers for SSE requests to ensure correct content type and connection settings, improving the API's handling of real-time updates and error reporting.
This commit is contained in:
Max 2024-12-19 17:15:44 +08:00
parent c1e95374f2
commit 33b28b50f4

View file

@ -609,6 +609,15 @@ func (neo *DSL) handleGenerateTitle(c *gin.Context) {
Content string `json:"content"`
}
if err := c.BindJSON(&body); err != nil {
// For SSE requests, send error message in SSE format
if strings.Contains(c.GetHeader("Accept"), "text/event-stream") {
c.Header("Content-Type", "text/event-stream;charset=utf-8")
c.Header("Cache-Control", "no-cache")
c.Header("Connection", "keep-alive")
msg := message.New().Error("invalid request body").Done()
msg.Write(c.Writer)
return
}
c.JSON(400, gin.H{"message": "invalid request body", "code": 400})
return
}
@ -620,6 +629,14 @@ func (neo *DSL) handleGenerateTitle(c *gin.Context) {
sid: c.GetString("__sid"),
content: content,
}
// For SSE requests, set headers before validation
if strings.Contains(c.GetHeader("Accept"), "text/event-stream") {
c.Header("Content-Type", "text/event-stream;charset=utf-8")
c.Header("Cache-Control", "no-cache")
c.Header("Connection", "keep-alive")
}
if !resp.validate() {
return
}