Permalink
Browse files

added in some better exception handling and notification of Rack::OAu…

…th exceptions
  • Loading branch information...
1 parent e92e58a commit a19b0b8379c7cc9f25851d4dc4457ad47a6804f9 @markburns markburns committed Jan 1, 2013
@@ -1,8 +1,9 @@
module Shutl
module Auth
- class Shutl::Error < ::StandardError; end
- class InvalidUrl < Shutl::Error; end
- class InvalidCredentials < Shutl::Error; end
+ class Shutl::Error < ::StandardError; end
+ class InvalidUrl < Shutl::Error; end
+ class InvalidCredentials < Shutl::Error; end
+ class InternalServerError < Shutl::Error; end
def access_token!
access_token_response!.access_token
@@ -14,8 +15,13 @@ def access_token_response!
Shutl::NetworkRetry.retry "Authentication Service Error" do
begin
c.access_token!
- rescue Rack::OAuth2::Client::Error
- raise_invalid_credentials
+ rescue Rack::OAuth2::Client::Error => e
+ case e.message
+ when /The client identifier provided is invalid, the client failed to authenticate, the client did not include its credentials, provided multiple client credentials, or used unsupported credentials type\./
+ raise_invalid_credentials
+ else
+ raise_internal_server_error e
+ end
end
end
end
@@ -56,6 +62,12 @@ def raise_invalid_uri
def raise_invalid_credentials
raise Shutl::Auth::InvalidCredentials, "Invalid credentials set, please see https://github.com/shutl/shutl_auth/blob/master/README.md"
end
+
+ def raise_internal_server_error e
+ Shutl.notify e
+ raise Shutl::Auth::InternalServerError
+ end
+
extend self
end
end
@@ -2,26 +2,25 @@
#authenticating requests
module Shutl
module Auth
- module AuthenticatedRequest
+ module Session
+ def session
+ @session ||= {}
+ end
+ end
- #If a method called session is not defined in the included object
- #then it will be defined
+ module AuthenticatedRequest
def self.included base
unless base.instance_methods.include? :session
- raise egg
base.class_eval do
- define_method :session do
- @session ||= {}
- end
+ include Shutl::Auth::Session
end
end
end
def request_access_token
- return if session[:access_token]
+ return session[:access_token] if session[:access_token]
- access_token_response = Shutl::Auth.access_token!
- access_token_response.access_token
+ Shutl::Auth.access_token!
end
def access_token
View
@@ -1,6 +1,14 @@
module Shutl
- def self.notifier_klass= klass
- NetworkRetry.notifier_klass = klass
+ class << self
+ def notifier_klass= klass
+ NetworkRetry.notifier_klass = klass
+ end
+
+ def notifier_klass
+ NetworkRetry.notifier_klass
+ end
+
+ delegate :notify, to: :notifier_klass
end
module NetworkRetry
@@ -40,6 +40,20 @@ def set_auth
end
end
+ specify "with 500 from auth server" do
+ set_auth
+
+ stub_request(:post, /.*#{Shutl::Auth.url}.*/).to_return(
+ { body: '',
+ status: 500,
+ headers: {"CONTENT_TYPE" => 'application/json'}}
+ )
+
+ Airbrake.should_receive(:notify)
+
+ expect{ Shutl::Auth.access_token!}.to raise_error Shutl::Auth::InternalServerError
+ end
+
specify "with invalid credentials" do
set_auth
Shutl::Auth.client_id = 'egg'
@@ -48,6 +62,13 @@ def set_auth
expect { Shutl::Auth.access_token!}.to raise_error Shutl::Auth::InvalidCredentials
end
+ set_auth
+ Shutl::Auth.client_id = 'egg'
+
+ VCR.use_cassette 'invalid_credentials' do
+ expect { Shutl::Auth.access_token!}.to raise_error Shutl::Auth::InvalidCredentials
+ end
+
end
end
end

0 comments on commit a19b0b8

Please sign in to comment.