Skip to content

Commit 90f6edc

Browse files
authored
Flag to globally disable error tracking (#82)
This change adds a new global flag named `:enabled` that allows to disable the error tracking on certain environments. This behaviour can be done also with a custom ignorer using the changes that we will introduce in #79 , but I felt like this use case will be quite common in dev environments and having a fast way to just disable every error is useful. Test have been added and documentation updated. Closes #81
1 parent e2c3d09 commit 90f6edc

File tree

4 files changed

+48
-5
lines changed

4 files changed

+48
-5
lines changed

guides/Getting Started.md

+4-1
Original file line numberDiff line numberDiff line change
@@ -32,13 +32,16 @@ ErrorTracker needs a few configuration options to work. This configuration shoul
3232
```elixir
3333
config :error_tracker,
3434
repo: MyApp.Repo,
35-
otp_app: :my_app
35+
otp_app: :my_app,
36+
enabled: true
3637
```
3738

3839
The `:repo` option specifies the repository that will be used by ErrorTracker. You can use your regular application repository or a different one if you prefer to keep errors in a different database.
3940

4041
The `:otp_app` option specifies your application name. When an error occurs, ErrorTracker will use this information to understand which parts of the stack trace belong to your application and which parts belong to third-party dependencies. This allows you to filter in-app vs third-party frames when viewing errors.
4142

43+
The `:enabled` option (defaults to `true` if not present) allows to disable the ErrorTracker on certain environments. This is useful to avoid filling your dev database with errors, for example.
44+
4245
## Setting up the database
4346

4447
Since ErrorTracker stores errors in the database you must create a database migration to add the required tables:

lib/error_tracker.ex

+13-3
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,9 @@ defmodule ErrorTracker do
7777
@doc """
7878
Report an exception to be stored.
7979
80+
Returns the occurrence stored or `:noop` if the ErrorTracker is disabled by
81+
configuration the exception has not been stored.
82+
8083
Aside from the exception, it is expected to receive the stack trace and,
8184
optionally, a context map which will be merged with the current process
8285
context.
@@ -111,9 +114,12 @@ defmodule ErrorTracker do
111114

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

114-
{_error, occurrence} = upsert_error!(error, stacktrace, context, reason)
115-
116-
occurrence
117+
if enabled?() do
118+
{_error, occurrence} = upsert_error!(error, stacktrace, context, reason)
119+
occurrence
120+
else
121+
:noop
122+
end
117123
end
118124

119125
@doc """
@@ -185,6 +191,10 @@ defmodule ErrorTracker do
185191
Process.get(:error_tracker_context, %{})
186192
end
187193

194+
defp enabled? do
195+
!!Application.get_env(:error_tracker, :enabled, true)
196+
end
197+
188198
defp normalize_exception(%struct{} = ex, _stacktrace) when is_exception(ex) do
189199
{to_string(struct), Exception.message(ex)}
190200
end

test/error_tracker_test.exs

+27
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,16 @@ defmodule ErrorTrackerTest do
77
@relative_file_path Path.relative_to(__ENV__.file, File.cwd!())
88

99
describe inspect(&ErrorTracker.report/3) do
10+
setup context do
11+
if Map.has_key?(context, :enabled) do
12+
Application.put_env(:error_tracker, :enabled, context[:enabled])
13+
# Ensure that the application env is restored after each test
14+
on_exit(fn -> Application.delete_env(:error_tracker, :enabled) end)
15+
end
16+
17+
[]
18+
end
19+
1020
test "reports exceptions" do
1121
%Occurrence{error: error = %Error{}} =
1222
report_error(fn -> raise "This is a test" end)
@@ -72,6 +82,23 @@ defmodule ErrorTrackerTest do
7282

7383
assert %Occurrence{} = report_error(fn -> raise "test" end, invalid_context)
7484
end
85+
86+
test "without enabled flag it works as expected" do
87+
# Ensure no value is set
88+
Application.delete_env(:error_tracker, :enabled)
89+
90+
assert %Occurrence{} = report_error(fn -> raise "Sample error" end)
91+
end
92+
93+
@tag enabled: true
94+
test "with enabled flag to true it works as expected" do
95+
assert %Occurrence{} = report_error(fn -> raise "Sample error" end)
96+
end
97+
98+
@tag enabled: false
99+
test "with enabled flag to false it does not store the exception" do
100+
assert report_error(fn -> raise "Sample error" end) == :noop
101+
end
75102
end
76103

77104
describe inspect(&ErrorTracker.resolve/1) do

test/support/case.ex

+4-1
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,10 @@ defmodule ErrorTracker.Test.Case do
2828
ErrorTracker.report({kind, reason}, __STACKTRACE__, context)
2929
end
3030

31-
repo().preload(occurrence, :error)
31+
case occurrence do
32+
%ErrorTracker.Occurrence{} -> repo().preload(occurrence, :error)
33+
other -> other
34+
end
3235
end
3336

3437
@doc """

0 commit comments

Comments
 (0)