[英]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']
设置为适合您环境的值( production
, development
等)。 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 deployment
将ENV['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 的框架通常具有更高级别的解决方案。
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
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.