@@ -70,6 +70,26 @@ func errorFromResponse(resp *http.Response) *APIError {
7070 return & APIError {StatusCode : resp .StatusCode , Message : fmt .Sprintf ("unexpected status: %d" , resp .StatusCode )}
7171}
7272
73+ func (c * Client ) logResponse (resp * http.Response ) {
74+ raw , err := io .ReadAll (resp .Body )
75+ if err != nil {
76+ fmt .Fprintf (os .Stderr , "[debug] Response: %s (body read failed: %v)\n " , resp .Status , err )
77+ resp .Body = io .NopCloser (bytes .NewReader (nil ))
78+ return
79+ }
80+ resp .Body = io .NopCloser (bytes .NewReader (raw ))
81+ fmt .Fprintf (os .Stderr , "[debug] Response: %s (%d bytes)\n " , resp .Status , len (raw ))
82+ if len (raw ) == 0 {
83+ return
84+ }
85+ var pretty bytes.Buffer
86+ if json .Indent (& pretty , raw , "" , " " ) == nil {
87+ fmt .Fprintf (os .Stderr , "[debug] Body:\n %s\n " , pretty .String ())
88+ } else {
89+ fmt .Fprintf (os .Stderr , "[debug] Body: %s\n " , raw )
90+ }
91+ }
92+
7393func (c * Client ) do (req * http.Request ) (* http.Response , error ) {
7494 var (
7595 resp * http.Response
@@ -96,6 +116,9 @@ func (c *Client) do(req *http.Request) (*http.Response, error) {
96116 continue
97117 }
98118 if ! isRetryable (resp .StatusCode ) {
119+ if c .debug {
120+ c .logResponse (resp )
121+ }
99122 return resp , nil
100123 }
101124 if attempt < maxRetries {
0 commit comments