Skip to content

Commit

Permalink
don't raise when calling deliver/2 or deliver_later/2
Browse files Browse the repository at this point in the history
  • Loading branch information
poops committed May 30, 2018
1 parent 023b012 commit 835c511
Show file tree
Hide file tree
Showing 9 changed files with 34 additions and 69 deletions.
10 changes: 3 additions & 7 deletions lib/bamboo/adapters/mailgun_adapter.ex
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,10 @@ defmodule Bamboo.MailgunAdapter do
end
"""

@service_name "Mailgun"
@base_uri "https://api.mailgun.net/v3"
@behaviour Bamboo.Adapter

alias Bamboo.{Email, Attachment}
import Bamboo.ApiError

@doc false
def handle_config(config) do
Expand All @@ -49,12 +47,10 @@ defmodule Bamboo.MailgunAdapter do
def deliver(email, config) do
body = to_mailgun_body(email)
case :hackney.post(full_uri(config), headers(email, config), body, [:with_body]) do
{:ok, status, _headers, response} when status > 299 ->
raise_api_error(@service_name, response, body)
{:ok, status, headers, response} ->
%{status_code: status, headers: headers, body: response}
{:error, reason} ->
raise_api_error(inspect(reason))
{:ok, %{status_code: status, headers: headers, body: response}}

resp -> resp
end
end

Expand Down
12 changes: 3 additions & 9 deletions lib/bamboo/adapters/mandrill_adapter.ex
Original file line number Diff line number Diff line change
Expand Up @@ -19,27 +19,21 @@ defmodule Bamboo.MandrillAdapter do
end
"""

@service_name "Mandrill"
@default_base_uri "https://mandrillapp.com"
@send_message_path "api/1.0/messages/send.json"
@send_message_template_path "api/1.0/messages/send-template.json"
@behaviour Bamboo.Adapter

import Bamboo.ApiError

def deliver(email, config) do
api_key = get_key(config)
params = email |> convert_to_mandrill_params(api_key) |> Poison.encode!
uri = [base_uri(), "/", api_path(email)]

case :hackney.post(uri, headers(), params, [:with_body]) do
{:ok, status, _headers, response} when status > 299 ->
filtered_params = params |> Poison.decode! |> Map.put("key", "[FILTERED]")
raise_api_error(@service_name, response, filtered_params)
{:ok, status, headers, response} ->
%{status_code: status, headers: headers, body: response}
{:error, reason} ->
raise_api_error(inspect(reason))
{:ok, %{status_code: status, headers: headers, body: response}}

resp -> resp
end
end

Expand Down
11 changes: 3 additions & 8 deletions lib/bamboo/adapters/send_grid_adapter.ex
Original file line number Diff line number Diff line change
Expand Up @@ -27,27 +27,22 @@ defmodule Bamboo.SendGridAdapter do
end
"""

@service_name "SendGrid"
@default_base_uri "https://sendgrid.com/v3/"
@send_message_path "/mail/send"
@behaviour Bamboo.Adapter

alias Bamboo.Email
import Bamboo.ApiError

def deliver(email, config) do
api_key = get_key(config)
body = email |> to_sendgrid_body(config) |> Poison.encode!
url = [base_uri(), @send_message_path]

case :hackney.post(url, headers(api_key), body, [:with_body]) do
{:ok, status, _headers, response} when status > 299 ->
filtered_params = body |> Poison.decode! |> Map.put("key", "[FILTERED]")
raise_api_error(@service_name, response, filtered_params)
{:ok, status, headers, response} ->
%{status_code: status, headers: headers, body: response}
{:error, reason} ->
raise_api_error(inspect(reason))
{:ok, %{status_code: status, headers: headers, body: response}}

resp -> resp
end
end

Expand Down
8 changes: 4 additions & 4 deletions lib/bamboo/mailer.ex
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ defmodule Bamboo.Mailer do
`deliver_later/1` if you want to send in the background to speed things up.
"""
def deliver_now(_email) do
raise @cannot_call_directly_error
{:error, @cannot_call_directly_error}
end

@doc """
Expand All @@ -110,7 +110,7 @@ defmodule Bamboo.Mailer do
with `deliver_later/1`.
"""
def deliver_later(_email) do
raise @cannot_call_directly_error
{:error, @cannot_call_directly_error}
end

@doc false
Expand All @@ -119,11 +119,11 @@ defmodule Bamboo.Mailer do

if email.to == [] && email.cc == [] && email.bcc == [] do
debug_unsent(email)
{:error, :empty_recipients}
else
debug_sent(email, adapter)
adapter.deliver(email, config)
end
email
end

@doc false
Expand All @@ -132,11 +132,11 @@ defmodule Bamboo.Mailer do

if email.to == [] && email.cc == [] && email.bcc == [] do
debug_unsent(email)
{:error, :empty_recipients}
else
debug_sent(email, adapter)
config.deliver_later_strategy.deliver_later(adapter, email, config)
end
email
end

defp debug_sent(email, adapter) do
Expand Down
10 changes: 6 additions & 4 deletions mix.lock
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
%{"certifi": {:hex, :certifi, "2.3.1", "d0f424232390bf47d82da8478022301c561cf6445b5b5fb6a84d49a9e76d2639", [:rebar3], [{:parse_trans, "3.2.0", [hex: :parse_trans, repo: "hexpm", optional: false]}], "hexpm"},
%{
"certifi": {:hex, :certifi, "2.3.1", "d0f424232390bf47d82da8478022301c561cf6445b5b5fb6a84d49a9e76d2639", [:rebar3], [{:parse_trans, "3.2.0", [hex: :parse_trans, repo: "hexpm", optional: false]}], "hexpm"},
"cowboy": {:hex, :cowboy, "1.1.2", "61ac29ea970389a88eca5a65601460162d370a70018afe6f949a29dca91f3bb0", [:rebar3], [{:cowlib, "~> 1.0.2", [hex: :cowlib, repo: "hexpm", optional: false]}, {:ranch, "~> 1.3.2", [hex: :ranch, repo: "hexpm", optional: false]}], "hexpm"},
"cowlib": {:hex, :cowlib, "1.0.2", "9d769a1d062c9c3ac753096f868ca121e2730b9a377de23dec0f7e08b1df84ee", [:make], [], "hexpm"},
"earmark": {:hex, :earmark, "1.2.5", "4d21980d5d2862a2e13ec3c49ad9ad783ffc7ca5769cf6ff891a4553fbaae761", [:mix], [], "hexpm"},
Expand All @@ -10,11 +11,11 @@
"hackney": {:hex, :hackney, "1.12.1", "8bf2d0e11e722e533903fe126e14d6e7e94d9b7983ced595b75f532e04b7fdc7", [:rebar3], [{:certifi, "2.3.1", [hex: :certifi, repo: "hexpm", optional: false]}, {:idna, "5.1.1", [hex: :idna, repo: "hexpm", optional: false]}, {:metrics, "1.0.1", [hex: :metrics, repo: "hexpm", optional: false]}, {:mimerl, "1.0.2", [hex: :mimerl, repo: "hexpm", optional: false]}, {:ssl_verify_fun, "1.1.1", [hex: :ssl_verify_fun, repo: "hexpm", optional: false]}], "hexpm"},
"html_entities": {:hex, :html_entities, "0.4.0", "f2fee876858cf6aaa9db608820a3209e45a087c5177332799592142b50e89a6b", [:mix], [], "hexpm"},
"idna": {:hex, :idna, "5.1.1", "cbc3b2fa1645113267cc59c760bafa64b2ea0334635ef06dbac8801e42f7279c", [:rebar3], [{:unicode_util_compat, "0.3.1", [hex: :unicode_util_compat, repo: "hexpm", optional: false]}], "hexpm"},
"jsx": {:hex, :jsx, "2.8.3", "a05252d381885240744d955fbe3cf810504eb2567164824e19303ea59eef62cf", [], [], "hexpm"},
"jsx": {:hex, :jsx, "2.8.3", "a05252d381885240744d955fbe3cf810504eb2567164824e19303ea59eef62cf", [:mix, :rebar3], [], "hexpm"},
"metrics": {:hex, :metrics, "1.0.1", "25f094dea2cda98213cecc3aeff09e940299d950904393b2a29d191c346a8486", [:rebar3], [], "hexpm"},
"mime": {:hex, :mime, "1.2.0", "78adaa84832b3680de06f88f0997e3ead3b451a440d183d688085be2d709b534", [:mix], [], "hexpm"},
"mimerl": {:hex, :mimerl, "1.0.2", "993f9b0e084083405ed8252b99460c4f0563e41729ab42d9074fd5e52439be88", [:rebar3], [], "hexpm"},
"mochiweb": {:hex, :mochiweb, "2.15.0", "e1daac474df07651e5d17cc1e642c4069c7850dc4508d3db7263a0651330aacc", [], [], "hexpm"},
"mochiweb": {:hex, :mochiweb, "2.15.0", "e1daac474df07651e5d17cc1e642c4069c7850dc4508d3db7263a0651330aacc", [:rebar3], [], "hexpm"},
"parse_trans": {:hex, :parse_trans, "3.2.0", "2adfa4daf80c14dc36f522cf190eb5c4ee3e28008fc6394397c16f62a26258c2", [:rebar3], [], "hexpm"},
"phoenix": {:hex, :phoenix, "1.3.0", "1c01124caa1b4a7af46f2050ff11b267baa3edb441b45dbf243e979cd4c5891b", [:mix], [{:cowboy, "~> 1.0", [hex: :cowboy, repo: "hexpm", optional: true]}, {:phoenix_pubsub, "~> 1.0", [hex: :phoenix_pubsub, repo: "hexpm", optional: false]}, {:plug, "~> 1.3.3 or ~> 1.4", [hex: :plug, repo: "hexpm", optional: false]}, {:poison, "~> 2.2 or ~> 3.0", [hex: :poison, repo: "hexpm", optional: false]}], "hexpm"},
"phoenix_html": {:hex, :phoenix_html, "2.11.2", "86ebd768258ba60a27f5578bec83095bdb93485d646fc4111db8844c316602d6", [:mix], [{:plug, "~> 1.5", [hex: :plug, repo: "hexpm", optional: false]}], "hexpm"},
Expand All @@ -23,4 +24,5 @@
"poison": {:hex, :poison, "3.1.0", "d9eb636610e096f86f25d9a46f35a9facac35609a7591b3be3326e99a0484665", [:mix], [], "hexpm"},
"ranch": {:hex, :ranch, "1.3.2", "e4965a144dc9fbe70e5c077c65e73c57165416a901bd02ea899cfd95aa890986", [:rebar3], [], "hexpm"},
"ssl_verify_fun": {:hex, :ssl_verify_fun, "1.1.1", "28a4d65b7f59893bc2c7de786dec1e1555bd742d336043fe644ae956c3497fbe", [:make, :rebar], [], "hexpm"},
"unicode_util_compat": {:hex, :unicode_util_compat, "0.3.1", "a1f612a7b512638634a603c8f401892afbf99b8ce93a45041f8aaca99cadb85e", [], [], "hexpm"}}
"unicode_util_compat": {:hex, :unicode_util_compat, "0.3.1", "a1f612a7b512638634a603c8f401892afbf99b8ce93a45041f8aaca99cadb85e", [:rebar3], [], "hexpm"},
}
7 changes: 3 additions & 4 deletions test/lib/bamboo/adapters/mailgun_adapter_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -154,12 +154,11 @@ defmodule Bamboo.MailgunAdapterTest do
assert params["bcc"] == "BCC <bcc@bar.com>"
end

test "raises if the response is not a success" do
test "returns error status if the response is not a success" do
email = new_email(from: "INVALID_EMAIL")

assert_raise Bamboo.ApiError, fn ->
email |> MailgunAdapter.deliver(@config)
end
{:ok, %{status_code: 500, body: "Error!!"}} =
MailgunAdapter.deliver(email, @config)
end

defp new_email(attrs \\ []) do
Expand Down
15 changes: 3 additions & 12 deletions test/lib/bamboo/adapters/mandrill_adapter_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -172,20 +172,11 @@ defmodule Bamboo.MandrillAdapterTest do
assert template_content == [%{"content" => 'example content', "name" => 'example name'}]
end

test "raises if the response is not a success" do
test "returns error status if the response is not a success" do
email = new_email(from: "INVALID_EMAIL")

assert_raise Bamboo.ApiError, fn ->
email |> MandrillAdapter.deliver(@config)
end
end

test "removes api key from error output" do
email = new_email(from: "INVALID_EMAIL")

assert_raise Bamboo.ApiError, ~r/"key" => "\[FILTERED\]"/, fn ->
email |> MandrillAdapter.deliver(@config)
end
{:ok, %{status_code: 500, body: "Error!!"}} =
MandrillAdapter.deliver(email, @config)
end

defp new_email(attrs \\ []) do
Expand Down
15 changes: 3 additions & 12 deletions test/lib/bamboo/adapters/send_grid_adapter_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -203,20 +203,11 @@ defmodule Bamboo.SendGridAdapterTest do
assert params["mail_settings"]["sandbox"] == true
end

test "raises if the response is not a success" do
test "returns error status if the response is not a success" do
email = new_email(from: "INVALID_EMAIL")

assert_raise Bamboo.ApiError, fn ->
email |> SendGridAdapter.deliver(@config)
end
end

test "removes api key from error output" do
email = new_email(from: "INVALID_EMAIL")

assert_raise Bamboo.ApiError, ~r/"key" => "\[FILTERED\]"/, fn ->
email |> SendGridAdapter.deliver(@config)
end
{:ok, %{status_code: 500, body: "Error!!"}} =
SendGridAdapter.deliver(email, @config)
end

defp new_email(attrs \\ []) do
Expand Down
15 changes: 6 additions & 9 deletions test/lib/bamboo/mailer_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -113,10 +113,9 @@ defmodule Bamboo.MailerTest do
test "deliver_now/1 calls the adapter with the email and config as a map" do
email = new_email(to: "foo@bar.com")

returned_email = FooMailer.deliver_now(email)
{:deliver, returned_email, config} = FooMailer.deliver_now(email)

assert returned_email == Bamboo.Mailer.normalize_addresses(email)
assert_received {:deliver, %Bamboo.Email{}, config}
config_with_default_strategy = Enum.into(@mailer_config, %{})
|> Map.put(:deliver_later_strategy, Bamboo.TaskSupervisorStrategy)
assert config == config_with_default_strategy
Expand Down Expand Up @@ -246,16 +245,14 @@ defmodule Bamboo.MailerTest do
end
end

test "raises an error if deliver_now or deliver_later is called directly" do
test "returns an error if deliver_now or deliver_later is called directly" do
email = new_email(from: %{foo: :bar})

assert_raise RuntimeError, ~r/cannot call Bamboo.Mailer/, fn ->
Bamboo.Mailer.deliver_now(email)
end
{:error, error} = Bamboo.Mailer.deliver_now(email)
assert String.contains?(error, "cannot call Bamboo.Mailer")

assert_raise RuntimeError, ~r/cannot call Bamboo.Mailer/, fn ->
Bamboo.Mailer.deliver_later(email)
end
{:error, error} = Bamboo.Mailer.deliver_later(email)
assert String.contains?(error, "cannot call Bamboo.Mailer")
end

defp new_email(attrs \\ []) do
Expand Down

0 comments on commit 835c511

Please sign in to comment.