Skip to content

Commit

Permalink
Clean up the errors VCR raises.
Browse files Browse the repository at this point in the history
This also allows us to remove the unneeded VCR::HTTPStubbingAdapters::Common module.
  • Loading branch information
myronmarston committed Oct 3, 2011
1 parent 5e4c8ad commit b561c66
Show file tree
Hide file tree
Showing 13 changed files with 63 additions and 45 deletions.
6 changes: 3 additions & 3 deletions lib/vcr.rb
Expand Up @@ -5,6 +5,7 @@
require 'vcr/configuration'
require 'vcr/deprecations'
require 'vcr/http_stubbing_adapters'
require 'vcr/errors'
require 'vcr/request_matcher_registry'
require 'vcr/request_ignorer'
require 'vcr/version'
Expand All @@ -13,16 +14,15 @@

module VCR
include VariableArgsBlockCaller
include Errors

extend self

autoload :BasicObject, 'vcr/util/basic_object'
autoload :CucumberTags, 'vcr/test_frameworks/cucumber'
autoload :InternetConnection, 'vcr/util/internet_connection'
autoload :RSpec, 'vcr/test_frameworks/rspec'

class CassetteInUseError < StandardError; end
class TurnedOffError < StandardError; end

module Middleware
autoload :CassetteArguments, 'vcr/middleware/cassette_arguments'
autoload :Common, 'vcr/middleware/common'
Expand Down
4 changes: 1 addition & 3 deletions lib/vcr/cassette/reader.rb
@@ -1,7 +1,5 @@
module VCR
class Cassette
class MissingERBVariableError < NameError; end

class Reader
def initialize(file_name, erb)
@file_name, @erb = file_name, erb
Expand All @@ -20,7 +18,7 @@ def read
def handle_name_error(e)
example_hash = (erb_variables || {}).merge(e.name => 'some value')

raise MissingERBVariableError.new(
raise Errors::MissingERBVariableError.new(
"The ERB in the #{@file_name} cassette file references undefined variable #{e.name}. " +
"Pass it to the cassette using :erb => #{ example_hash.inspect }."
)
Expand Down
7 changes: 7 additions & 0 deletions lib/vcr/deprecations.rb
Expand Up @@ -9,4 +9,11 @@ def self.const_missing(const)
warn "WARNING: `VCR::Config` is deprecated. Use VCR.configuration instead."
configuration
end

def Cassette.const_missing(const)
return super unless const == :MissingERBVariableError
warn "WARNING: `VCR::Cassette::MissingERBVariableError` is deprecated. Use `VCR::Errors::MissingERBVariableError` instead."
Errors::MissingERBVariableError
end
end

19 changes: 19 additions & 0 deletions lib/vcr/errors.rb
@@ -0,0 +1,19 @@
module VCR
module Errors
class Error < StandardError; end
class CassetteInUseError < Error; end
class TurnedOffError < Error; end
class MissingERBVariableError < Error; end

class HTTPConnectionNotAllowedError < Error
def initialize(request)
super \
"Real HTTP connections are disabled. " +
"Request: #{request.method.to_s.upcase} #{request.uri}. " +
"You can use VCR to automatically record this request and replay it later. " +
"For more details, visit the VCR documentation at: http://relishapp.com/myronmarston/vcr/v/#{VCR.version.gsub('.', '-')}"
end
end
end
end

19 changes: 0 additions & 19 deletions lib/vcr/http_stubbing_adapters/common.rb

This file was deleted.

4 changes: 2 additions & 2 deletions lib/vcr/http_stubbing_adapters/excon.rb
@@ -1,4 +1,4 @@
require 'vcr/http_stubbing_adapters/common'
require 'vcr/util/version_checker'
require 'excon'

VCR::VersionChecker.new('Excon', Excon::VERSION, '0.6.5', '0.6').check_version!
Expand All @@ -21,7 +21,7 @@ def handle
when http_connections_allowed?
record_interaction
else
VCR::HTTPStubbingAdapters::Common.raise_connections_disabled_error(vcr_request)
raise VCR::HTTPConnectionNotAllowedError.new(vcr_request)
end
end

Expand Down
8 changes: 2 additions & 6 deletions lib/vcr/http_stubbing_adapters/fakeweb.rb
@@ -1,4 +1,4 @@
require 'vcr/http_stubbing_adapters/common'
require 'vcr/util/version_checker'
require 'fakeweb'
require 'net/http'
require 'vcr/extensions/net_http_response'
Expand Down Expand Up @@ -27,7 +27,7 @@ def handle
elsif real_http_connections_allowed?
perform_and_record_request
else
raise_connections_disabled_error
raise VCR::HTTPConnectionNotAllowedError.new(vcr_request)
end
end

Expand Down Expand Up @@ -60,10 +60,6 @@ def perform_request(&record_block)
net_http.request_without_vcr(request, request_body, &(record_block || block))
end

def raise_connections_disabled_error
VCR::HTTPStubbingAdapters::Common.raise_connections_disabled_error(vcr_request)
end

def uri
@uri ||= ::FakeWeb::Utility.request_uri_as_string(net_http, request)
end
Expand Down
2 changes: 1 addition & 1 deletion lib/vcr/http_stubbing_adapters/faraday.rb
@@ -1,4 +1,4 @@
require 'vcr/http_stubbing_adapters/common'
require 'vcr/util/version_checker'
require 'faraday'

VCR::VersionChecker.new('Faraday', Faraday::VERSION, '0.6.0', '0.6').check_version!
Expand Down
8 changes: 2 additions & 6 deletions lib/vcr/http_stubbing_adapters/typhoeus.rb
@@ -1,4 +1,4 @@
require 'vcr/http_stubbing_adapters/common'
require 'vcr/util/version_checker'
require 'typhoeus'

VCR::VersionChecker.new('Typhoeus', Typhoeus::VERSION, '0.2.1', '0.2').check_version!
Expand All @@ -24,7 +24,7 @@ def handle
elsif real_http_connections_allowed?
nil # allow the request to be performed and recorded
else
raise_connections_disabled_error
raise VCR::HTTPConnectionNotAllowedError.new(vcr_request)
end
end

Expand All @@ -34,10 +34,6 @@ def disabled?
VCR.http_stubbing_adapters.disabled?(:typhoeus)
end

def raise_connections_disabled_error
VCR::HTTPStubbingAdapters::Common.raise_connections_disabled_error(vcr_request)
end

def vcr_request
@vcr_request ||= self.class.vcr_request_from(request)
end
Expand Down
4 changes: 2 additions & 2 deletions lib/vcr/http_stubbing_adapters/webmock.rb
@@ -1,4 +1,4 @@
require 'vcr/http_stubbing_adapters/common'
require 'vcr/util/version_checker'
require 'webmock'

VCR::VersionChecker.new('WebMock', WebMock.version, '1.7.0', '1.7').check_version!
Expand Down Expand Up @@ -49,7 +49,7 @@ def disabled?
elsif VCR.real_http_connections_allowed?
false
else
VCR::HTTPStubbingAdapters::Common.raise_connections_disabled_error(vcr_request)
raise VCR::HTTPConnectionNotAllowedError.new(vcr_request)
end
}.to_return(lambda { |request|
response_hash_for VCR.http_interactions.response_for(vcr_request_from(request))
Expand Down
2 changes: 1 addition & 1 deletion lib/vcr/middleware/faraday.rb
Expand Up @@ -25,7 +25,7 @@ def call(env)
VCR.record_http_interaction(VCR::HTTPInteraction.new(request, response_for(env)))
response
else
VCR::HTTPStubbingAdapters::Common.raise_connections_disabled_error(request)
raise VCR::HTTPConnectionNotAllowedError.new(request)
end
end
end
Expand Down
4 changes: 2 additions & 2 deletions spec/vcr/cassette/reader_spec.rb
Expand Up @@ -34,7 +34,7 @@ def read(*args)
it 'raises an appropriate error when the ERB template needs variables' do
expect {
read('vars', true)
}.to raise_error(VCR::Cassette::MissingERBVariableError,
}.to raise_error(VCR::Errors::MissingERBVariableError,
%{The ERB in the vars cassette file references undefined variable var1. } +
%{Pass it to the cassette using :erb => #{ {:var1=>"some value"}.inspect }.}
)
Expand All @@ -49,7 +49,7 @@ def read(*args)
it 'raises an appropriate error when one or more of the needed variables are not passed' do
expect {
read('vars', :var1 => 'foo')
}.to raise_error(VCR::Cassette::MissingERBVariableError,
}.to raise_error(VCR::Errors::MissingERBVariableError,
%{The ERB in the vars cassette file references undefined variable var2. } +
%{Pass it to the cassette using :erb => #{ {:var1 => "foo", :var2 => "some value"}.inspect }.}
)
Expand Down
21 changes: 21 additions & 0 deletions spec/vcr/deprecations_spec.rb
Expand Up @@ -39,4 +39,25 @@
}.to raise_error(NameError)
end
end

describe "Cassette::MissingERBVariableError" do
it 'returns VCR::Errors::MissingERBVariableError' do
VCR::Cassette::MissingERBVariableError.should be(VCR::Errors::MissingERBVariableError)
end

it 'prints a deprecation warning' do
VCR::Cassette.should_receive(:warn).with \
"WARNING: `VCR::Cassette::MissingERBVariableError` is deprecated. Use `VCR::Errors::MissingERBVariableError` instead."

VCR::Cassette::MissingERBVariableError
end

it 'preserves the normal undefined constant behavior' do
expect {
VCR::Cassette::SomeUndefinedConstant
}.to raise_error(NameError)
end

end
end

0 comments on commit b561c66

Please sign in to comment.