Skip to content

Commit

Permalink
Update Mailgun.deliver to return ok/error tuples (#573)
Browse files Browse the repository at this point in the history
What changed?
==============

Updates Mailgin.deliver function to return ok/error tuples to abide by
the new api.

Note on raising configuration errors
-----------------------------------

We do not change the raising of errors when they deal with configuration
options. Those errors are categorically different. We want to return
ok/error tuples when there's an error building/delivering email. If
a user of Bamboo is missing a configuration variable, it seems important
to raise an error.
  • Loading branch information
germsvel committed Feb 19, 2021
1 parent 6b68fca commit 4e7b5fa
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 15 deletions.
6 changes: 3 additions & 3 deletions lib/bamboo/adapters/mailgun_adapter.ex
Original file line number Diff line number Diff line change
Expand Up @@ -104,13 +104,13 @@ defmodule Bamboo.MailgunAdapter do
) do
{:ok, status, _headers, response} when status > 299 ->
body = decode_body(body)
raise_api_error(@service_name, response, body)
{:error, build_api_error(@service_name, response, body)}

{:ok, status, headers, response} ->
%{status_code: status, headers: headers, body: response}
{:ok, %{status_code: status, headers: headers, body: response}}

{:error, reason} ->
raise_api_error(inspect(reason))
{:error, build_api_error(inspect(reason))}
end
end

Expand Down
26 changes: 14 additions & 12 deletions test/lib/bamboo/adapters/mailgun_adapter_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,12 @@ defmodule Bamboo.MailgunAdapterTest do
assert request_path == "/test.tt/messages"
end

test "deliver/2 returns an {:ok, response} tuple" do
{:ok, response} = new_email() |> MailgunAdapter.deliver(@config)

assert %{status_code: 200, headers: _, body: _} = response
end

test "deliver/2 sends from, subject, text body, html body, headers, custom vars and recipient variables" do
email =
new_email(
Expand Down Expand Up @@ -293,28 +299,24 @@ defmodule Bamboo.MailgunAdapterTest do
assert params["t:text"] == "yes"
end

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

assert_raise Bamboo.ApiError,
~r/.*%{.*\"from\" => \"INVALID_EMAIL\".*}/,
fn ->
email |> MailgunAdapter.deliver(@config)
end
{:error, %Bamboo.ApiError{} = error} = email |> MailgunAdapter.deliver(@config)

assert error.message =~ ~r/.*%{.*\"from\" => \"INVALID_EMAIL\".*}/
end

test "raises if the response is not a success with attachment" do
test "returns an error if the response is not a success with attachment" do
attachment_source_path = Path.join(__DIR__, "../../../support/attachment.txt")

email =
new_email(from: "INVALID_EMAIL")
|> Email.put_attachment(attachment_source_path)

assert_raise Bamboo.ApiError,
~r/.*{.*\"from\", \"INVALID_EMAIL\".*}/,
fn ->
email |> MailgunAdapter.deliver(@config)
end
{:error, %Bamboo.ApiError{} = error} = email |> MailgunAdapter.deliver(@config)

assert error.message =~ ~r/.*{.*\"from\", \"INVALID_EMAIL\".*}/
end

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

0 comments on commit 4e7b5fa

Please sign in to comment.