Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Wrap errors in SMTPError to comply with Adapter.deliver/2 spec #216

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 7 additions & 4 deletions lib/bamboo/adapters/smtp_adapter.ex
Original file line number Diff line number Diff line change
Expand Up @@ -104,15 +104,18 @@ defmodule Bamboo.SMTPAdapter do
def supports_attachments?, do: true

defp handle_response({:error, :no_credentials}) do
{:error, "Username and password were not provided for authentication."}
{:error,
SMTPError.exception(
{:no_credentials, "Username and password were not provided for authentication."}
)}
end

defp handle_response({:error, _reason, detail}) do
{:error, detail}
defp handle_response({:error, reason, detail}) do
{:error, SMTPError.exception({reason, detail})}
end

defp handle_response({:error, detail}) do
{:error, detail}
{:error, SMTPError.exception({:not_specified, detail})}
end

defp handle_response(response) do
Expand Down
30 changes: 22 additions & 8 deletions test/lib/bamboo/adapters/smtp_adapter_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ defmodule Bamboo.SMTPAdapterTest do

alias Bamboo.Email
alias Bamboo.SMTPAdapter
alias Bamboo.SMTPAdapter.SMTPError

defmodule FakeGenSMTP do
use GenServer
Expand Down Expand Up @@ -57,7 +58,7 @@ defmodule Bamboo.SMTPAdapterTest do
defp check_configuration(config) do
case Keyword.fetch(config, :relay) do
{:ok, wrong_domain = "wrong.smtp.domain"} ->
{:error, wrong_domain}
{:error, :retries_exceeded, {:network_failure, wrong_domain, {:error, :nxdomain}}}

_ ->
:ok
Expand All @@ -67,7 +68,9 @@ defmodule Bamboo.SMTPAdapterTest do
defp check_email({from, _to, _raw}) do
case from do
"wrong@user.com" ->
{:error, "554 Message rejected: Email address is not verified."}
{:error, :no_more_hosts,
{:permanent_failure, "an-smtp-adddress",
"554 Message rejected: Email address is not verified.\r\n"}}

_ ->
:ok
Expand Down Expand Up @@ -344,8 +347,11 @@ defmodule Bamboo.SMTPAdapterTest do
auth: :always
})

assert {:error, "Username and password were not provided for authentication."} =
SMTPAdapter.deliver(bamboo_email, bamboo_config)
assert {:error,
%SMTPError{
raw:
{:no_credentials, "Username and password were not provided for authentication."}
}} = SMTPAdapter.deliver(bamboo_email, bamboo_config)
end

test "deliver is successful when username and password are required and present" do
Expand Down Expand Up @@ -378,7 +384,10 @@ defmodule Bamboo.SMTPAdapterTest do
bamboo_email = new_email()
bamboo_config = configuration(%{server: "wrong.smtp.domain"})

{:error, "wrong.smtp.domain"} = SMTPAdapter.deliver(bamboo_email, bamboo_config)
{:error,
%SMTPError{
raw: {:retries_exceeded, {:network_failure, "wrong.smtp.domain", {:error, :nxdomain}}}
}} = SMTPAdapter.deliver(bamboo_email, bamboo_config)
end

test "sets default auth key if not present" do
Expand Down Expand Up @@ -417,8 +426,13 @@ defmodule Bamboo.SMTPAdapterTest do
bamboo_email = new_email(from: {"Wrong User", "wrong@user.com"})
bamboo_config = configuration()

{:error, "554 Message rejected: Email address is not verified."} =
SMTPAdapter.deliver(bamboo_email, bamboo_config)
{:error,
%SMTPError{
raw:
{:no_more_hosts,
{:permanent_failure, "an-smtp-adddress",
"554 Message rejected: Email address is not verified.\r\n"}}
}} = SMTPAdapter.deliver(bamboo_email, bamboo_config)
end

test "emails looks fine when only text body is set" do
Expand Down Expand Up @@ -794,6 +808,7 @@ defmodule Bamboo.SMTPAdapterTest do
bamboo_email =
@email_in_utf8
|> new_email()

bamboo_config = configuration()
{:ok, "200 Ok 1234567890"} = SMTPAdapter.deliver(bamboo_email, bamboo_config)
[{{from, to, _raw_email}, _gen_smtp_config}] = FakeGenSMTP.fetch_sent_emails()
Expand All @@ -804,7 +819,6 @@ defmodule Bamboo.SMTPAdapterTest do
assert Enum.member?(to, "mary@major.com")
end


defp format_email(emails), do: format_email(emails, true)

defp format_email({name, email}, true), do: "#{rfc822_encode(name)} <#{email}>"
Expand Down