Skip to content

Commit 5c68e71

Browse files
committed
Ignore errors
1 parent c66e452 commit 5c68e71

File tree

2 files changed

+44
-3
lines changed

2 files changed

+44
-3
lines changed

lib/error_tracker.ex

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -108,12 +108,14 @@ defmodule ErrorTracker do
108108
{kind, reason} = normalize_exception(exception, stacktrace)
109109
{:ok, stacktrace} = ErrorTracker.Stacktrace.new(stacktrace)
110110
{:ok, error} = Error.new(kind, reason, stacktrace)
111-
112111
context = Map.merge(get_context(), given_context)
113112

114-
{_error, occurrence} = upsert_error!(error, stacktrace, context, reason)
113+
ignorer = Application.get_env(:error_tracker, :ignorer)
115114

116-
occurrence
115+
if ignorer && !ignorer.ignore?(error, context) do
116+
{_error, occurrence} = upsert_error!(error, stacktrace, context, reason)
117+
occurrence
118+
end
117119
end
118120

119121
@doc """

lib/error_tracker/ignorer.ex

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
defmodule ErrorTracker.Ignorer do
2+
@moduledoc """
3+
Behaviour for ignoring errors.
4+
5+
The ErrorTracker tracks every error that happens in your application. In certain cases you may
6+
want to ignore some errors and don't track them. To do so you can implement this behaviour.
7+
8+
defmodule MyApp.ErrorIgnorer do
9+
@behaviour ErrorTracker.Ignorer
10+
11+
@impl true
12+
def ignore?(error = %ErrorTracker.Error{}, context) do
13+
# return true if the error should be ignored
14+
end
15+
end
16+
17+
Once implemented, include it in the ErrorTracker configuration:
18+
19+
config :error_tracker, ignorer: MyApp.ErrorIgnorer
20+
21+
With this configuration in place, the ErrorTracker will call `MyApp.ErrorIgnorer.ignore?/2` before
22+
tracking errors. If the function returns `true` the error will be ignored and won't be tracked.
23+
24+
> #### A note on performance {: .warning}
25+
>
26+
> Keep in mind that the `ignore?/2` will be called in the context of the ErrorTracker itself.
27+
> Slow code will have a significant impact in the ErrorTracker performance. Buggy code can bring
28+
> the ErrorTracker process down.
29+
"""
30+
31+
@doc """
32+
Decide wether the given error should be ignored or not.
33+
34+
This function receives both the current Error and context and should return a boolean indicating
35+
if it should be ignored or not. If the function returns true the error will be ignored, otherwise
36+
it will be tracked.
37+
"""
38+
@callback ignore?(error :: ErrorTracker.Error.t(), context :: map()) :: boolean
39+
end

0 commit comments

Comments
 (0)