Skip to content

Commit 7f71719

Browse files
committed
Put the repo name in these errors
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 2512738 commit 7f71719

File tree

1 file changed

+22
-4
lines changed

1 file changed

+22
-4
lines changed

lib/db_connection/holder.ex

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -135,25 +135,43 @@ defmodule DBConnection.Holder do
135135
:ets.lookup(holder, :conn)
136136
rescue
137137
ArgumentError ->
138-
msg = "connection is closed because of an error, disconnect or timeout"
138+
msg =
139+
maybe_prefix_repo(
140+
"connection is closed because of an error, disconnect or timeout",
141+
opts
142+
)
143+
139144
{:disconnect, DBConnection.ConnectionError.exception(msg), _state = :unused}
140145
else
141146
[conn(lock: conn_lock)] when conn_lock != lock ->
142-
raise "an outdated connection has been given to DBConnection on #{fun}/#{length(args) + 2}"
147+
raise maybe_prefix_repo(
148+
"an outdated connection has been given to DBConnection on #{fun}/#{length(args) + 2}",
149+
opts,
150+
":"
151+
)
143152

144153
[conn(status: :error)] ->
145-
msg = "connection is closed because of an error, disconnect or timeout"
154+
msg =
155+
maybe_prefix_repo(
156+
"connection is closed because of an error, disconnect or timeout",
157+
opts
158+
)
159+
146160
{:disconnect, DBConnection.ConnectionError.exception(msg), _state = :unused}
147161

148162
[conn(status: :aborted)] when type != :cleanup ->
149-
msg = "transaction rolling back"
163+
msg = maybe_prefix_repo("transaction rolling back", opts)
150164
{:disconnect, DBConnection.ConnectionError.exception(msg), _state = :unused}
151165

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

171+
defp maybe_prefix_repo(msg, opts, separator \\ "") do
172+
if opts[:repo], do: "#{inspect(opts[:repo])} " <> separator <> msg, else: msg
173+
end
174+
157175
## Pool state helpers API (invoked by callers)
158176

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

0 commit comments

Comments
 (0)