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

JSON response causes error #26

Closed
BlakeWilliams opened this issue Oct 9, 2014 · 7 comments
Closed

JSON response causes error #26

BlakeWilliams opened this issue Oct 9, 2014 · 7 comments

Comments

@BlakeWilliams
Copy link
Contributor

I'm trying to consume a JSON API and I get this error, ** (Protocol.UndefinedError) protocol String.Chars not implemented for {:closed, "{.

The rest of it's just a bunch of JSON. Any ideas?

@edgurgel
Copy link
Owner

edgurgel commented Oct 9, 2014

Can you share the last line executed on HTTPoison code? Also which version are you using?

@BlakeWilliams
Copy link
Contributor Author

Sure thing!

I'm using 0.4 and I'm calling HTTPoison.get("http://endpoint.herokuapp.com/long/api/path", headers)

Turns out the request succeeds locally where I don't have much data, but it seems to fail when I hit the remote endpoint which has a bit larger amount. It's nothing ridiculous though.

Sorry for the poor error report, it's late and I didn't know if this would actually get addressed any time soon.

Hackney gave :closed and the response back, I got this from removing the raise on body issues.

@BlakeWilliams
Copy link
Contributor Author

So the issue might just be that HTTPoison only responds with the body only on {:ok, body} and not {:closed, body} as well?

@edgurgel
Copy link
Owner

edgurgel commented Oct 9, 2014

Found the issue:

HTTPoison is calling to_string on this tuple. I think the right thing is to pass the reason as it's.

Adding this test on httpoison_base_test.exs shows the issue:

  test "request failing with a error reason tuple" do
    expect(:hackney, :request, 5, {:error, {:closed, "Something happened"}})

    assert_raise HTTPoison.HTTPError, "...", fn ->
      HTTPoison.post("localhost", "body")
    end

    assert validate :hackney
  end

Speaking of this, I'm also considering (as a breaking change) to not raise errors, just return a HTTPError struct. It would have a better behaviour and perfect for just pattern matching. I'll open an issue as "discussion".

Edited to add the place where we call to_string: https://github.com/edgurgel/httpoison/blob/master/lib/httpoison/base.ex#L93-L94

@BlakeWilliams
Copy link
Contributor Author

Sounds good, I think that's a much better way to approach the issue and was considering bringing it up.

@edgurgel
Copy link
Owner

edgurgel commented Oct 9, 2014

Released a new version with this fix: https://github.com/edgurgel/httpoison/releases/tag/0.4.3 , https://hex.pm/packages/httpoison/0.4.3

0.5.* may not have exceptions anymore.

@BlakeWilliams
Copy link
Contributor Author

Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants