Skip to content

Commit

Permalink
Avoid raising on undefined protocol in Msgpax.pack/1 (#54)
Browse files Browse the repository at this point in the history
  • Loading branch information
mitchellhenke committed Dec 10, 2020
1 parent 28fa446 commit 8251397
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 1 deletion.
5 changes: 4 additions & 1 deletion lib/msgpax.ex
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ defmodule Msgpax do
{:ok, <<163, 102, 111, 111>>}
"""
@spec pack(term, Keyword.t()) :: {:ok, iodata} | {:error, Msgpax.PackError.t()}
@spec pack(term, Keyword.t()) :: {:ok, iodata} | {:error, Msgpax.PackError.t() | Exception.t()}
def pack(term, options \\ []) when is_list(options) do
iodata? = Keyword.get(options, :iodata, true)

Expand All @@ -71,6 +71,9 @@ defmodule Msgpax do
catch
:throw, reason ->
{:error, %Msgpax.PackError{reason: reason}}

:error, %Protocol.UndefinedError{protocol: Msgpax.Packer} = exception ->
{:error, exception}
else
iodata when iodata? ->
{:ok, iodata}
Expand Down
4 changes: 4 additions & 0 deletions test/msgpax_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,10 @@ defmodule MsgpaxTest do
{:error, %PackError{reason: {:not_encodable, <<5::3>>}}}
end

test "protocol not implemented" do
assert {:error, %Protocol.UndefinedError{}} = Msgpax.pack([{}])
end

test "pack!/2 with the :iodata option" do
assert Msgpax.pack!([], iodata: true) == [144]
assert Msgpax.pack!([], iodata: false) == <<144>>
Expand Down

0 comments on commit 8251397

Please sign in to comment.