ForzaElixir / rollbax

Exception tracking and logging from Elixir to Rollbar
https://hexdocs.pm/rollbax
ISC License
243 stars 52 forks source link

Feature Request: filter exception messages #101

Closed cdesch closed 5 years ago

cdesch commented 5 years ago

I want to add a filter for certain exception messages that can be put into the config.exs for Rollbax. I was thinking of something similar to the Rails Rollbar gem using a map like this:

config.exception_level_filters.merge!({
  'ActionController::RoutingError' => 'ignore',
  'NoMethodError' => 'critical'
})

We would have to add an :ignore message type and check for it before reporting. If this is something folks want, I'd be willing to work on a PR, although I'd want some feedback on where to implement best prior to starting work.

It could be handled in the router if you are using plug as described here in the docs. Here is my code in some simple filtering using plug. I think we would do something similar if the error structs were stored in the config.exs although the error struct would be checked for reportable exception type when calling report, report_message, or report_exception

  defp handle_errors(_conn, error_params = %{kind: kind, reason: reason, stack: stacktrace}) do
    case reason.__struct__ do
      Phoenix.Router.NoRouteError -> log_errors(error_params)
      Plug.CSRFProtection.InvalidCSRFTokenError -> log_errors(error_params)
      _ -> Rollbax.report(kind, reason, stacktrace)
    end
  end

  defp log_errors(%{kind: kind, reason: reason, stack: stacktrace}) do
    Logger.error "#{inspect(kind)} #{inspect(reason)} #{inspect(stacktrace)}"
  end
whatyouhide commented 5 years ago

Hey @cdesch, since you are able to customize this with handle_errors like you showed, I'm inclined to not have this in the library. Having this as a configuration option might limit the flexibility of filtering errors and I don't have a good API in mind. With handle_errors, you're able to customize exactly how you need it for your application. Thanks a lot for the feature request! 💟

cdesch commented 5 years ago

@whatyouhide Sounds good. I was thinking that it might be useful for not Phoenix applications, but I agreed in that handle_errors is a good alternative. Thank you.

florish commented 5 years ago

Just for anyone coming here by googling: an alternative for matching on reason.__struct__ is to do pattern matching on the handle_errors/2 function:

def handle_errors(_conn, %{reason: %Phoenix.Router.NoRouteError{}}), do: :ok

def handle_errors(conn, %{kind: kind, reason: reason, stack: stacktrace}) do
  # Actual reporting logic
end

This avoids having to add a case statement inside the handle_errors/2 function itself.