Skip to content

Commit

Permalink
Merge pull request #12 from CJPoll/error-message
Browse files Browse the repository at this point in the history
Make a better error message if passing a non-binary to publish
  • Loading branch information
CJPoll authored Nov 10, 2017
2 parents 8c8806b + b42d116 commit 81774f8
Showing 1 changed file with 15 additions and 1 deletion.
16 changes: 15 additions & 1 deletion lib/subscribex.ex
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
defmodule Subscribex do
require Logger

defmodule InvalidPayloadException do
defexception [:message]
end

@type monitor :: reference
@type channel :: %AMQP.Channel{}

Expand All @@ -13,7 +17,6 @@ defmodule Subscribex do
@type payload :: String.t

defdelegate close(channel), to: AMQP.Channel
defdelegate publish(channel, exchange, routing_key, payload), to: AMQP.Basic
defdelegate ack(channel, delivery_tag), to: AMQP.Basic
defdelegate reject(channel, delivery_tag, options), to: AMQP.Basic

Expand Down Expand Up @@ -50,6 +53,17 @@ defmodule Subscribex do
result
end

@spec publish(channel, String.t, String.t, binary, keyword) :: :ok | :blocked | :closing
def publish(channel, exchange, routing_key, payload, options \\ [])

def publish(channel, exchange, routing_key, payload, options) when is_binary(payload) do
AMQP.Basic.publish(channel, exchange, routing_key, payload, options)
end

def publish(_, _, _, _, _) do
raise InvalidPayloadException, "Payload must be a binary"
end

## Private Functions

defp apply_link(%AMQP.Channel{} = channel, :no_link), do: channel
Expand Down

0 comments on commit 81774f8

Please sign in to comment.