Skip to content

Commit 1d378b9

Browse files
committed
Return errors when response reading fails
Previously we got partial results instead, and we were having this happening silently. If we were lucky, the json parsing failed though.
1 parent 7776f92 commit 1d378b9

File tree

2 files changed

+11
-5
lines changed

2 files changed

+11
-5
lines changed

okta/requestExecutor.go

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -649,7 +649,10 @@ func CheckResponseForError(resp *http.Response) error {
649649
}
650650
}
651651
}
652-
bodyBytes, _ := io.ReadAll(resp.Body)
652+
bodyBytes, err := io.ReadAll(resp.Body)
653+
if err != nil {
654+
return err
655+
}
653656
copyBodyBytes := make([]byte, len(bodyBytes))
654657
copy(copyBodyBytes, bodyBytes)
655658
_ = resp.Body.Close()
@@ -668,7 +671,10 @@ func buildResponse(resp *http.Response, re *RequestExecutor, v interface{}) (*Re
668671
if err != nil {
669672
return response, err
670673
}
671-
bodyBytes, _ := io.ReadAll(resp.Body)
674+
bodyBytes, err := io.ReadAll(resp.Body)
675+
if err != nil {
676+
return nil, err
677+
}
672678
copyBodyBytes := make([]byte, len(bodyBytes))
673679
copy(copyBodyBytes, bodyBytes)
674680
_ = resp.Body.Close() // close it to avoid memory leaks

tests/unit/request_executor_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,15 +23,15 @@ func (r readerFun) Read(p []byte) (n int, err error) { return r(p) }
2323
type slowTransport struct{}
2424

2525
// RoundTrip, part of http.Transport interface. This servers 42 as a JSON response, but slowly.
26-
// In particular, we serve the response immediately, but getting the body takes a second.
26+
// In particular, we serve the response immediately, but getting the body takes some milliseconds.
2727
func (t slowTransport) RoundTrip(req *http.Request) (*http.Response, error) {
2828
realBody := strings.NewReader("42")
29-
// This body takes 1 second to read. It also needs a valid context for the whole duration.
29+
// This body takes 1 millisecond to read. It also needs a valid context for the whole duration.
3030
slowBody := func(p []byte) (n int, err error) {
3131
select {
3232
case <-req.Context().Done():
3333
return 0, req.Context().Err()
34-
case <-time.After(1 * time.Second):
34+
case <-time.After(1 * time.Millisecond):
3535
return realBody.Read(p)
3636
}
3737
}

0 commit comments

Comments
 (0)