Skip to content

Commit 8d0cd94

Browse files
committed
No longer raise if logging raises
1 parent 5e35baf commit 8d0cd94

File tree

3 files changed

+32
-43
lines changed

3 files changed

+32
-43
lines changed

integration_test/cases/prepare_stream_test.exs

Lines changed: 0 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -357,38 +357,4 @@ defmodule PrepareStreamTest do
357357
handle_close: [%Q{}, _, :state2],
358358
handle_commit: [_, :new_state2]] = A.record(agent)
359359
end
360-
361-
test "prepare_stream declare log raises and deallocates" do
362-
stack = [
363-
{:ok, :state},
364-
{:ok, :began, :new_state},
365-
{:ok, %Q{}, :newer_state},
366-
{:ok, %C{}, :newest_state},
367-
{:ok, :deallocated, :state2},
368-
{:ok, :commited, :new_state2}
369-
]
370-
{:ok, agent} = A.start_link(stack)
371-
372-
parent = self()
373-
opts = [agent: agent, parent: parent]
374-
{:ok, pool} = P.start_link(opts)
375-
376-
Process.flag(:trap_exit, true)
377-
assert P.transaction(pool, fn(conn) ->
378-
opts2 = [log: fn(_) -> raise "oops" end]
379-
stream = P.prepare_stream(conn, %Q{}, [:param], opts2)
380-
assert_raise RuntimeError, "oops", fn() -> Enum.to_list(stream) end
381-
:hi
382-
end) == {:ok, :hi}
383-
384-
assert [
385-
connect: [_],
386-
handle_begin: [_, :state],
387-
handle_prepare: [%Q{}, _, :new_state],
388-
handle_declare: [%Q{}, [:param], _, :newer_state],
389-
handle_deallocate: [%Q{}, %C{}, _, :newest_state],
390-
handle_commit: [_, :state2]
391-
] = A.record(agent)
392-
end
393-
394360
end

integration_test/cases/stream_test.exs

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
defmodule StreamTest do
22
use ExUnit.Case, async: true
3+
import ExUnit.CaptureLog
34

45
alias TestPool, as: P
56
alias TestAgent, as: A
@@ -401,34 +402,37 @@ defmodule StreamTest do
401402
{:handle_declare, [%Q{}, [:param], _, :new_state]} | _] = A.record(agent)
402403
end
403404

404-
test "stream declare log raises and deallocates" do
405+
test "stream declare log raises and continues" do
405406
stack = [
406407
{:ok, :state},
407408
{:ok, :began, :new_state},
408409
{:ok, %C{}, :newer_state},
409-
{:ok, :deallocated, :newest_state},
410-
{:ok, :commited, :state2}
410+
{:deallocate, %R{}, :newest_state},
411+
{:ok, :deallocated, :state2},
412+
{:ok, :commited, :new_state2}
411413
]
412414
{:ok, agent} = A.start_link(stack)
413415

414416
parent = self()
415417
opts = [agent: agent, parent: parent]
416418
{:ok, pool} = P.start_link(opts)
417419

418-
Process.flag(:trap_exit, true)
419420
assert P.transaction(pool, fn(conn) ->
420-
opts2 = [log: fn(_) -> raise "oops" end]
421+
opts2 = [log: fn(_) -> raise "logging oops" end]
421422
stream = P.stream(conn, %Q{}, [:param], opts2)
422-
assert_raise RuntimeError, "oops", fn() -> Enum.to_list(stream) end
423+
assert capture_log(fn() ->
424+
assert Enum.to_list(stream) == [%R{}]
425+
end) =~ "logging oops"
423426
:hi
424427
end) == {:ok, :hi}
425428

426429
assert [
427430
connect: [_],
428431
handle_begin: [_, :state],
429432
handle_declare: [%Q{}, [:param], _, :new_state],
430-
handle_deallocate: [%Q{}, %C{}, _, :newer_state],
431-
handle_commit: [_, :newest_state]
433+
handle_first: [%Q{}, %C{}, _, :newer_state],
434+
handle_deallocate: [%Q{}, %C{}, _, :newest_state],
435+
handle_commit: [_, :state2]
432436
] = A.record(agent)
433437
end
434438

lib/db_connection.ex

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,8 @@ defmodule DBConnection do
7272
`run/3` or `transaction/3` fun and using the run/transaction
7373
connection reference (`t`).
7474
"""
75+
require Logger
76+
7577
defstruct [:pool_mod, :pool_ref, :conn_mod, :conn_ref]
7678

7779
@typedoc """
@@ -1189,7 +1191,13 @@ defmodule DBConnection do
11891191

11901192
defp log(call, query, params, log, times, result) do
11911193
entry = DBConnection.LogEntry.new(call, query, params, times, entry_result(result))
1192-
log(log, entry)
1194+
try do
1195+
log(log, entry)
1196+
catch
1197+
kind, reason ->
1198+
stack = System.stacktrace()
1199+
log_failed(entry, kind, reason, stack)
1200+
end
11931201
log_result(result)
11941202
end
11951203

@@ -1208,6 +1216,17 @@ defmodule DBConnection do
12081216
end
12091217
defp log_result(other), do: other
12101218

1219+
defp log_failed(entry, kind, reason, stack) do
1220+
try do
1221+
Logger.error(fn() ->
1222+
"an exception was raised logging #{inspect entry}: " <> Exception.format(kind, reason, stack)
1223+
end)
1224+
catch
1225+
_, _ ->
1226+
:ok
1227+
end
1228+
end
1229+
12111230
defp run_begin(conn, fun, opts) do
12121231
try do
12131232
fun.(conn)

0 commit comments

Comments
 (0)