Skip to content

Commit

Permalink
MONGOID-4919 Use Mongo::Error::SessionsNotSupported for checking sess…
Browse files Browse the repository at this point in the history
…ion support
  • Loading branch information
p committed Jul 10, 2020
1 parent bbee04c commit 2cfaea7
Showing 1 changed file with 20 additions and 4 deletions.
24 changes: 20 additions & 4 deletions lib/mongoid/clients/sessions.rb
Original file line number Diff line number Diff line change
Expand Up @@ -41,12 +41,20 @@ module Sessions
#
# @since 6.4.0
def with_session(options = {})
raise Mongoid::Errors::InvalidSessionUse.new(:invalid_session_nesting) if Threaded.get_session
if Threaded.get_session
raise Mongoid::Errors::InvalidSessionUse.new(:invalid_session_nesting)
end
session = persistence_context.client.start_session(options)
Threaded.set_session(session)
yield(session)
rescue Mongo::Error::InvalidSession => ex
if ex.message == Mongo::Session::SESSIONS_NOT_SUPPORTED
if
# Driver 2.13.0+
defined?(Mongo::Error::SessionsNotSupported) &&
Mongo::Error::SessionsNotSupported === ex or
# Legacy drivers
ex.message == Mongo::Session::SESSIONS_NOT_SUPPORTED
then
raise Mongoid::Errors::InvalidSessionUse.new(:sessions_not_supported)
end
raise Mongoid::Errors::InvalidSessionUse.new(:invalid_session_use)
Expand Down Expand Up @@ -92,12 +100,20 @@ module ClassMethods
#
# @since 6.4.0
def with_session(options = {})
raise Mongoid::Errors::InvalidSessionUse.new(:invalid_session_nesting) if Threaded.get_session
if Threaded.get_session
raise Mongoid::Errors::InvalidSessionUse.new(:invalid_session_nesting)
end
session = persistence_context.client.start_session(options)
Threaded.set_session(session)
yield(session)
rescue Mongo::Error::InvalidSession => ex
if ex.message == Mongo::Session::SESSIONS_NOT_SUPPORTED
if
# Driver 2.13.0+
defined?(Mongo::Error::SessionsNotSupported) &&
Mongo::Error::SessionsNotSupported === ex or
# Legacy drivers
ex.message == Mongo::Session::SESSIONS_NOT_SUPPORTED
then
raise Mongoid::Errors::InvalidSessionUse.new(:sessions_not_supported)
end
raise Mongoid::Errors::InvalidSessionUse.new(:invalid_session_use)
Expand Down

0 comments on commit 2cfaea7

Please sign in to comment.