Skip to content

Commit 795d4b4

Browse files
committed
wip - dry-up Test.maybe_collect/2
1 parent be32e71 commit 795d4b4

1 file changed

Lines changed: 12 additions & 36 deletions

File tree

lib/sentry/test.ex

Lines changed: 12 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -84,55 +84,31 @@ defmodule Sentry.Test do
8484
@doc false
8585
@spec maybe_collect(Sentry.Event.t()) :: :collected | :not_collecting
8686
def maybe_collect(%Sentry.Event{} = event) do
87-
if Sentry.Config.test_mode?() do
88-
dsn_set? = not is_nil(Sentry.Config.dsn())
89-
ensure_ownership_server_started()
90-
91-
case NimbleOwnership.fetch_owner(@server, callers(), @events_key) do
92-
{:ok, owner_pid} ->
93-
result =
94-
NimbleOwnership.get_and_update(@server, owner_pid, @events_key, fn events ->
95-
{:collected, (events || []) ++ [event]}
96-
end)
97-
98-
case result do
99-
{:ok, :collected} ->
100-
:collected
101-
102-
{:error, error} ->
103-
raise ArgumentError, "cannot collect Sentry reports: #{Exception.message(error)}"
104-
end
105-
106-
:error when dsn_set? ->
107-
:not_collecting
108-
109-
# If the :dsn option is not set and we didn't capture the event, it's alright,
110-
# we can just swallow it.
111-
:error ->
112-
:collected
113-
end
114-
else
115-
:not_collecting
116-
end
87+
maybe_collect(event, @events_key)
11788
end
11889

11990
# Used internally when reporting a transaction, *before* reporting the actual transaction.
12091
@doc false
12192
@spec maybe_collect(Sentry.Transaction.t()) :: :collected | :not_collecting
12293
def maybe_collect(%Sentry.Transaction{} = transaction) do
94+
maybe_collect(transaction, @transactions_key)
95+
end
96+
97+
@doc false
98+
def maybe_collect(item, collection_key) do
12399
if Sentry.Config.test_mode?() do
124100
dsn_set? = not is_nil(Sentry.Config.dsn())
125101
ensure_ownership_server_started()
126102

127-
case NimbleOwnership.fetch_owner(@server, callers(), @transactions_key) do
103+
case NimbleOwnership.fetch_owner(@server, callers(), collection_key) do
128104
{:ok, owner_pid} ->
129105
result =
130106
NimbleOwnership.get_and_update(
131107
@server,
132108
owner_pid,
133-
@transactions_key,
134-
fn transactions ->
135-
{:collected, (transactions || []) ++ [transaction]}
109+
collection_key,
110+
fn items ->
111+
{:collected, (items || []) ++ [item]}
136112
end
137113
)
138114

@@ -142,13 +118,13 @@ defmodule Sentry.Test do
142118

143119
{:error, error} ->
144120
raise ArgumentError,
145-
"cannot collect Sentry transactions: #{Exception.message(error)}"
121+
"cannot collect Sentry #{collection_key}: #{Exception.message(error)}"
146122
end
147123

148124
:error when dsn_set? ->
149125
:not_collecting
150126

151-
# If the :dsn option is not set and we didn't capture the transaction, it's alright,
127+
# If the :dsn option is not set and we didn't capture the item, it's alright,
152128
# we can just swallow it.
153129
:error ->
154130
:collected

0 commit comments

Comments
 (0)