Skip to content

Commit 9f2f6fb

Browse files
authored
Improve Errors kind parsing (#6)
This change expands the `ErrorTracker.Error.new/2` API to allow receiving exceptions in three ways: 1. `{kind, reason}` 2. `{kind, exception}` 3. `exception` If a exception is received (either with a kind or not): - the `kind` field is set to the struct of the exception. - the `reason` field is set to the message of the exception. If a `{kind, reason}` is received: - the `kind` field is set to the `kind` value converted to string. - the `reason` field is set to the `reason` value converted to string. Aside from that, the dev server now includes another route to generate an `EXIT` signal to test this.
1 parent 7bc8c11 commit 9f2f6fb

4 files changed

Lines changed: 27 additions & 9 deletions

File tree

dev.exs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ defmodule ErrorTrackerDevWeb.PageController do
5959
<div><a href="/404">Generate Router 404</a></div>
6060
<div><a href="/noroute">Raise NoRouteError from a controller</a></div>
6161
<div><a href="/exception">Generate Exception</a></div>
62+
<div><a href="/exit">Generate Exit</a></div>
6263
""")
6364
end
6465

@@ -70,6 +71,10 @@ defmodule ErrorTrackerDevWeb.PageController do
7071
raise "This is a controller exception"
7172
end
7273

74+
def call(_conn, :exit) do
75+
exit(:timeout)
76+
end
77+
7378
defp content(conn, content) do
7479
conn
7580
|> put_resp_header("content-type", "text/html")
@@ -100,6 +105,7 @@ defmodule ErrorTrackerDevWeb.Router do
100105
get "/", ErrorTrackerDevWeb.PageController, :index
101106
get "/noroute", ErrorTrackerDevWeb.PageController, :noroute
102107
get "/exception", ErrorTrackerDevWeb.PageController, :exception
108+
get "/exit", ErrorTrackerDevWeb.PageController, :exit
103109
end
104110
end
105111

lib/error_tracker/integrations/phoenix.ex

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -27,15 +27,15 @@ defmodule ErrorTracker.Integrations.Phoenix do
2727
end
2828

2929
def handle_event([:phoenix, :router_dispatch, :exception], _measurements, metadata, :no_config) do
30-
{reason, stack} =
30+
{reason, kind, stack} =
3131
case metadata do
32-
%{reason: %Plug.Conn.WrapperError{reason: reason, stack: stack}} ->
33-
{reason, stack}
32+
%{reason: %Plug.Conn.WrapperError{reason: reason, kind: kind, stack: stack}} ->
33+
{reason, kind, stack}
3434

35-
%{reason: reason, stacktrace: stack} ->
36-
{reason, stack}
35+
%{kind: kind, reason: reason, stacktrace: stack} ->
36+
{reason, kind, stack}
3737
end
3838

39-
PlugIntegration.report_error(metadata.conn, reason, stack)
39+
PlugIntegration.report_error(metadata.conn, {reason, kind}, stack)
4040
end
4141
end

lib/error_tracker/integrations/plug.ex

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ defmodule ErrorTracker.Integrations.Plug do
6262
catch
6363
kind, reason ->
6464
stack = __STACKTRACE__
65-
unquote(__MODULE__).report_error(conn, reason, stack)
65+
unquote(__MODULE__).report_error(conn, {kind, reason}, stack)
6666

6767
:erlang.raise(kind, reason, stack)
6868
end

lib/error_tracker/schemas/error.ex

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,21 @@ defmodule ErrorTracker.Error do
2424
def new(exception, stacktrace = %ErrorTracker.Stacktrace{}) do
2525
source = ErrorTracker.Stacktrace.source(stacktrace)
2626

27+
{kind, reason} =
28+
case exception do
29+
%struct{} = ex when is_exception(ex) ->
30+
{to_string(struct), Exception.message(ex)}
31+
32+
{_kind, %struct{} = ex} when is_exception(ex) ->
33+
{to_string(struct), Exception.message(ex)}
34+
35+
{kind, ex} ->
36+
{to_string(kind), to_string(ex)}
37+
end
38+
2739
params = [
28-
kind: "error",
29-
reason: Exception.message(exception),
40+
kind: to_string(kind),
41+
reason: reason,
3042
source_line: "#{source.file}:#{source.line}",
3143
source_function: "#{source.module}.#{source.function}/#{source.arity}"
3244
]

0 commit comments

Comments
 (0)