From 1330b7441676da82ae89591b5d60e2e3f7b8ebe2 Mon Sep 17 00:00:00 2001
From: Mike McQuaid <mike@mikemcquaid.com>
Date: Tue, 18 Apr 2017 08:43:39 +0100
Subject: [PATCH] install: don't search when formula is unreadable.

These formulae are detected as missing but exist in an unreadable form.

Fixes #2485
---
 Library/Homebrew/cmd/install.rb |  3 +-
 Library/Homebrew/exceptions.rb  | 84 ++++++++++++++++++++++-----------
 Library/Homebrew/formulary.rb   |  4 ++
 3 files changed, 63 insertions(+), 28 deletions(-)

diff --git a/Library/Homebrew/cmd/install.rb b/Library/Homebrew/cmd/install.rb
index fd7aede868..394b31db04 100644
--- a/Library/Homebrew/cmd/install.rb
+++ b/Library/Homebrew/cmd/install.rb
@@ -206,7 +206,8 @@ module Homebrew
         Migrator.migrate_if_needed(f)
         install_formula(f)
       end
-    rescue FormulaClassUnavailableError => e
+    rescue FormulaUnreadableError, FormulaClassUnavailableError,
+           TapFormulaUnreadableError, TapFormulaClassUnavailableError => e
       # Need to rescue before `FormulaUnavailableError` (superclass of this)
       # is handled, as searching for a formula doesn't make sense here (the
       # formula was found, but there's a problem with its implementation).
diff --git a/Library/Homebrew/exceptions.rb b/Library/Homebrew/exceptions.rb
index cfdf5e12da..6751b22249 100644
--- a/Library/Homebrew/exceptions.rb
+++ b/Library/Homebrew/exceptions.rb
@@ -77,35 +77,11 @@ class FormulaUnavailableError < RuntimeError
   end
 end
 
-class TapFormulaUnavailableError < FormulaUnavailableError
-  attr_reader :tap, :user, :repo
-
-  def initialize(tap, name)
-    @tap = tap
-    @user = tap.user
-    @repo = tap.repo
-    super "#{tap}/#{name}"
-  end
-
-  def to_s
-    s = super
-    s += "\nPlease tap it and then try again: brew tap #{tap}" unless tap.installed?
-    s
-  end
-end
-
-class FormulaClassUnavailableError < FormulaUnavailableError
+module FormulaClassUnavailableErrorModule
   attr_reader :path
   attr_reader :class_name
   attr_reader :class_list
 
-  def initialize(name, path, class_name, class_list)
-    @path = path
-    @class_name = class_name
-    @class_list = class_list
-    super name
-  end
-
   def to_s
     s = super
     s += "\nIn formula file: #{path}"
@@ -131,16 +107,70 @@ class FormulaClassUnavailableError < FormulaUnavailableError
   end
 end
 
-class FormulaUnreadableError < FormulaUnavailableError
+class FormulaClassUnavailableError < FormulaUnavailableError
+  include FormulaClassUnavailableErrorModule
+
+  def initialize(name, path, class_name, class_list)
+    @path = path
+    @class_name = class_name
+    @class_list = class_list
+    super name
+  end
+end
+
+module FormulaUnreadableErrorModule
   attr_reader :formula_error
 
+  def to_s
+    "#{name}: " + formula_error.to_s
+  end
+end
+
+class FormulaUnreadableError < FormulaUnavailableError
+  include FormulaUnreadableErrorModule
+
   def initialize(name, error)
     super(name)
     @formula_error = error
   end
+end
+
+class TapFormulaUnavailableError < FormulaUnavailableError
+  attr_reader :tap, :user, :repo
+
+  def initialize(tap, name)
+    @tap = tap
+    @user = tap.user
+    @repo = tap.repo
+    super "#{tap}/#{name}"
+  end
 
   def to_s
-    "#{name}: " + formula_error.to_s
+    s = super
+    s += "\nPlease tap it and then try again: brew tap #{tap}" unless tap.installed?
+    s
+  end
+end
+
+class TapFormulaClassUnavailableError < TapFormulaUnavailableError
+  include FormulaClassUnavailableErrorModule
+
+  attr_reader :tap
+
+  def initialize(tap, name, path, class_name, class_list)
+    @path = path
+    @class_name = class_name
+    @class_list = class_list
+    super tap, name
+  end
+end
+
+class TapFormulaUnreadableError < TapFormulaUnavailableError
+  include FormulaUnreadableErrorModule
+
+  def initialize(tap, name, error)
+    super(tap, name)
+    @formula_error = error
   end
 end
 
diff --git a/Library/Homebrew/formulary.rb b/Library/Homebrew/formulary.rb
index 9401f57cbe..f5e6a2eb96 100644
--- a/Library/Homebrew/formulary.rb
+++ b/Library/Homebrew/formulary.rb
@@ -210,6 +210,10 @@ module Formulary
 
     def get_formula(spec, alias_path: nil)
       super
+    rescue FormulaUnreadableError => e
+      raise TapFormulaUnreadableError.new(tap, name, e.formula_error), "", e.backtrace
+    rescue FormulaClassUnavailableError => e
+      raise TapFormulaClassUnavailableError.new(tap, name, e.path, e.class_name, e.class_list), "", e.backtrace
     rescue FormulaUnavailableError => e
       raise TapFormulaUnavailableError.new(tap, name), "", e.backtrace
     end
-- 
GitLab