reduced panic

This commit is contained in:
syumai 2024-04-21 03:40:44 +09:00
parent ed6b2493ff
commit 0713696d4a

View File

@ -20,18 +20,21 @@ var (
func init() { func init() {
var handleRequestCallback js.Func var handleRequestCallback js.Func
handleRequestCallback = js.FuncOf(func(this js.Value, args []js.Value) any { 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] reqObj := args[0]
var cb js.Func var cb js.Func
cb = js.FuncOf(func(_ js.Value, pArgs []js.Value) any { cb = js.FuncOf(func(_ js.Value, pArgs []js.Value) any {
defer cb.Release() defer cb.Release()
resolve := pArgs[0] resolve := pArgs[0]
reject := pArgs[1]
go func() { go func() {
if len(args) > 1 {
reject.Invoke(jsutil.Errorf("too many args given to handleRequest: %d", len(args)))
return
}
res, err := handleRequest(reqObj) res, err := handleRequest(reqObj)
if err != nil { if err != nil {
panic(err) reject.Invoke(jsutil.Error(err.Error()))
return
} }
resolve.Invoke(res) resolve.Invoke(res)
}() }()
@ -58,7 +61,7 @@ func handleRequest(reqObj js.Value) (js.Value, error) {
} }
req, err := jshttp.ToRequest(reqObj) req, err := jshttp.ToRequest(reqObj)
if err != nil { if err != nil {
panic(err) return js.Value{}, err
} }
ctx := runtimecontext.New(context.Background(), reqObj) ctx := runtimecontext.New(context.Background(), reqObj)
req = req.WithContext(ctx) req = req.WithContext(ctx)