Skip to content

Commit

Permalink
Merge pull request #780 from trheyi/main
Browse files Browse the repository at this point in the history
Enhance JSON message handling with improved error reporting and respo…
  • Loading branch information
trheyi authored Nov 7, 2024
2 parents 13e94ae + 91091b8 commit 1d22041
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 2 deletions.
22 changes: 20 additions & 2 deletions neo/message/json.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package message
import (
"strings"

"github.com/fatih/color"
"github.com/gin-gonic/gin"
jsoniter "github.com/json-iterator/go"
"github.com/yaoapp/gou/helper"
Expand Down Expand Up @@ -197,10 +198,16 @@ func (json *JSON) Write(w gin.ResponseWriter) bool {

defer func() {
if r := recover(); r != nil {
log.Error("Write JSON Message Error: %s", r)
message := "Write Response Exception: (if clinet close the connection, it's normal) \n %s\n\n"
color.Red(message, r)
}
}()

if json.Error != "" {
json.writeError(w, json.Error)
return false
}

data, err := jsoniter.Marshal(json.Message)
if err != nil {
log.Error("%s", err.Error())
Expand All @@ -212,7 +219,7 @@ func (json *JSON) Write(w gin.ResponseWriter) bool {

_, err = w.Write(data)
if err != nil {
log.Error("%s", err.Error())
color.Red("Write JSON Message Error: %s", err.Error())
return false
}
w.Flush()
Expand All @@ -223,3 +230,14 @@ func (json *JSON) Write(w gin.ResponseWriter) bool {
func (json *JSON) Append(content []byte) []byte {
return append(content, []byte(json.Message.Text)...)
}

func (json *JSON) writeError(w gin.ResponseWriter, message string) {
data := []byte(`{"text":"` + strings.Trim(message, "\"") + `"}`)
data = append([]byte("data: "), data...)
data = append(data, []byte("\n\n")...)
_, err := w.Write(data)
if err != nil {
color.Red("Write JSON Message Error: %s", message)
}
w.Flush()
}
8 changes: 8 additions & 0 deletions neo/neo.go
Original file line number Diff line number Diff line change
Expand Up @@ -289,6 +289,11 @@ func (neo *DSL) send(ctx command.Context, msg *message.JSON, messages []map[stri

w := c.Writer

if msg.Error != "" {
msg.Write(w)
return nil
}

// Directly write the message
if neo.Write == "" {
ok := msg.Write(c.Writer)
Expand All @@ -303,6 +308,7 @@ func (neo *DSL) send(ctx command.Context, msg *message.JSON, messages []map[stri
p, err := process.Of(neo.Write, args...)
if err != nil {
msg.Write(w)
color.Red("Neo custom write error: %s", err.Error())
return fmt.Errorf("Stream write error: %s", err.Error())
}

Expand All @@ -316,6 +322,7 @@ func (neo *DSL) send(ctx command.Context, msg *message.JSON, messages []map[stri

res := p.Value()
if res == nil {
color.Red("Neo custom write return null")
return fmt.Errorf("Neo custom write return null")
}

Expand All @@ -330,6 +337,7 @@ func (neo *DSL) send(ctx command.Context, msg *message.JSON, messages []map[stri
return nil
}

color.Red("Neo custom write should return an array of response")
return fmt.Errorf("Neo should return an array of response")
}

Expand Down

0 comments on commit 1d22041

Please sign in to comment.