mirror of
https://github.com/syumai/workers.git
synced 2025-03-11 09:49:12 +00:00
add TryCatch jsutil and use it in socket dialer
This commit is contained in:
parent
68b61bf3d5
commit
cfee7c5d4e
@ -3,6 +3,7 @@ package sockets
|
|||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"net"
|
"net"
|
||||||
|
"syscall/js"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/syumai/workers/cloudflare/internal/cfruntimecontext"
|
"github.com/syumai/workers/cloudflare/internal/cfruntimecontext"
|
||||||
@ -42,7 +43,12 @@ func Connect(ctx context.Context, addr string, opts *SocketOptions) (net.Conn, e
|
|||||||
optionsObj.Set("secureTransport", string(opts.SecureTransport))
|
optionsObj.Set("secureTransport", string(opts.SecureTransport))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
sockVal := connect.Invoke(addr, optionsObj)
|
sockVal, err := jsutil.TryCatch(js.FuncOf(func(_ js.Value, args []js.Value) any {
|
||||||
|
return connect.Invoke(addr, optionsObj)
|
||||||
|
}))
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
deadline := time.Now().Add(defaultDeadline)
|
deadline := time.Now().Add(defaultDeadline)
|
||||||
return newSocket(ctx, sockVal, deadline, deadline), nil
|
return newSocket(ctx, sockVal, deadline, deadline), nil
|
||||||
}
|
}
|
||||||
|
@ -5,6 +5,18 @@ const go = new Go();
|
|||||||
|
|
||||||
let mod;
|
let mod;
|
||||||
|
|
||||||
|
globalThis.tryCatch = (fn) => {
|
||||||
|
try {
|
||||||
|
return {
|
||||||
|
result: fn(),
|
||||||
|
};
|
||||||
|
} catch(e) {
|
||||||
|
return {
|
||||||
|
error: e,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
export function init(m) {
|
export function init(m) {
|
||||||
mod = m;
|
mod = m;
|
||||||
}
|
}
|
||||||
|
16
internal/jsutil/trycatch.go
Normal file
16
internal/jsutil/trycatch.go
Normal file
@ -0,0 +1,16 @@
|
|||||||
|
package jsutil
|
||||||
|
|
||||||
|
import (
|
||||||
|
"errors"
|
||||||
|
"syscall/js"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TryCatch(fn js.Func) (js.Value, error) {
|
||||||
|
fnResultVal := js.Global().Call("tryCatch", fn)
|
||||||
|
resultVal := fnResultVal.Get("result")
|
||||||
|
errorVal := fnResultVal.Get("error")
|
||||||
|
if !errorVal.IsUndefined() {
|
||||||
|
return js.Value{}, errors.New(errorVal.String())
|
||||||
|
}
|
||||||
|
return resultVal, nil
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user