Przeglądaj źródła

Raise a useful error when trying to activate_session with nil

Chris Saunders 10 lat temu
rodzic
commit
bb8e6122dd
2 zmienionych plików z 8 dodań i 0 usunięć
  1. 2 0
      lib/shopify_api/resources/base.rb
  2. 6 0
      test/base_test.rb

+ 2 - 0
lib/shopify_api/resources/base.rb

@@ -2,6 +2,7 @@ require 'shopify_api/version'
 
 module ShopifyAPI
   class Base < ActiveResource::Base
+    class InvalidSessionError < StandardError; end
     extend Countable
     self.include_root_in_json = false
     self.headers['User-Agent'] = ["ShopifyAPI/#{ShopifyAPI::VERSION}",
@@ -49,6 +50,7 @@ module ShopifyAPI
       end
 
       def activate_session(session)
+        raise InvalidSessionError.new("Session cannot be nil") if session.nil?
         self.site = session.site
         self.headers.merge!('X-Shopify-Access-Token' => session.token)
       end

+ 6 - 0
test/base_test.rb

@@ -51,6 +51,12 @@ class BaseTest < Test::Unit::TestCase
     assert_equal 'token2', ShopifyAPI::Shop.headers['X-Shopify-Access-Token']
   end
 
+  test '#activate_session with nil raises an InvalidSessionError' do
+    assert_raises ShopifyAPI::Base::InvalidSessionError do
+      ShopifyAPI::Base.activate_session nil
+    end
+  end
+
   test "#delete should send custom headers with request" do
     ShopifyAPI::Base.activate_session @session1
     ShopifyAPI::Base.headers['X-Custom'] = 'abc'