Rob*_*udi 6 middleware exception-handling ruby-on-rails-3
我有一个使用Apartment的Rails 3.2应用程序,它用作中间件.公寓抛出一个Apartment::SchemaNotFound异常,也没有办法与拯救它rescue_from从ApplicationController.我想我会config.exceptions_app按照这篇博文中第3点的描述使用,但我不能将路由器设置为异常应用程序,我假设我必须创建自己的.
所以问题是:我该如何进行?
我遇到了类似的问题,另一个中间件抛出了自定义异常,所以我实际上根本没有看过 Apartment,但也许是这样的:
#app/middleware/apartment/rescued_apartment_middleware.rb
module Apartment
class RescuedApartmentMiddleware < Apartment::Middleware
def call(env)
begin
super
rescue Apartment::SchemaNotFound
env[:apartment_schema_not_found] = true # to be later referenced in your ApplicationController
@app.call(env) # the middleware call method should return this, but it was probably short-circuited by the raise
end
end
end
end
Run Code Online (Sandbox Code Playgroud)
然后在你的环境中:
config.middleware.use(Apartment::RescuedApartmentMiddleware, etc)
Run Code Online (Sandbox Code Playgroud)
要访问从 ApplicationController 或任何控制器设置的环境变量:
if request.env[:apartment_schema_not_found]
#handle
end
Run Code Online (Sandbox Code Playgroud)
如何从 Ruby on Rails 应用程序中的 OAuth::Unauthorized 异常中拯救出来的组合?以及如何从 Rails 内部访问 Rack 环境?