From 5e1a2fe3ff1e33f5c816e34d08dd8c1f06e5f5bf Mon Sep 17 00:00:00 2001 From: George Suntres Date: Tue, 31 Mar 2026 11:10:18 -0400 Subject: [PATCH] Return error when calling function does not exist --- persist.go | 20 ++++++++++++++------ 1 file changed, 14 insertions(+), 6 deletions(-) diff --git a/persist.go b/persist.go index c3b345f..cea748d 100644 --- a/persist.go +++ b/persist.go @@ -58,7 +58,7 @@ type InitProps struct { // Init runs on boot, reads system_collections from structful and registers them. No changes after boot are going to be applied. func Init(props *InitProps) { var persistProps PersistProps - commons.CopyToStruct(props, &persistProps) + commons.StructToStruct(props, &persistProps) persist = NewPersist(&persistProps) sysDb = props.MongoSysDb @@ -125,7 +125,9 @@ func Orig() *Persist { return persistOriginal } -func Call(p any, name string, args... any) []reflect.Value { +var ErrCall error = errors.New("failed to call") + +func Call(p any, name string, args... any) ([]reflect.Value, error) { v := reflect.ValueOf(p) // 1. Make sure it's the right type @@ -138,15 +140,21 @@ func Call(p any, name string, args... any) []reflect.Value { // 3. Validate if !fn.IsValid() { - log.Fatalf("Failed to find %s field", name) + log.Printf("Failed to find %s field", name) + + return nil, ErrCall } if fn.Kind() != reflect.Func { - log.Fatalf("%s is not a function", name) + log.Printf("%s is not a function", name) + + return nil, ErrCall } if fn.IsNil() { - log.Fatalf("%s is nil", name) + log.Printf("%s is nil", name) + + return nil, ErrCall } // 4. Prepare arguments @@ -157,7 +165,7 @@ func Call(p any, name string, args... any) []reflect.Value { } // 5. Call and return results - return fn.Call(vArgs) + return fn.Call(vArgs), nil } const SAVE_USER_DATA = `