Skip to content

Commit 3886369

Browse files
authored
Fix reporting errors from Oban (#70)
There was an error when an Oban worker returned an `{:error, reason}` tuple because Oban does not return an stacktrace (makes sense, as it is not an exception by itself). Our code was not prepared for that use case. I modified it to handle it and circumvent the needed for source file and source function information. After this change, empty stack traces and source information are not shown on the dashboard to avoid confusion. As an extra feature I have also added the reporting of the `state` of the job (which is mostly ever `failure` for us). Closes #63
1 parent 67ef9f7 commit 3886369

File tree

4 files changed

+26
-6
lines changed

4 files changed

+26
-6
lines changed

lib/error_tracker/integrations/oban.ex

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,8 @@ defmodule ErrorTracker.Integrations.Oban do
5959

6060
def handle_event([:oban, :job, :exception], _measurements, metadata, :no_config) do
6161
%{reason: exception, stacktrace: stacktrace} = metadata
62+
state = Map.get(metadata, :state, :failure)
6263

63-
ErrorTracker.report(exception, stacktrace)
64+
ErrorTracker.report(exception, stacktrace, %{state: state})
6465
end
6566
end

lib/error_tracker/schemas/error.ex

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,12 +29,21 @@ 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"
32+
33+
{source_line, source_function} =
34+
if source do
35+
source_line = if source.line, do: "#{source.file}:#{source.line}", else: "nofile"
36+
source_function = "#{source.module}.#{source.function}/#{source.arity}"
37+
38+
{source_line, source_function}
39+
else
40+
{"-", "-"}
41+
end
3342

3443
params = [
3544
kind: to_string(kind),
3645
source_line: source_line,
37-
source_function: "#{source.module}.#{source.function}/#{source.arity}"
46+
source_function: source_function
3847
]
3948

4049
fingerprint = :crypto.hash(:sha256, params |> Keyword.values() |> Enum.join())
@@ -46,4 +55,14 @@ defmodule ErrorTracker.Error do
4655
|> Ecto.Changeset.put_change(:last_occurrence_at, DateTime.utc_now())
4756
|> Ecto.Changeset.apply_action(:new)
4857
end
58+
59+
@doc """
60+
Returns if the Error has information of the source or not.
61+
62+
Errors usually have information about in which line and function occurred, but
63+
in some cases (like an Oban job ending with `{:error, any()}`) we cannot get
64+
that information and no source is stored.
65+
"""
66+
def has_source_info?(%__MODULE__{source_function: "-", source_line: "-"}), do: false
67+
def has_source_info?(%__MODULE__{}), do: true
4968
end

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@
6969
<p class="whitespace-nowrap text-ellipsis w-full overflow-hidden">
7070
(<%= sanitize_module(error.kind) %>) <%= error.reason %>
7171
</p>
72-
<p class="font-normal text-gray-400">
72+
<p :if={ErrorTracker.Error.has_source_info?(error)} class="font-normal text-gray-400">
7373
<%= sanitize_module(error.source_function) %>
7474
<br />
7575
<%= error.source_line %>

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,13 @@
1919
<pre class="overflow-auto p-4 rounded-lg bg-gray-300/10 border border-gray-900"><%= @occurrence.reason %></pre>
2020
</.section>
2121

22-
<.section title="Source">
22+
<.section :if={ErrorTracker.Error.has_source_info?(@error)} title="Source">
2323
<pre class="overflow-auto text-sm p-4 rounded-lg bg-gray-300/10 border border-gray-900">
2424
<%= sanitize_module(@error.source_function) %>
2525
<%= @error.source_line %></pre>
2626
</.section>
2727

28-
<.section title="Stacktrace">
28+
<.section :if={@occurrence.stacktrace.lines != []} title="Stacktrace">
2929
<div class="p-4 bg-gray-300/10 border border-gray-900 rounded-lg">
3030
<div class="w-full mb-4">
3131
<label class="flex justify-end">

0 commit comments

Comments
 (0)