Elixir/Ecto find_or_create with custom error
I think it is a perfect fit for with. It will be something like:

def find_or_create_user(attrs \\ %{}) do
  with  true &lt;- String.ends_with?(attrs.email, "@test.com"),
        nil &lt;- Repo.get_by(User, %{email: attrs.email}),
        {:ok, user} &lt;- create_user(attrs)
  do
    {:ok, user}
  else
    false -&gt; {:error, :invalid_email_domain}
    %User{} = user -&gt; {:ok, user}
    {:error, changeset} -&gt; {:error, changeset}
    _anything_else -&gt; {:error, "cannot find or create user"}
  end
end