Skip to content

Commit

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

Update MandrillAdapter to return an ok/error tuple to abide by the new
behaviour api.

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

We no longer raise api errors, but we do raise configuration errors.
Since configuration errors do not fall under the category of email
building/delivery errors, it seems fine that we raise those errors.
  • Loading branch information
germsvel authored Feb 19, 2021
1 parent 4e7b5fa commit ed6cfef
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 10 deletions.
6 changes: 3 additions & 3 deletions lib/bamboo/adapters/mandrill_adapter.ex
Original file line number Diff line number Diff line change
Expand Up @@ -41,13 +41,13 @@ defmodule Bamboo.MandrillAdapter do
filtered_params =
params |> Bamboo.json_library().decode!() |> Map.put("key", "[FILTERED]")

raise_api_error(@service_name, response, filtered_params)
{:error, build_api_error(@service_name, response, filtered_params)}

{: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
20 changes: 13 additions & 7 deletions test/lib/bamboo/adapters/mandrill_adapter_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,12 @@ defmodule Bamboo.MandrillAdapterTest do
assert request_path == "/api/1.0/messages/send.json"
end

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

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

test "deliver/2 sends the to the right url for templates" do
new_email() |> MandrillHelper.template("hello") |> MandrillAdapter.deliver(@config)

Expand Down Expand Up @@ -195,20 +201,20 @@ 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 an error if the response is not a success" do
email = new_email(from: "INVALID_EMAIL")

assert_raise Bamboo.ApiError, fn ->
email |> MandrillAdapter.deliver(@config)
end
{:error, error} = email |> MandrillAdapter.deliver(@config)

assert %Bamboo.ApiError{} = error
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
{:error, error} = email |> MandrillAdapter.deliver(@config)

assert error.message =~ ~r/"key" => "\[FILTERED\]"/
end

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

0 comments on commit ed6cfef

Please sign in to comment.