/* Copyright 2010-present MongoDB Inc. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ using System; using System.Runtime.Serialization; using System.Text; using MongoDB.Driver.Core.Connections; namespace MongoDB.Driver { /// /// Represents a write exception. /// public class MongoWriteException : MongoServerException { // static internal static MongoWriteException FromBulkWriteException(MongoBulkWriteException bulkException) { var writeConcernError = bulkException.WriteConcernError; var writeError = bulkException.WriteErrors.Count > 0 ? bulkException.WriteErrors[0] : null; return new MongoWriteException(bulkException.ConnectionId, writeError, writeConcernError, bulkException); } // private fields private readonly WriteConcernError _writeConcernError; private readonly WriteError _writeError; /// /// Initializes a new instance of the class. /// /// The connection identifier. /// The write error. /// The write concern error. /// The inner exception. public MongoWriteException( ConnectionId connectionId, WriteError writeError, WriteConcernError writeConcernError, Exception innerException) : base(connectionId, FormatMessage(writeError, writeConcernError), innerException) { _writeError = writeError; _writeConcernError = writeConcernError; if (_writeConcernError != null) { foreach (var errorLabel in _writeConcernError.ErrorLabels) { AddErrorLabel(errorLabel); } } } /// /// Initializes a new instance of the MongoQueryException class (this overload supports deserialization). /// /// The SerializationInfo. /// The StreamingContext. public MongoWriteException(SerializationInfo info, StreamingContext context) : base(info, context) { _writeConcernError = (WriteConcernError)info.GetValue("_writeConcernError", typeof(WriteConcernError)); _writeError = (WriteError)info.GetValue("_writeError", typeof(WriteError)); if (_writeConcernError != null) { foreach (var errorLabel in _writeConcernError.ErrorLabels) { AddErrorLabel(errorLabel); } } } // properties /// /// Gets the write concern error. /// public WriteConcernError WriteConcernError { get { return _writeConcernError; } } /// /// Gets the write error. /// public WriteError WriteError { get { return _writeError; } } // methods /// /// Gets the object data. /// /// The information. /// The context. public override void GetObjectData(SerializationInfo info, StreamingContext context) { base.GetObjectData(info, context); info.AddValue("_writeConcernError", _writeConcernError); info.AddValue("_writeError", _writeError); } // private static methods private static string FormatMessage(WriteError writeError, WriteConcernError writeConcernError) { var sb = new StringBuilder("A write operation resulted in an error."); if (writeError != null) { sb.Append($" WriteError: {writeError}."); } if (writeConcernError != null) { sb.Append($" WriteConcernError: {writeConcernError}."); } return sb.ToString(); } } }