1
0
mirror of https://github.com/pocketbase/pocketbase.git synced 2025-11-25 07:34:10 +02:00

updated jsvm errors handling

This commit is contained in:
Gani Georgiev
2023-07-18 12:33:18 +03:00
parent 0110869c89
commit 71a70bac9d
4 changed files with 65 additions and 22 deletions

View File

@@ -77,15 +77,12 @@ func hooksBinds(app core.App, loader *goja.Runtime, executors *vmsPool) {
// check for returned error or false
if res != nil {
exported := res.Export()
if exported != nil {
switch v := exported.(type) {
case error:
return v
case bool:
if !v {
return hook.StopPropagation
}
switch v := res.Export().(type) {
case error:
return v
case bool:
if !v {
return hook.StopPropagation
}
}
}
@@ -156,8 +153,16 @@ func routerBinds(app core.App, loader *goja.Runtime, executors *vmsPool) {
e.Router.Add(strings.ToUpper(method), path, func(c echo.Context) error {
return executors.run(func(executor *goja.Runtime) error {
executor.Set("__args", []any{c})
_, err := executor.RunProgram(pr)
res, err := executor.RunProgram(pr)
executor.Set("__args", goja.Undefined())
// check for returned error
if res != nil {
if v, ok := res.Export().(error); ok {
return v
}
}
return err
})
}, wrappedMiddlewares...)
@@ -207,9 +212,17 @@ func wrapMiddlewares(executors *vmsPool, rawMiddlewares ...goja.Value) ([]echo.M
return executors.run(func(executor *goja.Runtime) error {
executor.Set("__args", []any{next})
executor.Set("__args2", []any{c})
_, err := executor.RunProgram(pr)
res, err := executor.RunProgram(pr)
executor.Set("__args", goja.Undefined())
executor.Set("__args2", goja.Undefined())
// check for returned error
if res != nil {
if v, ok := res.Export().(error); ok {
return v
}
}
return err
})
}