-
Notifications
You must be signed in to change notification settings - Fork 55
/
mixin.ex
64 lines (51 loc) · 2.04 KB
/
mixin.ex
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
defmodule Honeybadger.Filter.Mixin do
@moduledoc """
A default implementation of `Honeybadger.Filter`.
If you need to implement custom filtering for one or more of the elements in
a `Honeybadger.Notice`, you can define your own filter module and register it
in the config. E.g., if you wanted to filter the error message string, but
keep all of the other default filtering, you could do:
defmoudle MyApp.MyFilter do
use Honeybadger.Filter.Mixin
def filter_error_message(message) do
# replace passwords in error message with `"xxx"`
Regex.replace(~r/(password:\s*)"([^"]+)"/, message, ~s(\\1"xxx"))
end
end
And set the configuration to:
config :honeybadger,
filter: MyApp.MyFilter
See the documentation for `Honeybadger.Filter` for a list of functions that
may be overridden. The default implementations for all of the functions that
take a `map` are to remove any keys from the map that match a key in
`filter_keys`. The check matches atoms and strings in a case insensitive
manner.
"""
defmacro __using__(_) do
quote location: :keep do
@behaviour Honeybadger.Filter
def filter_context(context), do: filter_map(context)
def filter_params(params), do: filter_map(params)
def filter_cgi_data(cgi_data), do: filter_map(cgi_data)
def filter_session(session), do: filter_map(session)
def filter_error_message(message), do: message
def filter_breadcrumbs(breadcrumbs), do: breadcrumbs
@doc false
def filter_map(map) do
filter_map(map, Honeybadger.get_env(:filter_keys))
end
def filter_map(map, keys) when is_list(keys) do
filter_keys = Enum.map(keys, &Honeybadger.Utils.canonicalize/1)
drop_keys =
map
|> Map.keys()
|> Enum.filter(&Enum.member?(filter_keys, Honeybadger.Utils.canonicalize(&1)))
Map.drop(map, drop_keys)
end
def filter_map(map, _keys) do
map
end
defoverridable Honeybadger.Filter
end
end
end