Return error when calling function does not exist

This commit is contained in:
George Suntres
2026-03-31 11:10:18 -04:00
parent edd7537811
commit 5e1a2fe3ff

View File

@@ -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 = `