diff --git a/lib/bamboo/adapters/mailgun_adapter.ex b/lib/bamboo/adapters/mailgun_adapter.ex index 77591473..b346065f 100644 --- a/lib/bamboo/adapters/mailgun_adapter.ex +++ b/lib/bamboo/adapters/mailgun_adapter.ex @@ -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 diff --git a/test/lib/bamboo/adapters/mailgun_adapter_test.exs b/test/lib/bamboo/adapters/mailgun_adapter_test.exs index d7438388..72f3f4f9 100644 --- a/test/lib/bamboo/adapters/mailgun_adapter_test.exs +++ b/test/lib/bamboo/adapters/mailgun_adapter_test.exs @@ -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( @@ -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