Merge pull request #780 from trheyi/main
Enhance JSON message handling with improved error reporting and respo…
This commit is contained in:
commit
1d22041dac
2 changed files with 28 additions and 2 deletions
|
|
@ -3,6 +3,7 @@ package message
|
||||||
import (
|
import (
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
|
"github.com/fatih/color"
|
||||||
"github.com/gin-gonic/gin"
|
"github.com/gin-gonic/gin"
|
||||||
jsoniter "github.com/json-iterator/go"
|
jsoniter "github.com/json-iterator/go"
|
||||||
"github.com/yaoapp/gou/helper"
|
"github.com/yaoapp/gou/helper"
|
||||||
|
|
@ -197,10 +198,16 @@ func (json *JSON) Write(w gin.ResponseWriter) bool {
|
||||||
|
|
||||||
defer func() {
|
defer func() {
|
||||||
if r := recover(); r != nil {
|
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)
|
data, err := jsoniter.Marshal(json.Message)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Error("%s", err.Error())
|
log.Error("%s", err.Error())
|
||||||
|
|
@ -212,7 +219,7 @@ func (json *JSON) Write(w gin.ResponseWriter) bool {
|
||||||
|
|
||||||
_, err = w.Write(data)
|
_, err = w.Write(data)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Error("%s", err.Error())
|
color.Red("Write JSON Message Error: %s", err.Error())
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
w.Flush()
|
w.Flush()
|
||||||
|
|
@ -223,3 +230,14 @@ func (json *JSON) Write(w gin.ResponseWriter) bool {
|
||||||
func (json *JSON) Append(content []byte) []byte {
|
func (json *JSON) Append(content []byte) []byte {
|
||||||
return append(content, []byte(json.Message.Text)...)
|
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
|
w := c.Writer
|
||||||
|
|
||||||
|
if msg.Error != "" {
|
||||||
|
msg.Write(w)
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
// Directly write the message
|
// Directly write the message
|
||||||
if neo.Write == "" {
|
if neo.Write == "" {
|
||||||
ok := msg.Write(c.Writer)
|
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...)
|
p, err := process.Of(neo.Write, args...)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
msg.Write(w)
|
msg.Write(w)
|
||||||
|
color.Red("Neo custom write error: %s", err.Error())
|
||||||
return fmt.Errorf("Stream 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()
|
res := p.Value()
|
||||||
if res == nil {
|
if res == nil {
|
||||||
|
color.Red("Neo custom write return null")
|
||||||
return fmt.Errorf("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
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
color.Red("Neo custom write should return an array of response")
|
||||||
return fmt.Errorf("Neo should return an array of response")
|
return fmt.Errorf("Neo should return an array of response")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue