Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

MONGOID-4919 Use Mongo::Error::SessionsNotSupported for checking session support #4812

Merged
merged 2 commits into from
Jul 13, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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 ||
# 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 ||
# 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