Attention is currently required from: Damien Neil, Dmitri Shuralyov, Ian Lance Taylor.
Emmanuel Odeke would like Damien Neil, Dmitri Shuralyov and Ian Lance Taylor to review this change.
net/http: make ExampleGet show StatusCode checks for non-1XX,2XX responses
Updates ExampleGet to show how to handle bad responses with non-1XX,2XX
status codes. Given that the canonical examples are copied, we need
to have them properly check against failures. This is a bug I've seen
often in the wild, that's exacerbated when for example unmarshalling
JSON or even protobufs, and no errors are returned by the decoders,
so code fails silently after making a request for example to a gateway
that they were unauthorized to access.
Fixes #39778
Change-Id: I1cd688f2fab47581c8cf228235d3662b4c8e4315
---
M src/net/http/example_test.go
1 file changed, 5 insertions(+), 2 deletions(-)
diff --git a/src/net/http/example_test.go b/src/net/http/example_test.go
index c677d52..2f411d1 100644
--- a/src/net/http/example_test.go
+++ b/src/net/http/example_test.go
@@ -45,12 +45,15 @@
if err != nil {
log.Fatal(err)
}
- robots, err := io.ReadAll(res.Body)
+ body, err := io.ReadAll(res.Body)
res.Body.Close()
+ if res.StatusCode > 299 {
+ log.Fatalf("Response failed with status code: %d and\nbody: %s\n", res.StatusCode, body)
+ }
if err != nil {
log.Fatal(err)
}
- fmt.Printf("%s", robots)
+ fmt.Printf("%s", body)
}
func ExampleFileServer() {
To view, visit change 299609. To unsubscribe, or for help writing mail filters, visit settings.
Attention is currently required from: Damien Neil, Dmitri Shuralyov, Ian Lance Taylor.
Patch set 1:Run-TryBot +1Trust +1
Attention is currently required from: Dmitri Shuralyov, Ian Lance Taylor, Emmanuel Odeke.
Patch set 1:Code-Review +2
Damien Neil submitted this change.
net/http: make ExampleGet show StatusCode checks for non-1XX,2XX responses
Updates ExampleGet to show how to handle bad responses with non-1XX,2XX
status codes. Given that the canonical examples are copied, we need
to have them properly check against failures. This is a bug I've seen
often in the wild, that's exacerbated when for example unmarshalling
JSON or even protobufs, and no errors are returned by the decoders,
so code fails silently after making a request for example to a gateway
that they were unauthorized to access.
Fixes #39778
Change-Id: I1cd688f2fab47581c8cf228235d3662b4c8e4315
Reviewed-on: https://go-review.googlesource.com/c/go/+/299609
Trust: Emmanuel Odeke <emma...@orijtech.com>
Run-TryBot: Emmanuel Odeke <emma...@orijtech.com>
TryBot-Result: Go Bot <go...@golang.org>
Reviewed-by: Damien Neil <dn...@google.com>
---
M src/net/http/example_test.go
1 file changed, 5 insertions(+), 2 deletions(-)
diff --git a/src/net/http/example_test.go b/src/net/http/example_test.go
index c677d52..2f411d1 100644
--- a/src/net/http/example_test.go
+++ b/src/net/http/example_test.go
@@ -45,12 +45,15 @@
if err != nil {
log.Fatal(err)
}
- robots, err := io.ReadAll(res.Body)
+ body, err := io.ReadAll(res.Body)
res.Body.Close()
+ if res.StatusCode > 299 {
+ log.Fatalf("Response failed with status code: %d and\nbody: %s\n", res.StatusCode, body)
+ }
if err != nil {
log.Fatal(err)
}
- fmt.Printf("%s", robots)
+ fmt.Printf("%s", body)
}
func ExampleFileServer() {
To view, visit change 299609. To unsubscribe, or for help writing mail filters, visit settings.