Skip to content

Commit e00040f

Browse files
authored
Filter context only when tracking errors (#100)
Just a few additions to #94: - We don't need to filter context when not tracking errors - Mention how to filter context in the Getting Started guide
1 parent fd552ad commit e00040f

File tree

2 files changed

+10
-9
lines changed

2 files changed

+10
-9
lines changed

guides/Getting Started.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,11 +107,13 @@ In certain cases, you may want to include some additional information when track
107107

108108
The `ErrorTracker.set_context/1` function stores the given context in the current process so any errors that occur in that process (for example, a Phoenix request or an Oban job) will include this given context along with the default integration context.
109109

110+
There are some requirements on the type of data that can be included in the context, so we recommend taking a look at `ErrorTracker.set_context/1` documentation
111+
110112
```elixir
111113
ErrorTracker.set_context(%{user_id: conn.assigns.current_user.id})
112114
```
113115

114-
There are some requirements on the type of data that can be included in the context, so we recommend taking a look at `set_context/1` documentation
116+
You may also want to sanitize or filter out some information from the context before saving it. To do that you can take a look at the `ErrorTracker.Filter` behaviour.
115117

116118
## Manual error tracking
117119

lib/error_tracker.ex

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -118,10 +118,11 @@ defmodule ErrorTracker do
118118
{kind, reason} = normalize_exception(exception, stacktrace)
119119
{:ok, stacktrace} = ErrorTracker.Stacktrace.new(stacktrace)
120120
{:ok, error} = Error.new(kind, reason, stacktrace)
121-
context = get_context() |> Map.merge(given_context) |> filter_context_data()
121+
context = Map.merge(get_context(), given_context)
122122

123123
if enabled?() && !ignored?(error, context) do
124-
{_error, occurrence} = upsert_error!(error, stacktrace, context, reason)
124+
sanitized_context = sanitize_context(context)
125+
{_error, occurrence} = upsert_error!(error, stacktrace, sanitized_context, reason)
125126
occurrence
126127
else
127128
:noop
@@ -209,14 +210,12 @@ defmodule ErrorTracker do
209210
ignorer && ignorer.ignore?(error, context)
210211
end
211212

212-
defp filter_context_data(context) do
213+
defp sanitize_context(context) do
213214
filter_mod = Application.get_env(:error_tracker, :filter)
214215

215-
if filter_mod do
216-
filter_mod.sanitize(context)
217-
else
218-
context
219-
end
216+
if filter_mod,
217+
do: filter_mod.sanitize(context),
218+
else: context
220219
end
221220

222221
defp normalize_exception(%struct{} = ex, _stacktrace) when is_exception(ex) do

0 commit comments

Comments
 (0)