From ade8128a18ec3a053306e364e31687739de8c568 Mon Sep 17 00:00:00 2001 From: Alyssa Ross Date: Wed, 1 Feb 2017 18:30:55 +0000 Subject: formulary: handle ScriptError in formula I added a new `FormulaUnreadableError` subclass of `FormulaUnavailableError` so existing `rescue`s of `FormulaUnavailableError` handle this as well. The new subclass will output the name of the formula with the error (because this isn't always obvious from the original exception message) followed by the original error message. Fixes #1927. --- Library/Homebrew/exceptions.rb | 13 +++++++++++++ Library/Homebrew/formulary.rb | 6 +++++- Library/Homebrew/test/exceptions_test.rb | 5 +++++ 3 files changed, 23 insertions(+), 1 deletion(-) (limited to 'Library') diff --git a/Library/Homebrew/exceptions.rb b/Library/Homebrew/exceptions.rb index c5d888d64..77da4489e 100644 --- a/Library/Homebrew/exceptions.rb +++ b/Library/Homebrew/exceptions.rb @@ -131,6 +131,19 @@ class FormulaClassUnavailableError < FormulaUnavailableError end end +class FormulaUnreadableError < FormulaUnavailableError + attr_reader :formula_error + + def initialize(name, error) + super(name) + @formula_error = error + end + + def to_s + "#{name}: " + formula_error.to_s + end +end + class TapFormulaAmbiguityError < RuntimeError attr_reader :name, :paths, :formulae diff --git a/Library/Homebrew/formulary.rb b/Library/Homebrew/formulary.rb index 25df57cdc..cf85ba03f 100644 --- a/Library/Homebrew/formulary.rb +++ b/Library/Homebrew/formulary.rb @@ -22,7 +22,11 @@ class Formulary mod = Module.new const_set(namespace, mod) - mod.module_eval(contents, path) + begin + mod.module_eval(contents, path) + rescue ScriptError => e + raise FormulaUnreadableError.new(name, e) + end class_name = class_s(name) begin diff --git a/Library/Homebrew/test/exceptions_test.rb b/Library/Homebrew/test/exceptions_test.rb index 689531c6e..e9fedef04 100644 --- a/Library/Homebrew/test/exceptions_test.rb +++ b/Library/Homebrew/test/exceptions_test.rb @@ -56,6 +56,11 @@ class ExceptionsTest < Homebrew::TestCase FormulaClassUnavailableError.new("foo", "foo.rb", "Foo", list).to_s end + def test_formula_unreadable_error + formula_error = LoadError.new("bar") + assert_equal "foo: bar", FormulaUnreadableError.new("foo", formula_error).to_s + end + def test_tap_unavailable_error assert_equal "No available tap foo.\n", TapUnavailableError.new("foo").to_s end -- cgit v1.2.3