From 639b8fb7432dcf86721df3236ed1720c1f26cb1c Mon Sep 17 00:00:00 2001 From: German Velasco Date: Wed, 23 Dec 2020 15:40:25 -0500 Subject: [PATCH] Update Mandrill to return ok/error tuples 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. --- lib/bamboo/adapters/mandrill_adapter.ex | 6 +++--- .../bamboo/adapters/mandrill_adapter_test.exs | 20 ++++++++++++------- 2 files changed, 16 insertions(+), 10 deletions(-) diff --git a/lib/bamboo/adapters/mandrill_adapter.ex b/lib/bamboo/adapters/mandrill_adapter.ex index 4d5caedc..38413cb7 100644 --- a/lib/bamboo/adapters/mandrill_adapter.ex +++ b/lib/bamboo/adapters/mandrill_adapter.ex @@ -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 diff --git a/test/lib/bamboo/adapters/mandrill_adapter_test.exs b/test/lib/bamboo/adapters/mandrill_adapter_test.exs index acc8888f..1f9abda3 100644 --- a/test/lib/bamboo/adapters/mandrill_adapter_test.exs +++ b/test/lib/bamboo/adapters/mandrill_adapter_test.exs @@ -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) @@ -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