]> Cypherpunks repositories - gostls13.git/commitdiff
net/http: improve js fetch errors
authorJohan Brandhorst-Satzkorn <johan.brandhorst@gmail.com>
Sat, 28 Jan 2023 06:53:25 +0000 (22:53 -0800)
committerGopher Robot <gobot@golang.org>
Thu, 9 Feb 2023 20:44:24 +0000 (20:44 +0000)
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>

src/net/http/roundtrip_js.go

index 21d8df96868f48ce825268c874e603a9bbbe4547..4f381247cd13ef888e135963af946155b24b69dc 100644 (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
        })