git-codereview: print more about Gerrit API errors

For golang/go#50576.

Change-Id: I3b67234098cbf9cd28bfa578c0b29744a4add2a3
Reviewed-on: https://go-review.googlesource.com/c/review/+/409914
Run-TryBot: Russ Cox <rsc@golang.org>
Reviewed-by: Dmitri Shuralyov <dmitshur@google.com>
Reviewed-by: Bryan Mills <bcmills@google.com>
TryBot-Result: Gopher Robot <gobot@golang.org>
Reviewed-by: Dmitri Shuralyov <dmitshur@golang.org>
diff --git a/git-codereview/api.go b/git-codereview/api.go
index 440489f..7a0da55 100644
--- a/git-codereview/api.go
+++ b/git-codereview/api.go
@@ -232,11 +232,15 @@
 // anti-xss line (]})' or some such) followed by JSON.
 // If requestBody != nil, gerritAPI sets the Content-Type to application/json.
 func gerritAPI(path string, requestBody []byte, target interface{}) (err error) {
+	var respBodyBytes []byte
 	defer func() {
 		if err != nil {
 			// os.Stderr, not stderr(), because the latter is not safe for
 			// use from multiple goroutines.
 			fmt.Fprintf(os.Stderr, "git-codereview: fetch %s: %v\n", path, err)
+			if len(respBodyBytes) > 0 {
+				fmt.Fprintf(os.Stderr, "Gerrit response:\n%s\n", respBodyBytes)
+			}
 		}
 	}()
 
@@ -281,6 +285,8 @@
 	body, err := ioutil.ReadAll(resp.Body)
 	resp.Body.Close()
 
+	respBodyBytes = body
+
 	if err != nil {
 		return fmt.Errorf("reading response body: %v", err)
 	}