From 33b28b50f48d3632810fe6cdf443a614b71bce5a Mon Sep 17 00:00:00 2001 From: Max Date: Thu, 19 Dec 2024 17:15:44 +0800 Subject: [PATCH] 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. --- neo/api.go | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/neo/api.go b/neo/api.go index e17aa8f9..163b3fad 100644 --- a/neo/api.go +++ b/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 }