Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix handler error handlings #111

Merged
merged 5 commits into from
Apr 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 9 additions & 8 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 Expand Up @@ -90,7 +93,5 @@ func Serve(handler http.Handler) {
}
httpHandler = handler
ready()
select {
case <-closeCh:
}
<-closeCh
}
8 changes: 8 additions & 0 deletions internal/jsutil/jsutil.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,14 @@ func NewPromise(fn js.Func) js.Value {
return PromiseClass.New(fn)
}

func Error(msg string) js.Value {
return ErrorClass.New(msg)
}

func Errorf(format string, args ...any) js.Value {
return ErrorClass.New(fmt.Sprintf(format, args...))
}

// ArrayFrom calls Array.from to given argument and returns result Array.
func ArrayFrom(v js.Value) js.Value {
return ArrayClass.Call("from", v)
Expand Down
26 changes: 18 additions & 8 deletions internal/jsutil/stream.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,8 +134,7 @@ func (rs *readerToReadableStream) Pull(controller js.Value) error {
return nil
}
if err != nil {
jsErr := ErrorClass.New(err.Error())
controller.Call("error", jsErr)
controller.Call("error", Error(err.Error()))
if err := rs.reader.Close(); err != nil {
return err
}
Expand Down Expand Up @@ -170,7 +169,7 @@ func ConvertReaderToReadableStream(reader io.ReadCloser) js.Value {
go func() {
err := stream.Pull(controller)
if err != nil {
reject.Invoke(ErrorClass.New(err.Error()))
reject.Invoke(Error(err.Error()))
return
}
resolve.Invoke()
Expand All @@ -180,11 +179,22 @@ func ConvertReaderToReadableStream(reader io.ReadCloser) js.Value {
return NewPromise(cb)
}))
rsInit.Set("cancel", js.FuncOf(func(js.Value, []js.Value) any {
err := stream.Cancel()
if err != nil {
panic(err)
}
return js.Undefined()
var cb js.Func
cb = js.FuncOf(func(this js.Value, pArgs []js.Value) any {
defer cb.Release()
resolve := pArgs[0]
reject := pArgs[1]
go func() {
err := stream.Cancel()
if err != nil {
reject.Invoke(Error(err.Error()))
return
}
resolve.Invoke()
}()
return js.Undefined()
})
return NewPromise(cb)
}))
return ReadableStreamClass.New(rsInit)
}
Loading