Skip to content

Commit 4a2d07a

Browse files
[FSSDK-9155] fix(utils): logging correct error message in HTTP requests (#374)
* Fixed error being logged in HTTP requests * Added test for logged errors * Improve test error checking + comments * Fix unnecessary re-cast in Error * Update copyright dates in headers * add review changes * Update bad url example --------- Co-authored-by: Sean Pfeifer <[email protected]>
1 parent 2d5458a commit 4a2d07a

File tree

2 files changed

+35
-6
lines changed

2 files changed

+35
-6
lines changed

pkg/utils/requester.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/****************************************************************************
2-
* Copyright 2019,2022 Optimizely, Inc. and contributors *
2+
* Copyright 2019,2022-2023 Optimizely, Inc. and contributors *
33
* *
44
* Licensed under the Apache License, Version 2.0 (the "License"); *
55
* you may not use this file except in compliance with the License. *
@@ -143,7 +143,7 @@ func (r HTTPRequester) Do(url, method string, body io.Reader, headers []Header)
143143
single := func(request *http.Request) (response []byte, responseHeaders http.Header, code int, e error) {
144144
resp, doErr := r.client.Do(request)
145145
if doErr != nil {
146-
r.logger.Error(fmt.Sprintf("failed to send request %v", request), e)
146+
r.logger.Error(fmt.Sprintf("failed to send request %v", request), doErr)
147147
return nil, http.Header{}, 0, doErr
148148
}
149149
defer func() {

pkg/utils/requester_test.go

+33-4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/****************************************************************************
2-
* Copyright 2019,2021-2022 Optimizely, Inc. and contributors *
2+
* Copyright 2019,2021-2023 Optimizely, Inc. and contributors *
33
* *
44
* Licensed under the Apache License, Version 2.0 (the "License"); *
55
* you may not use this file except in compliance with the License. *
@@ -190,12 +190,41 @@ func TestPostObj(t *testing.T) {
190190
assert.NotNil(t, err)
191191
}
192192

193+
type mockLogger struct {
194+
Errors []error
195+
}
196+
197+
func (m *mockLogger) Debug(message string) {}
198+
func (m *mockLogger) Info(message string) {}
199+
func (m *mockLogger) Warning(message string) {}
200+
func (m *mockLogger) Error(message string, err interface{}) {
201+
if err, ok := err.(error); ok {
202+
m.Errors = append(m.Errors, err)
203+
}
204+
}
205+
193206
func TestGetBad(t *testing.T) {
207+
// Using a mockLogger to ensure we're logging the expected error message
208+
mLogger := &mockLogger{}
209+
httpreq := NewHTTPRequester(mLogger)
194210

195-
httpreq := NewHTTPRequester(logging.GetLogger("", ""))
196-
_, _, _, err := httpreq.Get("blah12345/good")
197-
_, ok := err.(*url.Error)
211+
badURL := "http://ww.bad-url.fake/blah12345"
212+
_, _, _, err := httpreq.Get(badURL)
213+
returnedErr, ok := err.(*url.Error)
198214
assert.True(t, ok, "url error")
215+
216+
// Check to make sure we have some log for bad url
217+
assert.NotNil(t, mLogger.Errors)
218+
// If we didn't get the expected error, we need to stop before we do the rest
219+
// of the checks that depend on that error.
220+
if !assert.Len(t, mLogger.Errors, 1, "logged error") {
221+
t.FailNow()
222+
}
223+
// Check to make sure the error that was logged is the same as what was returned
224+
loggedErr, ok := mLogger.Errors[0].(*url.Error)
225+
assert.True(t, ok, "is URL error")
226+
assert.Equal(t, returnedErr, loggedErr, "expected same error")
227+
assert.Equal(t, badURL, loggedErr.URL, "expected the URL we requested")
199228
}
200229

201230
func TestGetBadWithResponse(t *testing.T) {

0 commit comments

Comments
 (0)