title: Rack middleware "trapping" stack trace
body: <p>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.</p>
  
  <p>Here is an example.</p>
  
  <p>I am throwing an exception in an a controller action like so:</p>
  
  <pre><code>def index
    throw "Exception in a Rails controller action"
    @taxonomies = Spree::Taxonomy.all
  end
  </code></pre>
  
  <p>You would expect that the stack trace would reference this location but it does not. Instead it reference a line in the middleware.</p>
  
  <pre><code>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'
  </code></pre>
  
  <p>Why does this happen? Have you seen anything like this before?</p>
  
  <p>Here is the middleware:</p>
  
  <pre><code># 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 &gt; 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 =&gt; false)
              end
            end
          else
            ENV['CURRENT_TENANT_ID'] = nil
            ENV['RAILS_CACHE_ID'] = ""
          end
          @app.call(env)
        end
  
      end
    end
  end
  </code></pre>
  
  <p>I am running ruby <code>2.2.0p0</code> and <code>rails 4.1.8</code>.</p>
  
  <p>I have searched the webs for this but could not find anything, probably because I'm not serching for the right thing.</p>
  
  <p>Any thoughts on why this is happening and what I am doing wrong?</p>
  
  <p>Cheers!</p>