1
0
mirror of https://github.com/golang/go synced 2024-11-11 20:20:23 -07:00

net/http: improve js fetch errors

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 <gobot@golang.org>
Reviewed-by: Dmitri Shuralyov <dmitshur@google.com>
Reviewed-by: David Chase <drchase@google.com>
Reviewed-by: Evan Phoenix <evan@phx.io>
Run-TryBot: Johan Brandhorst-Satzkorn <johan.brandhorst@gmail.com>
Reviewed-by: Dmitri Shuralyov <dmitshur@golang.org>
Auto-Submit: Johan Brandhorst-Satzkorn <johan.brandhorst@gmail.com>
This commit is contained in:
Johan Brandhorst-Satzkorn 2023-01-27 22:53:25 -08:00 committed by Gopher Robot
parent f1855993f3
commit a64919945b

View File

@ -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
})