Fix Response#to_hash to return empty hash when not finished#1639
Merged
Conversation
Add guard clause to return `{}` when response is not finished in order
to prevents potential errors when accessing response data `env` before
completion.
rossme
reviewed
Aug 21, 2025
Comment on lines
+62
to
+63
| return {} unless finished? | ||
|
|
There was a problem hiding this comment.
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
}
end
Contributor
Author
There was a problem hiding this comment.
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!
Contributor
Author
There was a problem hiding this comment.
I updated the pull request with this commit: a857c26
78c8fb0 to
a857c26
Compare
MitchLewis930
added a commit
to Signal65/faraday-Greptile
that referenced
this pull request
Jan 21, 2026
…yet (lostisland#1639)" This reverts commit edd8cc5.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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