Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 17 additions & 18 deletions lib/error_tracker.ex
Original file line number Diff line number Diff line change
Expand Up @@ -94,26 +94,11 @@ defmodule ErrorTracker do
* An exception struct: the module of the exception is stored along with
the exception message.

* A `{kind, exception}` tuple in which the `exception` is a struct: it
behaves the same as when passing just the exception struct.

* A `{kind, reason}` tuple: it stores the kind and the message itself cast
to strings, which is useful for some errors like EXIT signals or custom error
messages.
* A `{kind, exception}` tuple in which case the information is converted to
an Elixir exception (if possible) and stored.
"""
def report(exception, stacktrace, given_context \\ %{}) do
{kind, reason} =
case exception do
%struct{} = ex when is_exception(ex) ->
{to_string(struct), Exception.message(ex)}

{_kind, %struct{} = ex} when is_exception(ex) ->
{to_string(struct), Exception.message(ex)}

{kind, ex} ->
{to_string(kind), to_string(ex)}
end

{kind, reason} = normalize_exception(exception, stacktrace)
{:ok, stacktrace} = ErrorTracker.Stacktrace.new(stacktrace)
{:ok, error} = Error.new(kind, reason, stacktrace)

Expand Down Expand Up @@ -181,4 +166,18 @@ defmodule ErrorTracker do
def get_context do
Process.get(:error_tracker_context, %{})
end

defp normalize_exception(%struct{} = ex, _stacktrace) when is_exception(ex) do
{to_string(struct), Exception.message(ex)}
end

defp normalize_exception({kind, ex}, stacktrace) do
case Exception.normalize(kind, ex, stacktrace) do
%struct{} = ex ->
{to_string(struct), Exception.message(ex)}

other ->
{to_string(kind), to_string(other)}
end
end
end
3 changes: 2 additions & 1 deletion lib/error_tracker/schemas/error.ex
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,11 @@ defmodule ErrorTracker.Error do
@doc false
def new(kind, reason, stacktrace = %ErrorTracker.Stacktrace{}) do
source = ErrorTracker.Stacktrace.source(stacktrace)
source_line = if source.file, do: "#{source.file}:#{source.line}", else: "nofile"

params = [
kind: to_string(kind),
source_line: "#{source.file}:#{source.line}",
source_line: source_line,
source_function: "#{source.module}.#{source.function}/#{source.arity}"
]

Expand Down
5 changes: 4 additions & 1 deletion lib/error_tracker/schemas/stacktrace.ex
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ defmodule ErrorTracker.Stacktrace do
application: to_string(application),
module: module |> to_string() |> String.replace_prefix("Elixir.", ""),
function: to_string(function),
arity: arity,
arity: normalize_arity(arity),
file: to_string(opts[:file]),
line: opts[:line]
}
Expand All @@ -39,6 +39,9 @@ defmodule ErrorTracker.Stacktrace do
|> Ecto.Changeset.apply_action(:new)
end

defp normalize_arity(a) when is_integer(a), do: a
defp normalize_arity(a) when is_list(a), do: length(a)

defp line_changeset(line = %__MODULE__.Line{}, params) do
Ecto.Changeset.cast(line, params, ~w[application module function arity file line]a)
end
Expand Down
2 changes: 1 addition & 1 deletion lib/error_tracker/web/live/show.html.heex
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@
<td class="px-2 align-top"><pre>(<%= line.application || @app %>)</pre></td>
<td>
<pre><%= "#{sanitize_module(line.module)}.#{line.function}/#{line.arity}" %>
<%= "#{line.file}.#{line.line}" %></pre>
<%= if line.line, do: "#{line.file}:#{line.line}", else: "nofile" %></pre>
</td>
</tr>
</tbody>
Expand Down