Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I configure Rails to raise an error when it hits a deprecation warning?

Tags:

I'm upgrading a rather large app from Rails 3.0 to 3.2, and trying to get rid of all the deprecation warnings. My question isn't what to change to resolve particular warnings, but how to get Rails to treat them like exceptions so it will dump a stack trace in dev mode, so I'll have the full stack trace to see where the offending code is coming from. That way I can just use my test suite to find them all, and verify that they're all cleaned up. If there's a way to get rspec to fail a test if it logs a deprecation warning, that would work as well.

(In case anyone wonders, the reason I'm not upgrading all the way to rails 4.0 is that the app depends on some gems that AFAIK aren't fully ready for rails 4 yet, especially activerecord-oracle_enhanced-adapter.)

like image 653
sockmonk Avatar asked Sep 20 '13 18:09

sockmonk


2 Answers

In Rails 4 you can configure using

config.active_support.deprecation = :raise

in your test.rb or development.rb file.

Other available methods can be found in ActiveSupport::Deprecation::Behaviour

like image 52
Wolfgang Avatar answered Sep 19 '22 13:09

Wolfgang


The deprecation warning is handled by ActiveSupport::Deprecation, when some code want to show such warning, it calls

ActiveSupport::Deprecation.warn("some message")

There is no error thrown. To answer your question, I'm afraid you have to use dark tool of monkey patching :) Anyway the solution is for temp usage and will get removed later.

Update:

OP pointed out a quick and legit method in comment, so monkey patching is no longer a choice.

Here is his code:

# config/environments/test.rb
ActiveSupport::Deprecation.debug = true
like image 27
Billy Chan Avatar answered Sep 21 '22 13:09

Billy Chan