123456789_123456789_123456789_123456789_123456789_

Exception: Mongo::Error::BulkWriteError

Relationships & Source Files
Super Chains via Extension / Inclusion / Inheritance
Class Chain:
self, ::Mongo::Error, StandardError
Instance Chain:
Inherits: Mongo::Error
  • Object
Defined in: lib/mongo/error/bulk_write_error.rb

Overview

Note:

A bulk operation that resulted in a BulkWriteError may have written some of the documents to the database. If the bulk write was unordered, writes may have also continued past the write that produced a BulkWriteError.

Exception raised if there are write errors upon executing a bulk operation.

Unlike OperationFailure, BulkWriteError does not currently expose individual error components (such as the error code). The result document (which can be obtained using the result attribute) provides detailed error information and can be examined by the application if desired.

Since:

  • 2.0.0

Constant Summary

::Mongo::Error - Inherited

BAD_VALUE, CODE, CURSOR_NOT_FOUND, ERR, ERRMSG, ERROR, TRANSIENT_TRANSACTION_ERROR_LABEL, UNKNOWN_ERROR, UNKNOWN_TRANSACTION_COMMIT_RESULT_LABEL, WRITE_CONCERN_ERROR, WRITE_CONCERN_ERRORS, WRITE_ERRORS

Class Method Summary

::Mongo::Error - Inherited

Instance Attribute Summary

::Mongo::Error - Inherited

#change_stream_resumable?

Can the change stream on which this error occurred be resumed, provided the operation that triggered this error was a getMore?

ChangeStreamResumable - Included

#change_stream_resumable?

Can the change stream on which this error occurred be resumed, provided the operation that triggered this error was a getMore?

WriteRetryable - Included

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 - Inherited

#write_concern_error_label?

Does the write concern error have the given label?

#write_concern_error_labels

The set of error labels associated with the write concern 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.

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(result) ⇒ BulkWriteError

Instantiate the new exception.

Examples:

Instantiate the exception.

Mongo::Error::BulkWriteError.new(response)

Parameters:

  • result (Hash)

    A processed response from the server reporting results of the operation.

Since:

  • 2.0.0

[ GitHub ]

  
# File 'lib/mongo/error/bulk_write_error.rb', line 49

def initialize(result)
  @result = result
  # Exception constructor behaves differently for a nil argument and
  # for no argument. Avoid passing nil explicitly.
  super(*[build_message])
end

Instance Attribute Details

#resultBSON::Document (readonly)

Returns:

  • (BSON::Document)

    result The error result.

Since:

  • 2.0.0

[ GitHub ]

  
# File 'lib/mongo/error/bulk_write_error.rb', line 38

attr_reader :result

Instance Method Details

#build_messageString (private)

Generates an error message when there are multiple write errors.

col has validation { ‘validator’ => { ‘x’ => { ‘$type’ => ‘string’ } } } col.insert_many([1, 2], ordered: false)

Multiple errors:

[121]: Document failed validation --
  {"failingDocumentId":1,"details":{"operatorName":"$type",
  "specifiedAs":{"x":{"$type":"string"}},"reason":"field was
  missing"}};
[121]: Document failed validation --
  {"failingDocumentId":2, "details":{"operatorName":"$type",
  "specifiedAs":{"x":{"$type":"string"}}, "reason":"field was
  missing"}}

Examples:

Multiple documents fail validation

Returns:

  • (String)

    The error message

Since:

  • 2.0.0

[ GitHub ]

  
# File 'lib/mongo/error/bulk_write_error.rb', line 76

def build_message
  errors = @result['writeErrors']
  return nil unless errors

  fragment = ""
  cut_short = false
  errors.first(10).each_with_index do |error, i|
    fragment += "; " if fragment.length > 0
    fragment += "[#{error['code']}]: #{error['errmsg']}"
    fragment += " -- #{error['errInfo'].to_json}" if error['errInfo']

    if fragment.length > 3000
      cut_short = i < [9, errors.length].min
      break
    end
  end

  fragment += '...' if errors.length > 10 || cut_short

  if errors.length > 1
    fragment = "Multiple errors: #{fragment}"
  end

  fragment
end