-
Notifications
You must be signed in to change notification settings - Fork 998
Fix Response#to_hash to return empty hash when not finished #1639
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
Conversation
Add guard clause to return `{}` when response is not finished in order
to prevents potential errors when accessing response data `env` before
completion.
lib/faraday/response.rb
Outdated
| return {} unless finished? | ||
|
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for the PR @yykamei, good catch. A more explicit alternative would be to reuse the status, body and headers methods already defined in this class and adding def url. It also gives us an explicit reference that env = nil by retaining the hash keys and the nil values and not only returning an empty hash. What do you think?
def url
finished? ? env.url : nil
end def to_hash
{
status: status, body: body,
response_headers: headers,
url: url
}
endThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
OK, then I fix it and make the method return the data like this:
{
status: nil,
body: nil,
response_headers: {},
url: nil
}Thank you for reviewing!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I updated the pull request with this commit: a857c26
78c8fb0 to
a857c26
Compare
rossme
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🏁
iMacTia
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Description
I found that
Response#to_hashmight causeNoMethodErrorwhen it's initialized withoutenv.In this case,
#to_hashcannot return any data, so I added guard clause to return{}when response is not finished.Todos
List any remaining work that needs to be done, i.e:
[ ] Documentation