Merge pull request #874 from trheyi/main
Improve error handling and logging in assistant methods
This commit is contained in:
commit
71f71570b3
3 changed files with 22 additions and 6 deletions
|
|
@ -138,7 +138,7 @@ func jsCall(info *v8go.FunctionCallbackInfo) *v8go.Value {
|
||||||
chatContext.ChatID = fmt.Sprintf("chat_%s", uuid.New().String()) // New chat id
|
chatContext.ChatID = fmt.Sprintf("chat_%s", uuid.New().String()) // New chat id
|
||||||
chatContext.Silent = true // Silent mode
|
chatContext.Silent = true // Silent mode
|
||||||
|
|
||||||
var cb func(msg *chatMessage.Message)
|
var cb func(msg *chatMessage.Message) = nil
|
||||||
if len(args) > 2 {
|
if len(args) > 2 {
|
||||||
|
|
||||||
// Parse the callback
|
// Parse the callback
|
||||||
|
|
|
||||||
|
|
@ -64,29 +64,29 @@ func SubscribeFn(plan_id string, key string, value interface{}, source bool, met
|
||||||
// Data
|
// Data
|
||||||
plan, err := v8plan.GetPlan(plan_id)
|
plan, err := v8plan.GetPlan(plan_id)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
color.Red("Failed to get the plan: %s", err.Error())
|
color.Red("Subscribe Failed to get the plan: %s", err.Error())
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
global, ok := plan.Data().(*GlobalVariables)
|
global, ok := plan.Data().(*GlobalVariables)
|
||||||
if !ok {
|
if !ok {
|
||||||
color.Red("plan data is not a GlobalVariables")
|
color.Red("Subscribe Failed: plan data is not a GlobalVariables")
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
if global.Assistant == nil {
|
if global.Assistant == nil {
|
||||||
color.Red("assistant is not set")
|
color.Red("Subscribe Failed: assistant is not set")
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
if global.Assistant.Script == nil {
|
if global.Assistant.Script == nil {
|
||||||
color.Red("script is not set")
|
color.Red("Subscribe Failed: script is not set")
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
scriptCtx, err := global.Assistant.Script.NewContext(global.ChatContext.Sid, nil)
|
scriptCtx, err := global.Assistant.Script.NewContext(global.ChatContext.Sid, nil)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
color.Red("Failed to create the script context: %s", err.Error())
|
color.Red("Subscribe Failed: Failed to create the script context: %s", err.Error())
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
defer scriptCtx.Close()
|
defer scriptCtx.Close()
|
||||||
|
|
|
||||||
|
|
@ -698,10 +698,16 @@ func (m *Message) Callback(fn interface{}) *Message {
|
||||||
if fn != nil {
|
if fn != nil {
|
||||||
switch v := fn.(type) {
|
switch v := fn.(type) {
|
||||||
case func(msg *Message):
|
case func(msg *Message):
|
||||||
|
if v == nil {
|
||||||
|
break
|
||||||
|
}
|
||||||
v(m)
|
v(m)
|
||||||
break
|
break
|
||||||
|
|
||||||
case func():
|
case func():
|
||||||
|
if v == nil {
|
||||||
|
break
|
||||||
|
}
|
||||||
v()
|
v()
|
||||||
break
|
break
|
||||||
|
|
||||||
|
|
@ -717,8 +723,18 @@ func (m *Message) Callback(fn interface{}) *Message {
|
||||||
func (m *Message) Write(w gin.ResponseWriter) bool {
|
func (m *Message) Write(w gin.ResponseWriter) bool {
|
||||||
defer func() {
|
defer func() {
|
||||||
if r := recover(); r != nil {
|
if r := recover(); r != nil {
|
||||||
|
|
||||||
|
// Ignore if done is true
|
||||||
|
if m.IsDone {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
message := "Write Response Exception: (if client close the connection, it's normal) \n %s\n\n"
|
message := "Write Response Exception: (if client close the connection, it's normal) \n %s\n\n"
|
||||||
color.Red(message, r)
|
color.Red(message, r)
|
||||||
|
|
||||||
|
// Print the message
|
||||||
|
raw, _ := jsoniter.MarshalToString(m)
|
||||||
|
color.White("Message:\n %s", raw)
|
||||||
}
|
}
|
||||||
}()
|
}()
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue