Permalink
Browse files

raise exception if initializing middleware with missing dependencies

Previously it would just try to use the middleware and it would result
in unexplained errors when making requests.
  • Loading branch information...
1 parent 86925fd commit 394c62cd36c80790d88de929beaa44dceb1e4de7 @mislav mislav committed Jan 20, 2012
Showing with 27 additions and 4 deletions.
  1. +0 −2 lib/faraday/adapter/test.rb
  2. +14 −1 lib/faraday/middleware.rb
  3. +1 −1 test/adapters/live_test.rb
  4. +12 −0 test/middleware_stack_test.rb
@@ -14,8 +14,6 @@ class Adapter
class Test < Faraday::Adapter
attr_accessor :stubs
- def self.loaded?() false end
-
class Stubs
class NotFound < StandardError
end
@@ -4,6 +4,7 @@ class Middleware
class << self
attr_accessor :load_error, :supports_parallel_requests
+ private :load_error=
alias supports_parallel_requests? supports_parallel_requests
# valid parallel managers should respond to #run with no parameters.
@@ -13,15 +14,27 @@ def setup_parallel_manager(options = {})
end
end
+ self.load_error = nil
+
# Executes a block which should try to require and reference dependent libraries
def self.dependency(lib = nil)
lib ? require(lib) : yield
rescue LoadError, NameError => error
self.load_error = error
end
+ def self.new(*)
+ raise "missing dependency for #{self}: #{load_error.message}" unless loaded?
+ super
+ end
+
def self.loaded?
- @load_error.nil?
+ load_error.nil?
+ end
+
+ def self.inherited(subclass)
+ super
+ subclass.send(:load_error=, self.load_error)
end
def initialize(app = nil)
@@ -9,7 +9,7 @@ class LiveTest < Faraday::TestCase
ENV['ADAPTER'].split(':').map { |name| Faraday::Adapter.lookup_middleware name.to_sym }
else
loaded_adapters = Faraday::Adapter.all_loaded_constants
- loaded_adapters -= [Faraday::Adapter::ActionDispatch]
+ loaded_adapters -= [Faraday::Adapter::Test, Faraday::Adapter::ActionDispatch]
# https://github.com/geemus/excon/issues/98
loaded_adapters -= [Faraday::Adapter::Excon] if defined? RUBY_ENGINE and "rbx" == RUBY_ENGINE
loaded_adapters << :default
@@ -12,6 +12,10 @@ class Apple < Handler; end
class Orange < Handler; end
class Banana < Handler; end
+ class Broken < Faraday::Middleware
+ dependency 'zomg/i_dont/exist'
+ end
+
def setup
@conn = Faraday::Connection.new
@builder = @conn.builder
@@ -129,6 +133,14 @@ def test_registered_symbol_with_type
end
end
+ def test_missing_dependencies
+ build_stack Broken
+ err = assert_raises RuntimeError do
+ @conn.get('/')
+ end
+ assert_equal "missing dependency for MiddlewareStackTest::Broken: cannot load such file -- zomg/i_dont/exist", err.message
+ end
+
private
# make a stack with test adapter that reflects the order of middleware

0 comments on commit 394c62c

Please sign in to comment.