Skip to content

Commit 1956a56

Browse files
authored
Fix some exceptions reporting (#36)
Some erlang errors (bad arithmetic, no case clause matching, undefined functions, etc.) along with throws and exists were not being recorded properly. This commit reports them well and handles the case in which the source file and line are unknown.
1 parent 88f27a8 commit 1956a56

File tree

4 files changed

+24
-21
lines changed

4 files changed

+24
-21
lines changed

lib/error_tracker.ex

Lines changed: 17 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -94,26 +94,11 @@ defmodule ErrorTracker do
9494
* An exception struct: the module of the exception is stored along with
9595
the exception message.
9696
97-
* A `{kind, exception}` tuple in which the `exception` is a struct: it
98-
behaves the same as when passing just the exception struct.
99-
100-
* A `{kind, reason}` tuple: it stores the kind and the message itself cast
101-
to strings, which is useful for some errors like EXIT signals or custom error
102-
messages.
97+
* A `{kind, exception}` tuple in which case the information is converted to
98+
an Elixir exception (if possible) and stored.
10399
"""
104100
def report(exception, stacktrace, given_context \\ %{}) do
105-
{kind, reason} =
106-
case exception do
107-
%struct{} = ex when is_exception(ex) ->
108-
{to_string(struct), Exception.message(ex)}
109-
110-
{_kind, %struct{} = ex} when is_exception(ex) ->
111-
{to_string(struct), Exception.message(ex)}
112-
113-
{kind, ex} ->
114-
{to_string(kind), to_string(ex)}
115-
end
116-
101+
{kind, reason} = normalize_exception(exception, stacktrace)
117102
{:ok, stacktrace} = ErrorTracker.Stacktrace.new(stacktrace)
118103
{:ok, error} = Error.new(kind, reason, stacktrace)
119104

@@ -181,4 +166,18 @@ defmodule ErrorTracker do
181166
def get_context do
182167
Process.get(:error_tracker_context, %{})
183168
end
169+
170+
defp normalize_exception(%struct{} = ex, _stacktrace) when is_exception(ex) do
171+
{to_string(struct), Exception.message(ex)}
172+
end
173+
174+
defp normalize_exception({kind, ex}, stacktrace) do
175+
case Exception.normalize(kind, ex, stacktrace) do
176+
%struct{} = ex ->
177+
{to_string(struct), Exception.message(ex)}
178+
179+
other ->
180+
{to_string(kind), to_string(other)}
181+
end
182+
end
184183
end

lib/error_tracker/schemas/error.ex

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,11 @@ defmodule ErrorTracker.Error do
2929
@doc false
3030
def new(kind, reason, stacktrace = %ErrorTracker.Stacktrace{}) do
3131
source = ErrorTracker.Stacktrace.source(stacktrace)
32+
source_line = if source.file, do: "#{source.file}:#{source.line}", else: "nofile"
3233

3334
params = [
3435
kind: to_string(kind),
35-
source_line: "#{source.file}:#{source.line}",
36+
source_line: source_line,
3637
source_function: "#{source.module}.#{source.function}/#{source.arity}"
3738
]
3839

lib/error_tracker/schemas/stacktrace.ex

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ defmodule ErrorTracker.Stacktrace do
2727
application: to_string(application),
2828
module: module |> to_string() |> String.replace_prefix("Elixir.", ""),
2929
function: to_string(function),
30-
arity: arity,
30+
arity: normalize_arity(arity),
3131
file: to_string(opts[:file]),
3232
line: opts[:line]
3333
}
@@ -39,6 +39,9 @@ defmodule ErrorTracker.Stacktrace do
3939
|> Ecto.Changeset.apply_action(:new)
4040
end
4141

42+
defp normalize_arity(a) when is_integer(a), do: a
43+
defp normalize_arity(a) when is_list(a), do: length(a)
44+
4245
defp line_changeset(line = %__MODULE__.Line{}, params) do
4346
Ecto.Changeset.cast(line, params, ~w[application module function arity file line]a)
4447
end

lib/error_tracker/web/live/show.html.heex

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@
4848
<td class="px-2 align-top"><pre>(<%= line.application || @app %>)</pre></td>
4949
<td>
5050
<pre><%= "#{sanitize_module(line.module)}.#{line.function}/#{line.arity}" %>
51-
<%= "#{line.file}.#{line.line}" %></pre>
51+
<%= if line.line, do: "#{line.file}:#{line.line}", else: "nofile" %></pre>
5252
</td>
5353
</tr>
5454
</tbody>

0 commit comments

Comments
 (0)