Skip to content

Commit dd943d9

Browse files
committed
Sentry.capture_exception only accepts Exception.t()
1 parent 69668e5 commit dd943d9

File tree

7 files changed

+18
-66
lines changed

7 files changed

+18
-66
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
* Remove Sentry.Plug and Sentry.Phoenix.Endpoint in favor of Sentry.PlugContext and Sentry.PlugCapture (#402)
1919
* Remove feedback form rendering and configuration (#402)
2020
* Logger metadata is now specified by key in LoggerBackend instead of enabled/disabled
21+
* `Sentry.capture_exception/1` now only accepts exceptions
2122

2223
## 7.2.4 (2020-03-09)
2324

lib/sentry.ex

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ defmodule Sentry do
112112
Parses and submits an exception to Sentry if current environment is in included_environments.
113113
`opts` argument is passed as the second argument to `Sentry.send_event/2`.
114114
"""
115-
@spec capture_exception(Exception.t() | atom() | {atom(), atom()}, Keyword.t()) :: send_result
115+
@spec capture_exception(Exception.t(), Keyword.t()) :: send_result
116116
def capture_exception(exception, opts \\ []) do
117117
filter_module = Config.filter()
118118
event_source = Keyword.get(opts, :event_source)

lib/sentry/event.ex

Lines changed: 8 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -73,8 +73,8 @@ defmodule Sentry.Event do
7373
@doc """
7474
Creates an Event struct out of context collected and options
7575
## Options
76-
* `:exception` - Sentry-formatted exception
77-
* `:original_exception` - Original exception
76+
* `:exception` - Sentry-structured exception
77+
* `:original_exception` - Original Elixir exception struct
7878
* `:message` - message
7979
* `:stacktrace` - a list of Exception.stacktrace()
8080
* `:extra` - map of extra context
@@ -175,34 +175,18 @@ defmodule Sentry.Event do
175175
176176
"""
177177
@spec transform_exception(Exception.t(), keyword()) :: Event.t()
178-
def transform_exception(exception, opts) do
179-
error_type = Keyword.get(opts, :error_type) || :error
180-
normalized = Exception.normalize(:error, exception, Keyword.get(opts, :stacktrace, nil))
181-
178+
def transform_exception(%_{} = exception, opts) do
182179
type =
183-
if error_type == :error do
184-
normalized.__struct__
185-
|> to_string()
186-
|> String.trim_leading("Elixir.")
187-
else
188-
error_type
189-
end
180+
exception.__struct__
181+
|> to_string()
182+
|> String.trim_leading("Elixir.")
190183

191-
value =
192-
if error_type == :error do
193-
Exception.message(normalized)
194-
else
195-
Exception.format_banner(error_type, exception)
196-
end
184+
value = Exception.message(exception)
197185

198186
module = Keyword.get(opts, :module)
199187
transformed_exception = [%{type: type, value: value, module: module}]
200188

201-
message =
202-
:error
203-
|> Exception.format_banner(normalized)
204-
|> String.trim("*")
205-
|> String.trim()
189+
message = "(#{type} #{value})"
206190

207191
opts
208192
|> Keyword.put(:exception, transformed_exception)

test/event_test.exs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ defmodule Sentry.EventTest do
3232
assert event.level == "error"
3333

3434
assert event.message ==
35-
"(UndefinedFunctionError) function Sentry.Event.not_a_function/3 is undefined or private"
35+
"(UndefinedFunctionError function Sentry.Event.not_a_function/3 is undefined or private)"
3636

3737
assert is_binary(event.server_name)
3838

test/logger_backend_test.exs

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ defmodule Sentry.LoggerBackendTest do
4949
json = Jason.decode!(body)
5050

5151
assert List.first(json["exception"])["value"] ==
52-
~s[Erlang error: {:bad_return_value, "I am throwing"}]
52+
~s[** (exit) bad return value: "I am throwing"]
5353

5454
assert conn.request_path == "/api/1/store/"
5555
assert conn.method == "POST"
@@ -75,8 +75,8 @@ defmodule Sentry.LoggerBackendTest do
7575
Bypass.expect(bypass, fn conn ->
7676
{:ok, body, conn} = Plug.Conn.read_body(conn)
7777
json = Jason.decode!(body)
78-
assert List.first(json["exception"])["type"] == "ErlangError"
79-
assert List.first(json["exception"])["value"] == "Erlang error: :bad_exit"
78+
assert List.first(json["exception"])["type"] == "Sentry.CrashError"
79+
assert List.first(json["exception"])["value"] == "** (exit) :bad_exit"
8080
assert conn.request_path == "/api/1/store/"
8181
assert conn.method == "POST"
8282
send(self_pid, "API called")
@@ -166,11 +166,9 @@ defmodule Sentry.LoggerBackendTest do
166166
{:ok, body, conn} = Plug.Conn.read_body(conn)
167167
json = Jason.decode!(body)
168168

169-
exception_value =
170-
List.first(json["exception"])
171-
|> Map.fetch!("value")
172-
173-
assert String.contains?(exception_value, "{:timeout, {GenServer, :call")
169+
assert List.first(json["exception"])["type"] == "Sentry.CrashError"
170+
assert List.first(json["exception"])["value"] =~ "** (EXIT) time out"
171+
assert List.first(json["exception"])["value"] =~ "GenServer\.call"
174172

175173
assert conn.request_path == "/api/1/store/"
176174
assert conn.method == "POST"

test/plug_capture_test.exs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ defmodule Sentry.PlugCaptureTest do
100100
{:ok, body, conn} = Plug.Conn.read_body(conn)
101101
json = Jason.decode!(body)
102102
assert json["culprit"] == "Sentry.PlugCaptureTest.PhoenixController.error/2"
103-
assert json["message"] == "(RuntimeError) PhoenixError"
103+
assert json["message"] == "(RuntimeError PhoenixError)"
104104
Plug.Conn.resp(conn, 200, ~s<{"id": "340"}>)
105105
end)
106106

test/sentry_test.exs

Lines changed: 0 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -61,37 +61,6 @@ defmodule SentryTest do
6161
Bypass.pass(bypass)
6262
end
6363

64-
test "handles three element tuple as stacktrace" do
65-
bypass = Bypass.open()
66-
67-
Bypass.expect(bypass, fn conn ->
68-
assert conn.request_path == "/api/1/store/"
69-
assert conn.method == "POST"
70-
{:ok, body, conn} = Plug.Conn.read_body(conn)
71-
{:ok, json} = Jason.decode(body)
72-
assert json["culprit"] == "GenServer.call/3"
73-
assert List.first(json["exception"])["value"] == "Erlang error: :timeout"
74-
Plug.Conn.send_resp(conn, 200, ~s<{"id": "340"}>)
75-
end)
76-
77-
modify_env(
78-
:sentry,
79-
dsn: "http://public:secret@localhost:#{bypass.port}/1"
80-
)
81-
82-
Process.flag(:trap_exit, true)
83-
84-
{:ok, pid} = Sentry.TestGenServer.start_link(self())
85-
spawn_pid = spawn_link(fn -> GenServer.call(pid, {:sleep, 100}, 0) end)
86-
87-
receive do
88-
{:EXIT, ^spawn_pid, {reason, stack}} ->
89-
Sentry.capture_exception(reason, stacktrace: stack)
90-
end
91-
92-
Bypass.pass(bypass)
93-
end
94-
9564
test "sets last_event_id_and_source when an event is sent" do
9665
bypass = Bypass.open()
9766

0 commit comments

Comments
 (0)