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
This commit is contained in:
parent
df99b9a228
commit
9c9f137a8a
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.Silent = true // Silent mode
|
||||
|
||||
var cb func(msg *chatMessage.Message)
|
||||
var cb func(msg *chatMessage.Message) = nil
|
||||
if len(args) > 2 {
|
||||
|
||||
// Parse the callback
|
||||
|
|
|
|||
|
|
@ -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()
|
||||
|
|
|
|||
|
|
@ -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)
|
||||
}
|
||||
}()
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue