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:
parent
c1e95374f2
commit
33b28b50f4
1 changed files with 17 additions and 0 deletions
17
neo/api.go
17
neo/api.go
|
|
@ -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
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue