I have a piece of Rack middleware that downloads a tenant through a subdomain and applies some default settings. The intermediate level tool, although not very beautiful, does the job quite well. However, when an exception is thrown in the application, the trap middleware tracks the full stack. When I say "trap", I mean that it hides the expected stack trace.
Here is an example.
I throw an exception to the controller action as follows:
def index throw "Exception in a Rails controller action" @taxonomies = Spree::Taxonomy.all end
You expect the stack trace to reference this location, but it is not. Instead, it refers to 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 is this happening? 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 searched the web pages for this, but could not find anything, perhaps because I am not angry at the right thing.
Any thoughts on why this is happening and what I am doing wrong?
Hooray!
source share