@@ -51,11 +51,40 @@ defmodule Sentry do
5151 our local development machines, exceptions will never be sent, because the
5252 default value is not in the list of `included_environments`.
5353
54+ ## Filtering Exceptions
55+
56+ If you would like to prevent certain exceptions, the :filter configuration option
57+ allows you to implement the `Sentry.EventFilter` behaviour. The first argument is the
58+ source of the event, and the second is the exception to be sent. `Sentry.Plug`
59+ will have a source of `:plug`, and `Sentry.Logger` will have a source of `:logger`.
60+ If an exception does not come from either of those sources, the source will be nil
61+ unless the `:event_source` option is passed to `Sentry.capture_exception/2`
62+
63+ A configuration like below will prevent sending `Phoenix.Router.NoRouteError` from `Sentry.Plug`, but
64+ allows other exceptions to be sent.
65+
66+ # sentry_event_filter.exs
67+ defmodule MyApp.SentryEventFilter do
68+ @behaviour Sentry.EventFilter
69+
70+ def exclude_exception?(:plug, %Elixir.Phoenix.Router.NoRouteError{}), do: true
71+ def exclude_exception?(_, ), do: false
72+ end
73+
74+ # config.exs
75+ config :sentry, filter: MyApp.SentryEventFilter,
76+ included_environments: ~w(production staging),
77+ environment_name: System.get_env("RELEASE_LEVEL") || "development"
78+
5479 ## Capturing Exceptions
5580
56- Simply calling `capture_exception\ 2 ` will send the event.
81+ Simply calling `capture_exception/ 2` will send the event.
5782
5883 Sentry.capture_exception(my_exception)
84+ Sentry.capture_exception(other_exception, [source_name: :my_source])
85+
86+ ### Options
87+ * `:event_source` - The source passed as the first argument to `Sentry.EventFilter.exclude_exception?/2`
5988
6089 ## Configuring The `Logger` Backend
6190
@@ -82,11 +111,16 @@ defmodule Sentry do
82111 @ doc """
83112 Parses and submits an exception to Sentry if current environment is in included_environments.
84113 """
85- @ spec capture_exception ( Exception . t , Keyword . t ) :: { :ok , String . t } | :error
114+ @ spec capture_exception ( Exception . t , Keyword . t ) :: { :ok , String . t } | :error | :excluded
86115 def capture_exception ( exception , opts \\ [ ] ) do
87- exception
88- |> Event . transform_exception ( opts )
89- |> send_event ( )
116+ filter_module = Application . get_env ( :sentry , :filter , Sentry.DefaultEventFilter )
117+ { source , opts } = Keyword . pop ( opts , :event_source )
118+ if ( filter_module . exclude_exception? ( exception , source ) ) do
119+ :excluded
120+ else
121+ Event . transform_exception ( exception , opts )
122+ |> send_event ( )
123+ end
90124 end
91125
92126 @ doc """
0 commit comments