Can You Ask Ruby to Treat Warnings as Errors

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 from source/server.c, completely bypassing your redefinition of Kernel.warn (e.g. the "string literal in condition" warning, for example, issued when doing something like: do_something if 'string', is printed via the native rb_warn from source/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:

  1. alter source/error.c to exit in rb_warn and rb_warning (and rb_warn_m?), and rebuild Ruby
  2. 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



Leave a reply



Submit