Skip to content

Commit ed0badf

Browse files
authored
Include html respone if kosli command fails and the debug flag is set server/#4839 (#744)
1 parent b138726 commit ed0badf

2 files changed

Lines changed: 21 additions & 1 deletion

File tree

internal/requests/requests.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -267,6 +267,7 @@ func (c *Client) Do(p *RequestParams) (*HTTPResponse, error) {
267267
var respBody any
268268
err := json.Unmarshal([]byte(body), &respBody)
269269
if err != nil {
270+
c.Logger.Debug("response body from %s (status %d):\n%s", req.URL, resp.StatusCode, string(body))
270271
return &HTTPResponse{}, err
271272
}
272273
cleanedErrorMessage := ""

internal/requests/requests_test.go

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,10 @@ func (suite *RequestsTestSuite) SetupSuite() {
5959
Get("/fail/").
6060
Reply(500).
6161
BodyString("server broken")
62+
suite.fakeService.NewHandler().
63+
Get("/proxy-block/").
64+
Reply(403).
65+
BodyString(`<!DOCTYPE html><html><body><h1>403 Forbidden</h1><p>Blocked by proxy</p></body></html>`)
6266
}
6367

6468
// shutdown the fake service after the suite execution
@@ -262,6 +266,7 @@ func (suite *RequestsTestSuite) TestDo() {
262266
for _, t := range []struct {
263267
name string
264268
params *RequestParams
269+
debug bool
265270
wantError bool
266271
expectedLog string
267272
expectedErrorMsg string
@@ -377,15 +382,29 @@ func (suite *RequestsTestSuite) TestDo() {
377382
wantError: true,
378383
expectedErrorMsg: "unexpected end of JSON input",
379384
},
385+
{
386+
name: "non-JSON error response body is logged with debug enabled",
387+
params: &RequestParams{
388+
Method: http.MethodGet,
389+
URL: suite.fakeService.ResolveURL("/proxy-block/"),
390+
},
391+
debug: true,
392+
wantError: true,
393+
expectedErrorMsg: "invalid character '<' looking for beginning of value",
394+
expectedLog: `<!DOCTYPE html><html><body><h1>403 Forbidden</h1><p>Blocked by proxy</p></body></html>`,
395+
},
380396
} {
381397
suite.Run(t.name, func() {
382398
buf := new(bytes.Buffer)
383-
client, err := NewKosliClient("", 1, false, logger.NewLogger(buf, buf, false))
399+
client, err := NewKosliClient("", 1, t.debug, logger.NewLogger(buf, buf, t.debug))
384400
require.NoError(suite.T(), err)
385401
resp, err := client.Do(t.params)
386402
if t.wantError {
387403
require.Error(suite.T(), err)
388404
require.Equal(suite.T(), t.expectedErrorMsg, err.Error())
405+
if t.expectedLog != "" {
406+
require.Contains(suite.T(), buf.String(), t.expectedLog)
407+
}
389408
} else {
390409
require.NoError(suite.T(), err)
391410
output := buf.String()

0 commit comments

Comments
 (0)