Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Trace Error in Rack Middleware

I'm not 100% sure at this point, but I believe I'm getting an exception that is causing the status of a request to respond with a 401.

I've eliminated all the code in the controller, and the issue still happens, and something seems to be catching the exception in order to return the 401 status, so there's no stack trace.

I've created a custom middleware to try and debug, but I still haven't been able to get to the source of the issue.

How can I isolate where the issue is happening?

We are using heroku, so pry doesn't seem to work, nor do I have command line access to the running server. I've not been able to replicate the issue on my local dev machine.

like image 231
agbodike Avatar asked Jan 18 '26 22:01

agbodike


1 Answers

An alternative is the following module which inserts a debug message at each middleware entry/return (with the status code printed after each return) (adapted from https://github.com/DataDog/dd-trace-rb/issues/368)

# Trace each middleware module entry/exit. Freely adapted from
# https://github.com/DataDog/dd-trace-rb/issues/368 "Trace each middleware in the Rails stack"
module MiddlewareTracer
  def call(env)
    Rails.logger.debug { "MiddleWare: entry #{self.class.name}" }
    status, headers, response = super(env)
    Rails.logger.debug { "MiddleWare: #{self.class.name}, returning with status #{status}" }
    [status, headers, response]
  end
end

# Instrument the middleware stack after initialization so that we
# know the stack won't be changed afterwards.
Rails.configuration.after_initialize do
  Rails.application.middleware.each do |middleware|
    klass = middleware.klass

    # There are a few odd middlewares that aren't classes.
    klass.prepend(MiddlewareTracer) if klass.respond_to?(:prepend)
  end
end
like image 122
orpe Avatar answered Jan 20 '26 12:01

orpe