Skip to content

Instantly share code, notes, and snippets.

@thbar
Last active December 14, 2015 06:59
Show Gist options
  • Save thbar/5046900 to your computer and use it in GitHub Desktop.
Save thbar/5046900 to your computer and use it in GitHub Desktop.
Slightly more paranoid better_errors activation.

Despite existing checks in the gem, I want to make sure it's not possible to activate better_errors at all in production (eg: erroneous RAILS_ENV configuration set to development, etc).

Here better_errors should be activated only if a .use_better_errors file is in place and if I'm on Mac OS X.

Not sure if this is necessary given the existing checks in the gem, but I suspect it cannot hurt either.

Next step: find a way to avoid putting them in the Gemfile completely.

require 'rbconfig'
YourApp::Application.configure do
# ...
if File.exists?(Rails.root.join('.use_better_errors')) && RbConfig::CONFIG['host_os'] =~ /darwin/
require 'better_errors'
# make sure we show proper app frames
BetterErrors.application_root = Rails.root.to_s
config.middleware.use BetterErrors::Middleware
end
end
group :development do
# require those manually to make sure they do not automatically show up
gem 'better_errors', require: false
gem 'binding_of_caller', require: false
end
@dmathieu
Copy link

If you happen to have an erroneous RAILS_ENV set to development in production, you're going to end up with other problems anyway (and you will have default rails errors anyway).

Perhaps a better approach to the problem would be to configure database.yml only for production and not dev, so the server won't start if you aren't using the right environment.
And to use bundle install --deployment when deploying to not install the development and test gems.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment