Skip to content

Commit 2665095

Browse files
committed
Put a label (like Repo name) in these errors
The label must be passed in - see elixir-ecto/ecto_sql#698 Assuming it's the repo name, this is helpful if the application has more than one repo (eg read replicas). For example: Before: "** (DBConnection.ConnectionError) connection is closed because of an error, disconnect or timeout" After: "** (DBConnection.ConnectionError) MyApp.Repo connection is closed because of an error, disconnect or timeout"
1 parent 79143bb commit 2665095

File tree

2 files changed

+29
-8
lines changed

2 files changed

+29
-8
lines changed

lib/db_connection/holder.ex

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -137,25 +137,43 @@ defmodule DBConnection.Holder do
137137
:ets.lookup(holder, :conn)
138138
rescue
139139
ArgumentError ->
140-
msg = "connection is closed because of an error, disconnect or timeout"
140+
msg =
141+
maybe_prefix_label(
142+
"connection is closed because of an error, disconnect or timeout",
143+
opts
144+
)
145+
141146
{:disconnect, DBConnection.ConnectionError.exception(msg), _state = :unused}
142147
else
143148
[conn(lock: conn_lock)] when conn_lock != lock ->
144-
raise "an outdated connection has been given to DBConnection on #{fun}/#{length(args) + 2}"
149+
raise maybe_prefix_label(
150+
"an outdated connection has been given to DBConnection on #{fun}/#{length(args) + 2}",
151+
opts,
152+
":"
153+
)
145154

146155
[conn(status: :error)] ->
147-
msg = "connection is closed because of an error, disconnect or timeout"
156+
msg =
157+
maybe_prefix_label(
158+
"connection is closed because of an error, disconnect or timeout",
159+
opts
160+
)
161+
148162
{:disconnect, DBConnection.ConnectionError.exception(msg), _state = :unused}
149163

150164
[conn(status: :aborted)] when type != :cleanup ->
151-
msg = "transaction rolling back"
165+
msg = maybe_prefix_label("transaction rolling back", opts)
152166
{:disconnect, DBConnection.ConnectionError.exception(msg), _state = :unused}
153167

154168
[conn(module: module, state: state)] ->
155169
holder_apply(holder, module, fun, args ++ [opts, state])
156170
end
157171
end
158172

173+
defp maybe_prefix_label(msg, opts, separator \\ "") do
174+
if opts[:label], do: "#{inspect(opts[:label])} " <> separator <> msg, else: msg
175+
end
176+
159177
## Pool state helpers API (invoked by callers)
160178

161179
@spec put_state(pool_ref :: any, term) :: :ok

lib/db_connection/ownership/manager.ex

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,8 @@ defmodule DBConnection.Ownership.Manager do
107107
mode: mode,
108108
mode_ref: nil,
109109
ets: ets,
110-
log: log
110+
log: log,
111+
label: pool_opts[:label]
111112
}}
112113
end
113114

@@ -223,7 +224,7 @@ defmodule DBConnection.Ownership.Manager do
223224
{:noreply, state}
224225

225226
:not_found when mode == :manual ->
226-
not_found(from, mode)
227+
not_found(from, mode, state.label)
227228
{:noreply, state}
228229

229230
:not_found ->
@@ -405,10 +406,12 @@ defmodule DBConnection.Ownership.Manager do
405406
caller
406407
end
407408

408-
defp not_found({pid, _} = from, mode) do
409+
defp not_found({pid, _} = from, mode, label) do
409410
msg = """
410411
cannot find ownership process for #{Util.inspect_pid(pid)}
411-
using mode #{inspect(mode)}.
412+
#{if label, do: "(#{inspect(label)})"} using mode #{inspect(mode)}.
413+
(Note that a connection's mode reverts to :manual if its owner
414+
terminates.)
412415
413416
When using ownership, you must manage connections in one
414417
of the four ways:

0 commit comments

Comments
 (0)