Skip to content

Commit

Permalink
reduced panic
Browse files Browse the repository at this point in the history
  • Loading branch information
syumai committed Apr 20, 2024
1 parent ed6b249 commit 0713696
Showing 1 changed file with 8 additions and 5 deletions.
13 changes: 8 additions & 5 deletions handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,18 +20,21 @@ var (
func init() {
var handleRequestCallback js.Func
handleRequestCallback = js.FuncOf(func(this js.Value, args []js.Value) any {
if len(args) > 1 {
panic(fmt.Errorf("too many args given to handleRequest: %d", len(args)))
}
reqObj := args[0]
var cb js.Func
cb = js.FuncOf(func(_ js.Value, pArgs []js.Value) any {
defer cb.Release()
resolve := pArgs[0]
reject := pArgs[1]
go func() {
if len(args) > 1 {
reject.Invoke(jsutil.Errorf("too many args given to handleRequest: %d", len(args)))
return
}
res, err := handleRequest(reqObj)
if err != nil {
panic(err)
reject.Invoke(jsutil.Error(err.Error()))
return
}
resolve.Invoke(res)
}()
Expand All @@ -58,7 +61,7 @@ func handleRequest(reqObj js.Value) (js.Value, error) {
}
req, err := jshttp.ToRequest(reqObj)
if err != nil {
panic(err)
return js.Value{}, err
}
ctx := runtimecontext.New(context.Background(), reqObj)
req = req.WithContext(ctx)
Expand Down

0 comments on commit 0713696

Please sign in to comment.