Skip to content

Commit

Permalink
Allow using poison 3.0 in addition to 1.5 and 2.0
Browse files Browse the repository at this point in the history
  • Loading branch information
mkrogemann committed Nov 19, 2016
1 parent 4a3dfb3 commit f9c5115
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 13 deletions.
25 changes: 25 additions & 0 deletions lib/couchdb_connector/as_map.ex
Expand Up @@ -28,6 +28,7 @@ defimpl Couchdb.Connector.AsMap, for: BitString do
def as_map(json) do
case Poison.decode(json) do
{:ok, decoded} -> decoded
# poison 1.5 and 2.0 can produce these errors
{:error, :invalid} ->
raise RuntimeError, message:
"""
Expand All @@ -41,6 +42,30 @@ defimpl Couchdb.Connector.AsMap, for: BitString do
token: #{token}
json: #{json}
"""
# poison 3.0 can produce these errors
{:error, :invalid, pos} ->
raise RuntimeError, message:
"""
Document returned by CouchDB is invalid
pos: #{pos}
json: #{json}
"""
{:error, {:invalid, token, pos}} ->
raise RuntimeError, message:
"""
Document returned by CouchDB is invalid
token: #{token}
pos: #{pos}
json: #{json}
"""
# catch all
{:error, any} ->
raise RuntimeError, message:
"""
Document returned by CouchDB is invalid
reason: #{inspect(any)}
json: #{json}
"""
end
end
end
Expand Down
4 changes: 2 additions & 2 deletions mix.exs
Expand Up @@ -4,7 +4,7 @@ defmodule Couchdb.Connector.Mixfile do
def project do
[
app: :couchdb_connector,
version: "0.4.0",
version: "0.4.1",
elixir: "~> 1.2",
build_embedded: Mix.env == :prod,
start_permanent: Mix.env == :prod,
Expand All @@ -23,7 +23,7 @@ defmodule Couchdb.Connector.Mixfile do
defp deps do
[
{:httpoison, "~> 0.8 or ~> 0.9 or ~> 0.10"},
{:poison, "~> 1.5 or ~> 2.0"},
{:poison, "~> 1.5 or ~> 2.0 or ~> 3.0"},
{:excoveralls, "~> 0.5", only: [:dev, :test]},
{:credo, "~> 0.5", only: [:dev, :test]},
{:earmark, "~> 1.0", only: :dev},
Expand Down
2 changes: 1 addition & 1 deletion mix.lock
Expand Up @@ -12,5 +12,5 @@
"jsx": {:hex, :jsx, "2.6.2", "213721e058da0587a4bce3cc8a00ff6684ced229c8f9223245c6ff2c88fbaa5a", [:mix, :rebar], []},
"metrics": {:hex, :metrics, "1.0.1", "25f094dea2cda98213cecc3aeff09e940299d950904393b2a29d191c346a8486", [:rebar3], []},
"mimerl": {:hex, :mimerl, "1.0.2", "993f9b0e084083405ed8252b99460c4f0563e41729ab42d9074fd5e52439be88", [:rebar3], []},
"poison": {:hex, :poison, "2.2.0", "4763b69a8a77bd77d26f477d196428b741261a761257ff1cf92753a0d4d24a63", [:mix], []},
"poison": {:hex, :poison, "3.0.0", "625ebd64d33ae2e65201c2c14d6c85c27cc8b68f2d0dd37828fde9c6920dd131", [:mix], []},
"ssl_verify_fun": {:hex, :ssl_verify_fun, "1.1.1", "28a4d65b7f59893bc2c7de786dec1e1555bd742d336043fe644ae956c3497fbe", [:make, :rebar], []}}
12 changes: 2 additions & 10 deletions test/couchdb_connector/as_map_test.exs
Expand Up @@ -6,21 +6,13 @@ defmodule Couchdb.Connector.AsMapTest do
test "as_map/1 with invalid json string should raise RuntimeError" do
invalid = "{\"_id\":\"foo\",\"_rev\":\"1-0f97561a543ed2e9c98a24dea818ec10\",test_key\":\"test_value\"}\n"

assert_raise(RuntimeError, """
Document returned by CouchDB is invalid
token: t
json: {"_id":"foo","_rev":"1-0f97561a543ed2e9c98a24dea818ec10",test_key":"test_value"}\n
""", fn -> as_map(invalid) end)
assert_raise(RuntimeError, fn -> as_map(invalid) end)
end

test "as_map/1 with empty string should raise RuntimeError" do
empty = ""

assert_raise(RuntimeError, """
Document returned by CouchDB is invalid
json: #{empty}
""",
fn -> as_map(empty) end)
assert_raise(RuntimeError, fn -> as_map(empty) end)
end

test "as_map/1 with valid json string should return decoded Map" do
Expand Down

0 comments on commit f9c5115

Please sign in to comment.