简体   繁体   English

如何关闭 Rack 应用程序中的异常?

[英]How do I turn off exceptions in a Rack app?

I'm ready to deploy a RackServerPages application but can't seem to find a way to disable exceptions ie, the one rendered by Rack::ShowExceptions.我已准备好部署 RackServerPages 应用程序,但似乎无法找到禁用异常的方法,即由 Rack::ShowExceptions 呈现的异常。

Thanks!谢谢!

Set the RACK_ENV environment variable to deployment . RACK_ENV环境变量设置为deployment

Technically, setting ENV['RACK_ENV'] to anything besides development will disable the exceptions. 从技术上讲,将ENV['RACK_ENV']为除development之外的任何内容都将禁用异常。 Rack::ShowExceptions middleware is included by default when rack is running in the default development environment . 当机架在默认development环境中运行时, 默认情况下 包含 Rack::ShowExceptions中间件。

For Rails apps, set ENV['RACK_ENV'] to deployment , making sure you also set ENV['RAILS_ENV'] to the correct value for your environment ( production , development , etc.). 对于Rails应用程序,将ENV['RACK_ENV']deployment ,确保您还将ENV['RAILS_ENV']设置为适合您环境的值( productiondevelopment等)。 If ENV['RAILS_ENV'] is not set, the Rails app will fallback to ENV['RACK_ENV'] and Rails will complain about an unknown deployment environment. 如果没有设置ENV['RAILS_ENV'] ,Rails应用程序将回ENV['RACK_ENV'] ,Rails会抱怨未知的deployment环境。

If you use unicorn , you can alternatively use -E deployment to set ENV['RACK_ENV'] to deployment . 如果使用unicorn ,也可以使用-E deploymentENV['RACK_ENV']deployment

I have several hour unpleasant experience of unsuccessfully trying to disable Rack::ShowExceptions but in the end I discovered I don't need to do that. 我有几个小时不愉快的经历,试图禁用Rack::ShowExceptions但最后我发现我不需要这样做。

In production this is turned off (when you try to curl -XINVALID -k https://my-production-app.com it will return just blank screen). 在生产中,这是关闭的(当您尝试curl -XINVALID -k https://my-production-app.com ,它将仅返回空白屏幕)。

But this won't solve the issue if you need to disable this in custom (eg "staging") environment (still showing the rack trace code.) 但是,如果您需要在自定义(例如“staging”)环境中禁用此功能(仍显示机架跟踪代码),则无法解决此问题。

tested on Rails 3.2.21 在Rails 3.2.21上测试

on Rails 4.0.12 this works for my production and custom "staging" environment 在Rails 4.0.12这适用于我的生产和自定义“登台”环境

As ENV['RACK_ENV'] is the universal Rack solution, Rack-based framework often have higher level solutions.由于ENV['RACK_ENV']是通用的 Rack 解决方案,基于 Rack 的框架通常具有更高级别的解决方案。

Examples例子

Sinatra西纳特拉

In the config.rb it is possible to define :environment .config.rb中可以定义:environment It is also possible to use ENV['APP_ENV'] .也可以使用ENV['APP_ENV']

configure do
  set :environment, :production
end

Roda罗达

There is an error handling plugin .有一个错误处理插件

class App < Roda
  plugin :error_handler do |e|
    'Oh No!'
  end
end

And also an environment plugin.还有一个环境插件。

class App < Roda
  plugin :environments

  self.environment = :production
end

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM