Skip to content

Commit

Permalink
net/http: improve js fetch errors
Browse files Browse the repository at this point in the history
The Error type used in failed fetch invocations contain more
information than we're currently extracting. Optimistically
look for the cause field for extra context for errors.

Change-Id: I6c8e4b230a21ec684af2107707aa605fc2148fcf
Reviewed-on: https://go-review.googlesource.com/c/go/+/463978
TryBot-Result: Gopher Robot <[email protected]>
Reviewed-by: Dmitri Shuralyov <[email protected]>
Reviewed-by: David Chase <[email protected]>
Reviewed-by: Evan Phoenix <[email protected]>
Run-TryBot: Johan Brandhorst-Satzkorn <[email protected]>
Reviewed-by: Dmitri Shuralyov <[email protected]>
Auto-Submit: Johan Brandhorst-Satzkorn <[email protected]>
  • Loading branch information
johanbrandhorst authored and gopherbot committed Feb 9, 2023
1 parent f185599 commit a649199
Showing 1 changed file with 16 additions and 1 deletion.
17 changes: 16 additions & 1 deletion src/net/http/roundtrip_js.go
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,22 @@ func (t *Transport) RoundTrip(req *Request) (*Response, error) {
failure = js.FuncOf(func(this js.Value, args []js.Value) any {
success.Release()
failure.Release()
errCh <- fmt.Errorf("net/http: fetch() failed: %s", args[0].Get("message").String())
err := args[0]
// The error is a JS Error type
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error
// We can use the toString() method to get a string representation of the error.
errMsg := err.Call("toString").String()
// Errors can optionally contain a cause.
if cause := err.Get("cause"); !cause.IsUndefined() {
// The exact type of the cause is not defined,
// but if it's another error, we can call toString() on it too.
if !cause.Get("toString").IsUndefined() {
errMsg += ": " + cause.Call("toString").String()
} else if cause.Type() == js.TypeString {
errMsg += ": " + cause.String()
}
}
errCh <- fmt.Errorf("net/http: fetch() failed: %s", errMsg)
return nil
})

Expand Down

0 comments on commit a649199

Please sign in to comment.