123456789_123456789_123456789_123456789_123456789_

Exception: Mongo::Auth::Unauthorized

Relationships & Source Files
Super Chains via Extension / Inclusion / Inheritance
Class Chain:
self, ::Mongo::Error::AuthError, RuntimeError
Instance Chain:
Inherits: Mongo::Error::AuthError
Defined in: lib/mongo/auth.rb

Overview

Raised when a user is not authorized on a database.

Since:

  • 2.0.0

Constant Summary

::Mongo::Error::ReadWriteRetryable - Included

RETRY_MESSAGES, WRITE_RETRY_ERRORS, WRITE_RETRY_MESSAGES

Class Method Summary

Instance Attribute Summary

::Mongo::Error::ReadWriteRetryable - Included

#retryable?

Whether the error is a retryable error according to the legacy read retry logic.

#write_retryable?

Whether the error is a retryable error according to the modern retryable reads and retryable writes specifications.

#write_retryable_code?

::Mongo::Error::Notable - Included

#connection_global_id

Returns global id of the connection on which the error occurred.

#generation

Returns connection pool generation for the connection on which the error occurred.

#service_id

Returns service id for the connection on which the error occurred.

Instance Method Summary

::Mongo::Error::Labelable - Included

#add_label

Adds the specified label to the error instance, if the label is not already in the set of labels.

#label?

Does the error have the given label?

#labels

Gets the set of labels associated with the error.

::Mongo::Error::Notable - Included

#add_note,
#add_notes

Allows multiple notes to be added in a single call, for convenience.

#notes

Returns an array of strings with additional information about the exception.

#to_s, #notes_tail

Constructor Details

.new(user, used_mechanism: nil, message: nil, server: nil, code: nil) ⇒ Unauthorized

Instantiate the new error.

Examples:

Instantiate the error.

Mongo::Auth::Unauthorized.new(user)

Parameters:

  • user (Mongo::Auth::User)

    The unauthorized user.

  • used_mechanism (String)

    ::Mongo::Auth mechanism actually used for authentication. This is a full string like SCRAM-SHA-256.

  • message (String)

    The error message returned by the server.

  • server (Server)

    The server instance that authentication was attempted against.

  • The (Integer)

    error code.

Since:

  • 2.0.0

[ GitHub ]

  
# File 'lib/mongo/auth.rb', line 153

def initialize(user, used_mechanism: nil, message: nil,
  server: nil, code: nil
)
  @code = code

  configured_bits = []
  used_bits = [
    "auth source: #{user.auth_source}",
  ]

  if user.mechanism
    configured_bits << "mechanism: #{user.mechanism}"
  end

  if used_mechanism
    used_bits << "used mechanism: #{used_mechanism}"
  end

  if server
    used_bits << "used server: #{server.address} (#{server.status})"
  end

  used_user = if user.mechanism == :mongodb_x509
    'Client certificate'
  else
    "User #{user.name}"
  end

  if configured_bits.empty?
    configured_bits = ''
  else
    configured_bits = " (#{configured_bits.join(', ')})"
  end

  used_bits = " (#{used_bits.join(', ')})"

  msg = "#{used_user}#{configured_bits} is not authorized to access #{user.database}#{used_bits}"
  if message
    msg += ': ' + message
  end
  super(msg)
end

Instance Attribute Details

#codeInteger (readonly)

Returns:

  • (Integer)

    The error code.

Since:

  • 2.0.0

[ GitHub ]

  
# File 'lib/mongo/auth.rb', line 137

attr_reader :code