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
16 changes: 14 additions & 2 deletions lib/error_tracker.ex
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,20 @@ defmodule ErrorTracker do
alias ErrorTracker.Repo

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

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

context = Map.merge(get_context(), given_context)

Expand All @@ -24,7 +36,7 @@ defmodule ErrorTracker do
)

error
|> Ecto.build_assoc(:occurrences, stacktrace: stacktrace, context: context)
|> Ecto.build_assoc(:occurrences, stacktrace: stacktrace, context: context, reason: reason)
|> Repo.insert!()
end

Expand Down
1 change: 1 addition & 0 deletions lib/error_tracker/migrations/postgres/v01.ex
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ defmodule ErrorTracker.Migrations.Postgres.V01 do

create table(:error_tracker_occurrences, prefix: prefix) do
add :context, :map, null: false
add :reason, :text, null: false
add :stacktrace, :map, null: false
add :error_id, references(:error_tracker_errors, on_delete: :delete_all), null: false

Expand Down
16 changes: 2 additions & 14 deletions lib/error_tracker/schemas/error.ex
Original file line number Diff line number Diff line change
Expand Up @@ -22,24 +22,11 @@ defmodule ErrorTracker.Error do
timestamps(type: :utc_datetime_usec)
end

def new(exception, stacktrace = %ErrorTracker.Stacktrace{}) do
def new(kind, reason, stacktrace = %ErrorTracker.Stacktrace{}) do
source = ErrorTracker.Stacktrace.source(stacktrace)

{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

params = [
kind: to_string(kind),
reason: reason,
source_line: "#{source.file}:#{source.line}",
source_function: "#{source.module}.#{source.function}/#{source.arity}"
]
Expand All @@ -48,6 +35,7 @@ defmodule ErrorTracker.Error do

%__MODULE__{}
|> Ecto.Changeset.change(params)
|> Ecto.Changeset.put_change(:reason, reason)
|> Ecto.Changeset.put_change(:fingerprint, Base.encode16(fingerprint))
|> Ecto.Changeset.put_change(:last_occurrence_at, DateTime.utc_now())
|> Ecto.Changeset.apply_action(:new)
Expand Down
1 change: 1 addition & 0 deletions lib/error_tracker/schemas/occurrence.ex
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ defmodule ErrorTracker.Occurrence do

schema "error_tracker_occurrences" do
field :context, :map
field :reason, :string

embeds_one :stacktrace, ErrorTracker.Stacktrace
belongs_to :error, ErrorTracker.Error
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 @@ -16,7 +16,7 @@
<div class="grid grid-cols-1 md:grid-cols-4 md:space-x-3 mt-6 gap-2">
<div class="px-3 md:col-span-3 md:border-r-2 md:border-gray-500 space-y-8">
<.section title="Full message">
<pre class="overflow-auto p-4 bg-gray-600"><%= @error.reason %></pre>
<pre class="overflow-auto p-4 bg-gray-600"><%= @occurrence.reason %></pre>
</.section>

<.section title="Source">
Expand Down