Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

check response size for debug logs #99

Merged
merged 3 commits into from
Oct 18, 2017
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 9 additions & 6 deletions response.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,19 +120,22 @@ func (r *Response) RawBody() io.ReadCloser {
}

func (r *Response) fmtBodyString() string {
bodyStr := "***** NO CONTENT *****"
if r.body != nil {
if len(r.body) > 1000000 {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What is this 1000000 value? are you picking random max value to limit it? and why?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yea.. It is arbitrary. What do you suggest? Should I add a var so that the user can override if required?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Making this value configurable at client level would be best. Since some user wants to get complete body value.

  • Add an un-exported field debugBodySizeLimit int64 to Client and method in the client SetDebugBodyLimit(sl int64)
  • Initializing debugBodySizeLimit default value as math.MaxInt32 (this is good enough to begin with) at client instance creation
  • Add corresponding method for default resty client in default.go

This way it will satisfy possibilities.

return "***** RESPONSE TOO LARGE *****"
}
ct := r.Header().Get(hdrContentTypeKey)
if IsJSONType(ct) {
out := acquireBuffer()
defer releaseBuffer(out)
if err := json.Indent(out, r.body, "", " "); err == nil {
bodyStr = string(out.Bytes())
err := json.Indent(out, r.body, "", " ")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This could be better and concise.

if err := json.Indent(out, r.body, "", "   "); err == nil {
    return out.String()
}

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That results in a warning from golint - if block ends with a return statement, so drop this else and outdent its block (move short variable declaration to its own line if necessary) (golint).

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually remove this line return "unexpected error: " + err.Error() with my above code snippet then golint warning goaway. Because if any errors in formatting debug log, return the original body value which is r.String().

if err == nil {
return out.String()
}
} else {
bodyStr = r.String()
return "unexpected error: " + err.Error()
}
return r.String()
}

return bodyStr
return "***** NO CONTENT *****"
}