Return error when calling function does not exist
This commit is contained in:
20
persist.go
20
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.
|
// 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) {
|
func Init(props *InitProps) {
|
||||||
var persistProps PersistProps
|
var persistProps PersistProps
|
||||||
commons.CopyToStruct(props, &persistProps)
|
commons.StructToStruct(props, &persistProps)
|
||||||
persist = NewPersist(&persistProps)
|
persist = NewPersist(&persistProps)
|
||||||
|
|
||||||
sysDb = props.MongoSysDb
|
sysDb = props.MongoSysDb
|
||||||
@@ -125,7 +125,9 @@ func Orig() *Persist {
|
|||||||
return persistOriginal
|
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)
|
v := reflect.ValueOf(p)
|
||||||
|
|
||||||
// 1. Make sure it's the right type
|
// 1. Make sure it's the right type
|
||||||
@@ -138,15 +140,21 @@ func Call(p any, name string, args... any) []reflect.Value {
|
|||||||
|
|
||||||
// 3. Validate
|
// 3. Validate
|
||||||
if !fn.IsValid() {
|
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 {
|
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() {
|
if fn.IsNil() {
|
||||||
log.Fatalf("%s is nil", name)
|
log.Printf("%s is nil", name)
|
||||||
|
|
||||||
|
return nil, ErrCall
|
||||||
}
|
}
|
||||||
|
|
||||||
// 4. Prepare arguments
|
// 4. Prepare arguments
|
||||||
@@ -157,7 +165,7 @@ func Call(p any, name string, args... any) []reflect.Value {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// 5. Call and return results
|
// 5. Call and return results
|
||||||
return fn.Call(vArgs)
|
return fn.Call(vArgs), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
const SAVE_USER_DATA = `
|
const SAVE_USER_DATA = `
|
||||||
|
|||||||
Reference in New Issue
Block a user