Hello there!
This is probably a very common issue, however I can't seem to find the correct term to search to find a related issue — I do sincerely apologise if it's a duplicate!
I have made some a custom middleware, which looks like this:
class RedirectMiddleware
def initialize(app)
@app = app
end
def redirect(location)
[301, { 'Location' => location, 'Content-Type' => 'text/html' }, ['Moved Permanently']]
end
def call(env)
req = Rack::Request.new(env)
if redirection = Redirection.find_by(old_path: req.path)
return redirect(redirection.new_path)
end
@app.call(env)
end
end
I then use the middleware in production.rb by doing the following:
config.middleware.use ::RedirectMiddleware
In Sentry, every exception points to the following line under the "App Only" stacktrace:
@app.call(env)
When I open the "Full" stacktrace, it correctly points to the line where the exception took place.
Is there an easy way to prevent this from happening? I've tried inserting my middleware before Raven::Rack, however that didn't help.
Thanks!
I've been unable to find a solution for this. Does anyone have any recommendations?
Try moving the source code for code for your custom middleware to vendor/middleware/ to trick Rails into silencing it from the backtrace. You can use require_relative to require the middleware manually prior to adding the middleware to the stack.
(I was trying to solve the same thing using Rails backtrace_cleaner.add_silencer, but was unsuccessful due to https://github.com/rails/rails/issues/24434, moving my code to vendor/ was my solution)
@robertlyall Did you find a different solution than @gkop ? Or did that work for you?
I've just rolled the proposed solution into production. I'll do some tests and report back. Many thanks!
Most helpful comment
Try moving the source code for code for your custom middleware to
vendor/middleware/to trick Rails into silencing it from the backtrace. You can userequire_relativeto require the middleware manually prior to adding the middleware to the stack.(I was trying to solve the same thing using Rails backtrace_cleaner.add_silencer, but was unsuccessful due to https://github.com/rails/rails/issues/24434, moving my code to vendor/ was my solution)