From 9c9f137a8ada4a92c5a1a3a93b853279dd4b22da Mon Sep 17 00:00:00 2001 From: Max Date: Mon, 24 Feb 2025 10:44:15 +0800 Subject: [PATCH] Improve error handling and logging in assistant methods - Add nil checks for callbacks in message and object methods - Enhance error logging in plan subscription with more descriptive messages - Improve write method error handling with additional context logging - Add safeguards against potential nil function calls --- neo/assistant/object.go | 2 +- neo/assistant/plan.go | 10 +++++----- neo/message/message.go | 16 ++++++++++++++++ 3 files changed, 22 insertions(+), 6 deletions(-) diff --git a/neo/assistant/object.go b/neo/assistant/object.go index ae0c8021..2ab2095a 100644 --- a/neo/assistant/object.go +++ b/neo/assistant/object.go @@ -138,7 +138,7 @@ func jsCall(info *v8go.FunctionCallbackInfo) *v8go.Value { chatContext.ChatID = fmt.Sprintf("chat_%s", uuid.New().String()) // New chat id chatContext.Silent = true // Silent mode - var cb func(msg *chatMessage.Message) + var cb func(msg *chatMessage.Message) = nil if len(args) > 2 { // Parse the callback diff --git a/neo/assistant/plan.go b/neo/assistant/plan.go index 5d0b08bb..ee470970 100644 --- a/neo/assistant/plan.go +++ b/neo/assistant/plan.go @@ -64,29 +64,29 @@ func SubscribeFn(plan_id string, key string, value interface{}, source bool, met // Data plan, err := v8plan.GetPlan(plan_id) 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 } global, ok := plan.Data().(*GlobalVariables) if !ok { - color.Red("plan data is not a GlobalVariables") + color.Red("Subscribe Failed: plan data is not a GlobalVariables") return } if global.Assistant == nil { - color.Red("assistant is not set") + color.Red("Subscribe Failed: assistant is not set") return } if global.Assistant.Script == nil { - color.Red("script is not set") + color.Red("Subscribe Failed: script is not set") return } scriptCtx, err := global.Assistant.Script.NewContext(global.ChatContext.Sid, 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 } defer scriptCtx.Close() diff --git a/neo/message/message.go b/neo/message/message.go index 0236b547..02341acd 100644 --- a/neo/message/message.go +++ b/neo/message/message.go @@ -698,10 +698,16 @@ func (m *Message) Callback(fn interface{}) *Message { if fn != nil { switch v := fn.(type) { case func(msg *Message): + if v == nil { + break + } v(m) break case func(): + if v == nil { + break + } v() break @@ -717,8 +723,18 @@ func (m *Message) Callback(fn interface{}) *Message { func (m *Message) Write(w gin.ResponseWriter) bool { defer func() { 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" color.Red(message, r) + + // Print the message + raw, _ := jsoniter.MarshalToString(m) + color.White("Message:\n %s", raw) } }()