MongoWriteException.cs 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. /* Copyright 2010-present MongoDB Inc.
  2. *
  3. * Licensed under the Apache License, Version 2.0 (the "License");
  4. * you may not use this file except in compliance with the License.
  5. * You may obtain a copy of the License at
  6. *
  7. * http://www.apache.org/licenses/LICENSE-2.0
  8. *
  9. * Unless required by applicable law or agreed to in writing, software
  10. * distributed under the License is distributed on an "AS IS" BASIS,
  11. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. * See the License for the specific language governing permissions and
  13. * limitations under the License.
  14. */
  15. using System;
  16. #if NET452
  17. using System.Runtime.Serialization;
  18. #endif
  19. using System.Text;
  20. using MongoDB.Driver.Core.Connections;
  21. namespace MongoDB.Driver
  22. {
  23. /// <summary>
  24. /// Represents a write exception.
  25. /// </summary>
  26. #if NET452
  27. [Serializable]
  28. #endif
  29. public class MongoWriteException : MongoServerException
  30. {
  31. // static
  32. internal static MongoWriteException FromBulkWriteException(MongoBulkWriteException bulkException)
  33. {
  34. var writeConcernError = bulkException.WriteConcernError;
  35. var writeError = bulkException.WriteErrors.Count > 0
  36. ? bulkException.WriteErrors[0]
  37. : null;
  38. return new MongoWriteException(bulkException.ConnectionId, writeError, writeConcernError, bulkException);
  39. }
  40. // private fields
  41. private readonly WriteConcernError _writeConcernError;
  42. private readonly WriteError _writeError;
  43. /// <summary>
  44. /// Initializes a new instance of the <see cref="MongoWriteException" /> class.
  45. /// </summary>
  46. /// <param name="connectionId">The connection identifier.</param>
  47. /// <param name="writeError">The write error.</param>
  48. /// <param name="writeConcernError">The write concern error.</param>
  49. /// <param name="innerException">The inner exception.</param>
  50. public MongoWriteException(
  51. ConnectionId connectionId,
  52. WriteError writeError,
  53. WriteConcernError writeConcernError,
  54. Exception innerException)
  55. : base(connectionId, FormatMessage(writeError, writeConcernError), innerException)
  56. {
  57. _writeError = writeError;
  58. _writeConcernError = writeConcernError;
  59. }
  60. #if NET452
  61. /// <summary>
  62. /// Initializes a new instance of the MongoQueryException class (this overload supports deserialization).
  63. /// </summary>
  64. /// <param name="info">The SerializationInfo.</param>
  65. /// <param name="context">The StreamingContext.</param>
  66. public MongoWriteException(SerializationInfo info, StreamingContext context)
  67. : base(info, context)
  68. {
  69. _writeConcernError = (WriteConcernError)info.GetValue("_writeConcernError", typeof(WriteConcernError));
  70. _writeError = (WriteError)info.GetValue("_writeError", typeof(WriteError));
  71. }
  72. #endif
  73. // properties
  74. /// <summary>
  75. /// Gets the write concern error.
  76. /// </summary>
  77. public WriteConcernError WriteConcernError
  78. {
  79. get { return _writeConcernError; }
  80. }
  81. /// <summary>
  82. /// Gets the write error.
  83. /// </summary>
  84. public WriteError WriteError
  85. {
  86. get { return _writeError; }
  87. }
  88. // methods
  89. #if NET452
  90. /// <summary>
  91. /// Gets the object data.
  92. /// </summary>
  93. /// <param name="info">The information.</param>
  94. /// <param name="context">The context.</param>
  95. public override void GetObjectData(SerializationInfo info, StreamingContext context)
  96. {
  97. base.GetObjectData(info, context);
  98. info.AddValue("_writeConcernError", _writeConcernError);
  99. info.AddValue("_writeError", _writeError);
  100. }
  101. #endif
  102. // private static methods
  103. private static string FormatMessage(WriteError writeError, WriteConcernError writeConcernError)
  104. {
  105. var sb = new StringBuilder("A write operation resulted in an error.");
  106. if (writeError != null)
  107. {
  108. sb.AppendLine().Append(" " + writeError.Message);
  109. }
  110. if (writeConcernError != null)
  111. {
  112. sb.AppendLine().Append(" " + writeConcernError.Message);
  113. }
  114. return sb.ToString();
  115. }
  116. }
  117. }