Skip to content

Commit ab02160

Browse files
authored
Wrap HTTP requests in try/catch (#515)
1 parent 5116d84 commit ab02160

File tree

6 files changed

+76
-2
lines changed

6 files changed

+76
-2
lines changed

lib/sentry/client.ex

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -216,6 +216,8 @@ defmodule Sentry.Client do
216216
e ->
217217
{:error, e}
218218
end
219+
catch
220+
kind, data -> {:error, {kind, data, __STACKTRACE__}}
219221
end
220222

221223
@doc """
@@ -358,7 +360,14 @@ defmodule Sentry.Client do
358360
"Unable to encode JSON Sentry error - #{inspect(error)}"
359361

360362
{:error, {:request_failure, last_error}} ->
361-
"Error in HTTP Request to Sentry - #{inspect(last_error)}"
363+
case last_error do
364+
{kind, data, stacktrace}
365+
when kind in [:exit, :throw, :error] and is_list(stacktrace) ->
366+
Exception.format(kind, data, stacktrace)
367+
368+
_other ->
369+
"Error in HTTP Request to Sentry - #{inspect(last_error)}"
370+
end
362371

363372
{:error, error} ->
364373
inspect(error)

mix.exs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,8 @@ defmodule Sentry.Mixfile do
4646
{:ex_doc, "~> 0.29.0", only: :dev},
4747
{:bypass, "~> 2.0", only: [:test]},
4848
{:phoenix, "~> 1.5", only: [:test]},
49-
{:phoenix_html, "~> 2.0", only: [:test]}
49+
{:phoenix_html, "~> 2.0", only: [:test]},
50+
{:mox, "~> 1.0", only: [:test]}
5051
]
5152
end
5253

mix.lock

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
"metrics": {:hex, :metrics, "1.0.1", "25f094dea2cda98213cecc3aeff09e940299d950904393b2a29d191c346a8486", [:rebar3], [], "hexpm", "69b09adddc4f74a40716ae54d140f93beb0fb8978d8636eaded0c31b6f099f16"},
1919
"mime": {:hex, :mime, "1.5.0", "203ef35ef3389aae6d361918bf3f952fa17a09e8e43b5aa592b93eba05d0fb8d", [:mix], [], "hexpm", "55a94c0f552249fc1a3dd9cd2d3ab9de9d3c89b559c2bd01121f824834f24746"},
2020
"mimerl": {:hex, :mimerl, "1.2.0", "67e2d3f571088d5cfd3e550c383094b47159f3eee8ffa08e64106cdf5e981be3", [:rebar3], [], "hexpm", "f278585650aa581986264638ebf698f8bb19df297f66ad91b18910dfc6e19323"},
21+
"mox": {:hex, :mox, "1.0.2", "dc2057289ac478b35760ba74165b4b3f402f68803dd5aecd3bfd19c183815d64", [:mix], [], "hexpm", "f9864921b3aaf763c8741b5b8e6f908f44566f1e427b2630e89e9a73b981fef2"},
2122
"nimble_parsec": {:hex, :nimble_parsec, "1.3.1", "2c54013ecf170e249e9291ed0a62e5832f70a476c61da16f6aac6dca0189f2af", [:mix], [], "hexpm", "2682e3c0b2eb58d90c6375fc0cc30bc7be06f365bf72608804fb9cffa5e1b167"},
2223
"parse_trans": {:hex, :parse_trans, "3.3.1", "16328ab840cc09919bd10dab29e431da3af9e9e7e7e6f0089dd5a2d2820011d8", [:rebar3], [], "hexpm", "07cd9577885f56362d414e8c4c4e6bdf10d43a8767abb92d24cbe8b24c54888b"},
2324
"phoenix": {:hex, :phoenix, "1.5.8", "71cfa7a9bb9a37af4df98939790642f210e35f696b935ca6d9d9c55a884621a4", [:mix], [{:jason, "~> 1.0", [hex: :jason, repo: "hexpm", optional: true]}, {:phoenix_html, "~> 2.13", [hex: :phoenix_html, repo: "hexpm", optional: true]}, {:phoenix_pubsub, "~> 2.0", [hex: :phoenix_pubsub, repo: "hexpm", optional: false]}, {:plug, "~> 1.10", [hex: :plug, repo: "hexpm", optional: false]}, {:plug_cowboy, "~> 1.0 or ~> 2.2", [hex: :plug_cowboy, repo: "hexpm", optional: true]}, {:plug_crypto, "~> 1.1.2 or ~> 1.2", [hex: :plug_crypto, repo: "hexpm", optional: false]}, {:telemetry, "~> 0.4", [hex: :telemetry, repo: "hexpm", optional: false]}], "hexpm", "35ded0a32f4836168c7ab6c33b88822eccd201bcd9492125a9bea4c54332d955"},

test/client_test.exs

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
defmodule Sentry.ClientTest do
22
use ExUnit.Case
33
import ExUnit.CaptureLog
4+
import Mox
45
import Sentry.TestEnvironmentHelper
56
require Logger
67

@@ -354,4 +355,63 @@ defmodule Sentry.ClientTest do
354355
Sentry.capture_message("something happened", extra: %{metadata: [keyword: "list"]})
355356
end) =~ "Failed to send Sentry event. Unable to encode JSON"
356357
end
358+
359+
describe "client handles exits/throws/exceptions in adapters" do
360+
setup :verify_on_exit!
361+
362+
setup do
363+
modify_env(:sentry,
364+
dsn: "http://public:secret@localhost:0/1",
365+
client: Sentry.HTTPClientMock
366+
)
367+
368+
faulty_capture_message = fn failure ->
369+
expect(Sentry.HTTPClientMock, :post, fn _url, _headers, _body -> failure.() end)
370+
{:ok, task} = Sentry.capture_message("all your code are belong to us", result: :async)
371+
Task.await(task)
372+
end
373+
374+
{:ok, faulty_capture_message: faulty_capture_message}
375+
end
376+
377+
test "exits", %{faulty_capture_message: faulty_capture_message} do
378+
log =
379+
capture_log(fn ->
380+
assert {:error, {:request_failure, {:exit, :through_the_window, _stacktrace}}} =
381+
faulty_capture_message.(fn -> exit(:through_the_window) end)
382+
end)
383+
384+
assert log =~ """
385+
Failed to send Sentry event. ** (exit) :through_the_window
386+
test/client_test.exs:\
387+
"""
388+
end
389+
390+
test "throws", %{faulty_capture_message: faulty_capture_message} do
391+
log =
392+
capture_log(fn ->
393+
assert {:error, {:request_failure, {:throw, :catch_me_if_you_can, _stacktrace}}} =
394+
faulty_capture_message.(fn -> throw(:catch_me_if_you_can) end)
395+
end)
396+
397+
assert log =~ """
398+
Failed to send Sentry event. ** (throw) :catch_me_if_you_can
399+
test/client_test.exs:\
400+
"""
401+
end
402+
403+
test "exceptions", %{faulty_capture_message: faulty_capture_message} do
404+
log =
405+
capture_log(fn ->
406+
assert {:error,
407+
{:request_failure, {:error, %RuntimeError{message: "oops"}, _stacktrace}}} =
408+
faulty_capture_message.(fn -> raise "oops" end)
409+
end)
410+
411+
assert log =~ """
412+
Failed to send Sentry event. ** (RuntimeError) oops
413+
test/client_test.exs:\
414+
"""
415+
end
416+
end
357417
end

test/event_test.exs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -267,6 +267,7 @@ defmodule Sentry.EventTest do
267267
:metrics,
268268
:mime,
269269
:mimerl,
270+
:mox,
270271
:parse_trans,
271272
:phoenix,
272273
:phoenix_html,

test/test_helper.exs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ Code.require_file("test/support/test_filter.exs")
55
Code.require_file("test/support/test_gen_server.exs")
66
Code.require_file("test/support/test_error_view.exs")
77

8+
Mox.defmock(Sentry.HTTPClientMock, for: Sentry.HTTPClient)
9+
810
ExUnit.start(assert_receive_timeout: 500)
911

1012
Application.ensure_all_started(:bypass)

0 commit comments

Comments
 (0)