gpt4 book ai didi

ruby - 公寓 ruby gem : Want to Catch an exception

转载 作者:数据小太阳 更新时间:2023-10-29 06:46:27 26 4
gpt4 key购买 nike

我正在使用这个 apartment ruby 。

我在 application.rb 文件中添加了这个:

config.middleware.use 'Apartment::Elevators::Subdomain'

当我尝试在 PostgreSQL 中不存在子域“test”模式的浏览器 url“test.domain.local:3000”中点击这个时,我看到了这个错误

Apartment::SchemaNotFound (One of the following schema(s) is invalid: test, "public")

我知道这是 gem 的正常行为,但想捕获此异常并将用户重定向到其他页面,我该怎么做?

最佳答案

这是我做的:

在lib/rescued_apartment_middleware.rb下创建文件

module RescuedApartmentMiddleware
def call(*args)
begin
super
rescue Apartment::TenantNotFound
Rails.logger.error "ERROR: Apartment Tenant not found: #{Apartment::Tenant.current.inspect}"
return [404, {"Content-Type" => "text/html"}, ["#{File.read(Rails.root.to_s + '/public/404.html')}"] ]
end
end
end

并将以下行添加到您的公寓初始化程序文件中:

require 'rescued_apartment_middleware'

Rails.application.config.middleware.use 'Apartment::Elevators::Subdomain'
Apartment::Elevators::Subdomain.prepend RescuedApartmentMiddleware

这很有魅力! (使用 ruby​​ 2.1 和 Rails 4.1 测试)

关于ruby - 公寓 ruby gem : Want to Catch an exception,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27188411/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com