Can you ask ruby to treat warnings as errors?
There is unfortunately no real way of doing this, at least not on most versions of Ruby out there (variations may exist), short of monitoring the program output and aborting it when a warning appears on standard error. Here's why:
- Ruby defines
Kernel.warn
, which you can redefine to do whatever you wish (including exiting), and which you'd expect (hope) to be used consistently by Ruby to report warnings (including internal e.g. parsing warning), but - methods implemented natively (in C) inside Ruby will in turn directly invoke a native method called
rb_warn
fromsource/server.c
, completely bypassing your redefinition ofKernel.warn
(e.g. the "string literal in condition
" warning, for example, issued when doing something like:do_something if 'string'
, is printed via the nativerb_warn
fromsource/parse.c
) - to make things even worse, there is an additional,
rb_warning
native method, which can be used by Ruby to log warnings if-w
or-v
is specified.
So, if you need to take action solely on warnings generated by your application code's calling Kernel.warn
then simply redefine Kernel.warn
. Otherwise, you have exactly two options:
- alter
source/error.c
to exit inrb_warn
andrb_warning
(andrb_warn_m
?), and rebuild Ruby - monitor your program's standard error output for '
: warning:
', and abort it on match
How can I make Rubocop treat some errors as warnings instead of errors?
The answer turned out to be fairly simple. you can specify the Severity of a cop:
Metrics/MethodLength:
Max: 100
Severity: warning
The question ends up being slightly silly because I found the answer right where I'd been looking for it--in the documentation.
Ruby on Rails: Treat deprecation warnings as errors or otherwise find deprecated code?
You can customise the behaviour of deprecated calls by setting ActiveSupport::Deprecation.behavior
. This should be set to a Proc
that accepts a message and a callstack e.g. you could do:
ActiveSupport::Deprecation.behavior = Proc.new { |message, callstack|
raise message + "\n" + callstack.join("\n ")
}
If you have automated tests for your app these are invaluable when upgrading the version of Rails being used.
Capturing warning messages
You can redirect stderr
to a StringIO object to capture the warnings output in a string:
require 'stringio'
old_stderr = $stderr
$stderr = StringIO.new
Foo = 1
Foo = 2 # generates a warning
puts $stderr.string # prints the warning
$stderr = old_stderr
Ruby how to make exit on warning?
Here is one way to override the Kernel#warn
module Kernel
alias orig_warn warn
def warn args
orig_warn args
exit
end
end
puts "Foo"
warn "Bar"
puts "Don't want to see this"
Throw exception when re-assigning a constant in Ruby?
Look at Can you ask ruby to treat warnings as errors? to see how it is possible in some cases to treat warnings as errors.
Otherwise I guess you'd have to write a custom method to assign constants and raise the exception if already assigned.
If you know that a reassignment happens to a specific constant, you can also add a sanity check just before the assignment.
Efficient way to report record validation warnings as well as errors?
Here is some code I wrote for a Rails 3 project that does exactly what you're talking about here.
# Define a "warnings" validation bucket on ActiveRecord objects.
#
# @example
#
# class MyObject < ActiveRecord::Base
# warning do |vehicle_asset|
# unless vehicle_asset.description == 'bob'
# vehicle_asset.warnings.add(:description, "should be 'bob'")
# end
# end
# end
#
# THEN:
#
# my_object = MyObject.new
# my_object.description = 'Fred'
# my_object.sensible? # => false
# my_object.warnings.full_messages # => ["Description should be 'bob'"]
module Warnings
module Validations
extend ActiveSupport::Concern
include ActiveSupport::Callbacks
included do
define_callbacks :warning
end
module ClassMethods
def warning(*args, &block)
options = args.extract_options!
if options.key?(:on)
options = options.dup
options[:if] = Array.wrap(options[:if])
options[:if] << "validation_context == :#{options[:on]}"
end
args << options
set_callback(:warning, *args, &block)
end
end
# Similar to ActiveModel::Validations#valid? but for warnings
def sensible?
warnings.clear
run_callbacks :warning
warnings.empty?
end
# Similar to ActiveModel::Validations#errors but returns a warnings collection
def warnings
@warnings ||= ActiveModel::Errors.new(self)
end
end
end
ActiveRecord::Base.send(:include, Warnings::Validations)
The comments at the top show how to use it. You can put this code into an initializer and then warnings should be available to all of your ActiveRecord objects. And then basically just add a warnings do
block to the top of each model that can have warnings and just manually add as many warnings as you want. This block won't be executed until you call .sensible?
on the model.
Also, note that since warnings are not validation errors, a model will still be technically valid even if it isn't "sensible" (as I called it).
Related Topics
Rails4 Unknown Encoding Name - Cp720
Ruby on Rails - Access Controller Variable from Model
Rails Paperclip How to Delete Attachment
Unzip (Zip, Tar, Tag.Gz) Files With Ruby
Rails Engines Extending Functionality
Ruby: Class Instance Variables VS Instance Variables
Rvm Warning! Path Is Not Properly Set Up
The <<- Operator on Ruby, Where Is It Documented
Test If Variable Matches Any of Several Strings W/O Long If-Elsif Chain, or Case-When
Differencebetween Raising Exceptions VS Throwing Exceptions in Ruby
Why Do I Get a "Permission Denied" Error While Installing a Gem
Can You Ask Ruby to Treat Warnings as Errors
Passing a Method as a Parameter in Ruby
In Ruby, Is There an Array Method That Combines 'Select' and 'Map'
How to Run All Tests With Minitest
How to Run Untrusted Ruby Code Inside a Safe Sandbox