diff options
| author | Johan Brandhorst-Satzkorn <johan.brandhorst@gmail.com> | 2023-01-27 22:53:25 -0800 |
|---|---|---|
| committer | Gopher Robot <gobot@golang.org> | 2023-02-09 20:44:24 +0000 |
| commit | a64919945bdb65202783c5f9e078c3e735efc58f (patch) | |
| tree | d15ccbc0350a959a3b35a576e28002b6111f8a64 /src/net/http | |
| parent | f1855993f3aa0042014952b3a3ce80296df838b1 (diff) | |
| download | go-a64919945bdb65202783c5f9e078c3e735efc58f.tar.xz | |
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>
Diffstat (limited to 'src/net/http')
| -rw-r--r-- | src/net/http/roundtrip_js.go | 17 |
1 files changed, 16 insertions, 1 deletions
diff --git a/src/net/http/roundtrip_js.go b/src/net/http/roundtrip_js.go index 21d8df9686..4f381247cd 100644 --- a/src/net/http/roundtrip_js.go +++ b/src/net/http/roundtrip_js.go @@ -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 }) |
