Enhance JSON message handling with improved error reporting and response writing
This commit is contained in:
parent
6450fd49e8
commit
91091b8dc2
2 changed files with 28 additions and 2 deletions
|
|
@ -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"
|
||||
|
|
@ -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())
|
||||
|
|
@ -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()
|
||||
|
|
@ -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()
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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)
|
||||
|
|
@ -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())
|
||||
}
|
||||
|
||||
|
|
@ -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")
|
||||
}
|
||||
|
||||
|
|
@ -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")
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue