Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rack middleware "trapping" stack trace

I have a piece of Rack middleware that loads a tenant, via subdomain, and applies some default settings. The middleware, while not pretty, does it's job fine enough. However, when an exception is thrown within the app the middleware "traps" the full stack trace. When I say trap I mean it hides the expected stack trace.

Here is an example.

I am throwing an exception in an a controller action like so:

def index
  throw "Exception in a Rails controller action"
  @taxonomies = Spree::Taxonomy.all
end

You would expect that the stack trace would reference this location but it does not. Instead it reference a line in the middleware.

Completed 500 Internal Server Error in 139ms

UncaughtThrowError (uncaught throw "Exception in a Rails controller action"):
lib/tenant_manager/middleware/loader.rb:42:in `call'

Why does this happen? Have you seen anything like this before?

Here is the middleware:

# lib/tenant_manager/middleware/loader.rb
module TenantManager
  module Middleware
    class Loader
    # Middleware to detect an tenant via subdomain early in
    # the request process
    #
    # Usage:
    #   # config/application.rb
    #   config.middleware.use TenantManager::Middleware::Loader
    #
    # A scaled down version of https://github.com/radar/houser

      def initialize(app)
        @app = app
      end

      def call(env)
        domain_parts = env['HTTP_HOST'].split('.')
        if domain_parts.length > 2
          subdomain = domain_parts.first
          tenant = Leafer::Tenant.find_by_database(subdomain)
          if tenant
            ENV['CURRENT_TENANT_ID'] = tenant.id.to_s
            ENV['RAILS_CACHE_ID'] = tenant.database
            Spree::Image.change_paths tenant.database
            Apartment::Tenant.process(tenant.database) do
              country = Spree::Country.find_by_name('United States')
              Spree.config do |config|
                config.default_country_id = country.id if country.present?
                config.track_inventory_levels = false
              end
              Spree::Auth::Config.set(:registration_step => false)
            end
          end
        else
          ENV['CURRENT_TENANT_ID'] = nil
          ENV['RAILS_CACHE_ID'] = ""
        end
        @app.call(env)
      end

    end
  end
end

I am running ruby 2.2.0p0 and rails 4.1.8.

I have searched the webs for this but could not find anything, probably because I'm not serching for the right thing.

Any thoughts on why this is happening and what I am doing wrong?

Cheers!

like image 617
bennick Avatar asked Apr 07 '15 18:04

bennick


1 Answers

I finally found the solution to this. It turns out that the last line in what is considered my application is in the middleware. I was running the rest of the code in a local rails engine located in a components directory. All we needed to do was create a new silencer for BacktraceCleaner. Notice components dir is now included.

# config/initializers/backtrace_silencers.rb
Rails.backtrace_cleaner.remove_silencers!
Rails.backtrace_cleaner.add_silencer { |line| line !~ /^\/(app|config|lib|test|components)/}

If you are interested here is an issue I posted on the rails project about how to replicate this in detail. https://github.com/rails/rails/issues/22265

like image 126
bennick Avatar answered Oct 12 '22 22:10

bennick