on main help sign in
fix(log): don't conflate body drain errors with HTTP errors
ffc7a9dChristian Rocha
internal/log/http.go+5−2
@@ -63,6 +63,10 @@ func (h *HTTPRoundTripLogger) RoundTrip(req *http.Request) (*http.Response, erro
63 }
63 }
64
64
65 save, resp.Body, err = drainBody(resp.Body)
65 save, resp.Body, err = drainBody(resp.Body)
66+ if err != nil {
67+ slog.Error("Failed to drain response body", "error", err)
68+ return resp, err
69+ }
66 if slog.Default().Enabled(req.Context(), slog.LevelDebug) {
70 if slog.Default().Enabled(req.Context(), slog.LevelDebug) {
67 slog.Debug(
71 slog.Debug(
68 "HTTP Response",
72 "HTTP Response",
@@ -72,10 +76,9 @@ func (h *HTTPRoundTripLogger) RoundTrip(req *http.Request) (*http.Response, erro
72 "body", bodyToString(save),
76 "body", bodyToString(save),
73 "content_length", resp.ContentLength,
77 "content_length", resp.ContentLength,
74 "duration_ms", duration.Milliseconds(),
78 "duration_ms", duration.Milliseconds(),
75− "error", err,
76 )
79 )
77 }
80 }
78− return resp, err
81+ return resp, nil
79}
82}
80
83
81func bodyToString(body io.ReadCloser) string {
84func bodyToString(body io.ReadCloser) string {