sentry-ruby icon indicating copy to clipboard operation
sentry-ruby copied to clipboard

Custom transaction status code for exceptions

Open gustavodiel opened this issue 1 year ago • 2 comments

Describe the idea

The idea is to allow us to set custom status code for when a exception occurs during a transaction, something like

Sentry.init do |config|
  config.exception_status_code = ->(exception) do
    case exception
    when ActiveRecord::RecordNotFound, ActionController::RoutingError
      404
    else
      500
    end
  end
end

Why do you think it's beneficial to most of the users

Sentry assumes status code 500 (internal error) for all of the exceptions during transactions. The problem is that in a production environment, by default Rails will raise ActionController::RoutingError whenever somebody tries to access a non-existing page, which will result in "internal error" transactions.

This feature allows the user to map different exceptions to different status codes, so they can ignore that without having to change the default Rails behavior.

Possible implementation

In the Sentry::Rack::CaptureExceptions we can replace the hardcoded 500 status code from the rescue Exception with a configurable lambda that returns the desired status code for the specific exception.

Another possibility, is to bake this rule into Sentry (404 for routing errors), but I think that would be a shift in the current behavior which might not be ideal

gustavodiel avatar Jun 27 '24 17:06 gustavodiel

@gustavodiel thanks for the contribution!

If this is mostly about rails, I prefer just making the default experience better since it's also semantically correct for the 404s.

sl0thentr0py avatar Jul 09 '24 14:07 sl0thentr0py

Thanks for the update @sl0thentr0py. Can we help speed this up? I saw you closed https://github.com/getsentry/sentry-ruby/pull/2334. We're happy to rework the patch if you provide guidance on where we want to have it.

mhfs avatar Dec 19 '24 13:12 mhfs