Skip to content

Commit 79a2363

Browse files
committed
Move add expectation error handling
1 parent 2ddd80c commit 79a2363

File tree

1 file changed

+34
-30
lines changed

1 file changed

+34
-30
lines changed

lib/mox.ex

+34-30
Original file line numberDiff line numberDiff line change
@@ -685,11 +685,8 @@ defmodule Mox do
685685
:ok ->
686686
:ok
687687

688-
{:error, error} when is_binary(error) ->
689-
raise ArgumentError, error
690-
691688
{:error, error} ->
692-
raise error
689+
raise_cannot_add_expectation!(error, mock)
693690
end
694691
end
695692

@@ -700,14 +697,41 @@ defmodule Mox do
700697
:ok ->
701698
:ok
702699

703-
{:error, error} when is_binary(error) ->
704-
raise ArgumentError, error
705-
706700
{:error, error} ->
707-
raise error
701+
raise_cannot_add_expectation!(error, mock)
708702
end
709703
end
710704

705+
defp raise_cannot_add_expectation!(
706+
%NimbleOwnership.Error{reason: {:already_allowed, owner_pid}},
707+
mock
708+
) do
709+
inspected = inspect(self())
710+
711+
raise ArgumentError, """
712+
cannot add expectations/stubs to #{inspect(mock)} in the current process (#{inspected}) \
713+
because the process has been allowed by #{inspect(owner_pid)}. \
714+
You cannot define expectations/stubs in a process that has been allowed
715+
"""
716+
end
717+
718+
defp raise_cannot_add_expectation!(
719+
%NimbleOwnership.Error{reason: {:not_shared_owner, global_pid}},
720+
mock
721+
) do
722+
inspected = inspect(self())
723+
724+
raise ArgumentError, """
725+
cannot add expectations/stubs to #{inspect(mock)} in the current process (#{inspected}) \
726+
because Mox is in global mode and the global process is #{inspect(global_pid)}. \
727+
Only the process that set Mox to global can set expectations/stubs in global mode
728+
"""
729+
end
730+
731+
defp raise_cannot_add_expectation!(error, _mock) do
732+
raise error
733+
end
734+
711735
@doc """
712736
Allows other processes to share expectations and stubs
713737
defined by owner process.
@@ -937,28 +961,8 @@ defmodule Mox do
937961
update_fun = &{:ok, init_or_merge_expectations(&1, key_expectation_list)}
938962

939963
case get_and_update(owner_pid, mock, update_fun) do
940-
{:ok, value} ->
941-
value
942-
943-
{:error, %NimbleOwnership.Error{reason: {:already_allowed, _}}} ->
944-
inspected = inspect(self())
945-
946-
{:error,
947-
"""
948-
cannot add expectations/stubs to #{inspect(mock)} in the current process (#{inspected}) \
949-
because the process has been allowed by #{inspect(owner_pid)}. \
950-
You cannot define expectations/stubs in a process that has been allowed
951-
"""}
952-
953-
{:error, %NimbleOwnership.Error{reason: {:not_shared_owner, global_pid}}} ->
954-
inspected = inspect(self())
955-
956-
{:error,
957-
"""
958-
cannot add expectations/stubs to #{inspect(mock)} in the current process (#{inspected}) \
959-
because Mox is in global mode and the global process is #{inspect(global_pid)}. \
960-
Only the process that set Mox to global can set expectations/stubs in global mode
961-
"""}
964+
{:ok, _value} ->
965+
:ok
962966

963967
{:error, error} ->
964968
{:error, error}

0 commit comments

Comments
 (0)