From 835c511902294d1acacb76a766de4738ab8121c9 Mon Sep 17 00:00:00 2001 From: brandon paolin Date: Wed, 30 May 2018 15:51:51 -0400 Subject: [PATCH] don't raise when calling deliver/2 or deliver_later/2 --- lib/bamboo/adapters/mailgun_adapter.ex | 10 +++------- lib/bamboo/adapters/mandrill_adapter.ex | 12 +++--------- lib/bamboo/adapters/send_grid_adapter.ex | 11 +++-------- lib/bamboo/mailer.ex | 8 ++++---- mix.lock | 10 ++++++---- test/lib/bamboo/adapters/mailgun_adapter_test.exs | 7 +++---- .../lib/bamboo/adapters/mandrill_adapter_test.exs | 15 +++------------ .../bamboo/adapters/send_grid_adapter_test.exs | 15 +++------------ test/lib/bamboo/mailer_test.exs | 15 ++++++--------- 9 files changed, 34 insertions(+), 69 deletions(-) diff --git a/lib/bamboo/adapters/mailgun_adapter.ex b/lib/bamboo/adapters/mailgun_adapter.ex index 4265ffbe..76c4edb0 100644 --- a/lib/bamboo/adapters/mailgun_adapter.ex +++ b/lib/bamboo/adapters/mailgun_adapter.ex @@ -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 @@ -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 diff --git a/lib/bamboo/adapters/mandrill_adapter.ex b/lib/bamboo/adapters/mandrill_adapter.ex index acb85db9..59092fcb 100644 --- a/lib/bamboo/adapters/mandrill_adapter.ex +++ b/lib/bamboo/adapters/mandrill_adapter.ex @@ -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 diff --git a/lib/bamboo/adapters/send_grid_adapter.ex b/lib/bamboo/adapters/send_grid_adapter.ex index 34639ef7..d188af40 100644 --- a/lib/bamboo/adapters/send_grid_adapter.ex +++ b/lib/bamboo/adapters/send_grid_adapter.ex @@ -27,13 +27,11 @@ 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) @@ -41,13 +39,10 @@ defmodule Bamboo.SendGridAdapter do 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 diff --git a/lib/bamboo/mailer.ex b/lib/bamboo/mailer.ex index fae70d22..703d22eb 100644 --- a/lib/bamboo/mailer.ex +++ b/lib/bamboo/mailer.ex @@ -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 """ @@ -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 @@ -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 @@ -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 diff --git a/mix.lock b/mix.lock index 1e9b032c..94196d9e 100644 --- a/mix.lock +++ b/mix.lock @@ -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"}, @@ -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"}, @@ -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"}, +} diff --git a/test/lib/bamboo/adapters/mailgun_adapter_test.exs b/test/lib/bamboo/adapters/mailgun_adapter_test.exs index 763da506..1d875363 100644 --- a/test/lib/bamboo/adapters/mailgun_adapter_test.exs +++ b/test/lib/bamboo/adapters/mailgun_adapter_test.exs @@ -154,12 +154,11 @@ defmodule Bamboo.MailgunAdapterTest do assert params["bcc"] == "BCC " 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 diff --git a/test/lib/bamboo/adapters/mandrill_adapter_test.exs b/test/lib/bamboo/adapters/mandrill_adapter_test.exs index 4d9537ff..e3daa1fb 100644 --- a/test/lib/bamboo/adapters/mandrill_adapter_test.exs +++ b/test/lib/bamboo/adapters/mandrill_adapter_test.exs @@ -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 diff --git a/test/lib/bamboo/adapters/send_grid_adapter_test.exs b/test/lib/bamboo/adapters/send_grid_adapter_test.exs index 7861e078..380577d0 100644 --- a/test/lib/bamboo/adapters/send_grid_adapter_test.exs +++ b/test/lib/bamboo/adapters/send_grid_adapter_test.exs @@ -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 diff --git a/test/lib/bamboo/mailer_test.exs b/test/lib/bamboo/mailer_test.exs index 54d711dd..28e6fc35 100644 --- a/test/lib/bamboo/mailer_test.exs +++ b/test/lib/bamboo/mailer_test.exs @@ -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 @@ -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