Exemple #1
0
 /// <summary>
 /// Initializes a new instance of the <see cref="MongoWriteConcernException"/> class.
 /// </summary>
 /// <param name="info">The SerializationInfo.</param>
 /// <param name="context">The StreamingContext.</param>
 public MongoWriteConcernException(SerializationInfo info, StreamingContext context)
     : base(info, context)
 {
     _writeConcernResult = (WriteConcernResult)info.GetValue("_writeConcernResult", typeof(WriteConcernResult));
     _ = TryMapWriteConcernResultToException(ConnectionId, _writeConcernResult, out _writeConcernResultException);
     AddErrorLabelsFromWriteConcernResult(this, _writeConcernResult);
 }
        public void TraceRecordId(string collectionName, WriteConcernResult wcr, ObjectId dataId)
        {
            try
            {
                Trace.TraceInformation("Starting TraceRecordId...");
                if (wcr != null)
                {
                    if (wcr.Ok == false)
                    {
                        if (wcr.ErrorMessage != null)
                            Trace.TraceError(wcr.ErrorMessage);
                    }

                    Trace.TraceInformation("Record ID (" + collectionName + ") inserted into Mongo: " + dataId);
                }
                else
                {
                    Trace.TraceInformation(
                        "Check to see that WriteConcern = 1 (in ServiceConfiguration.Cloud.cscfg), if you want to see the IDs of inserted records in this log. ",
                        "Information");
                }
            }
            catch (Exception ex)
            {
                Trace.TraceError(ex.Message);
            }

            Trace.TraceInformation("Finishing TraceRecordId.");
        }
Exemple #3
0
 // constructors
 /// <summary>
 /// Initializes a new instance of the <see cref="MongoWriteConcernException"/> class.
 /// </summary>
 /// <param name="connectionId">The connection identifier.</param>
 /// <param name="message">The error message.</param>
 /// <param name="writeConcernResult">The command result.</param>
 public MongoWriteConcernException(ConnectionId connectionId, string message, WriteConcernResult writeConcernResult)
     : base(connectionId, message, null, writeConcernResult.Response)
 {
     _writeConcernResult = Ensure.IsNotNull(writeConcernResult, nameof(writeConcernResult));
     _ = TryMapWriteConcernResultToException(ConnectionId, _writeConcernResult, out _writeConcernResultException);
     AddErrorLabelsFromWriteConcernResult(this, _writeConcernResult);
 }
        /// <summary>
        /// Maps the specified writeConcernResult to a custom exception (if necessary).
        /// </summary>
        /// <param name="writeConcernResult">The write concern result.</param>
        /// <returns>
        /// The custom exception (or null if the writeConcernResult was not mapped to an exception).
        /// </returns>
        public static Exception Map(WriteConcernResult writeConcernResult)
        {
            if (writeConcernResult.Code.HasValue)
            {
                switch (writeConcernResult.Code.Value)
                {
                case 11000:
                case 11001:
                case 12582:
                    var errorMessage = string.Format(
                        "WriteConcern detected an error '{0}'. (Response was {1}).",
                        writeConcernResult.ErrorMessage, writeConcernResult.Response.ToJson());
                    return(new MongoDuplicateKeyException(errorMessage, writeConcernResult));
                }
            }

            if (!writeConcernResult.Ok)
            {
                var errorMessage = string.Format(
                    "WriteConcern detected an error '{0}'. (Response was {1}).",
                    writeConcernResult.ErrorMessage, writeConcernResult.Response.ToJson());
                return(new WriteConcernException(errorMessage, writeConcernResult));
            }

            if (writeConcernResult.HasLastErrorMessage)
            {
                var errorMessage = string.Format(
                    "WriteConcern detected an error '{0}'. (Response was {1}).",
                    writeConcernResult.LastErrorMessage, writeConcernResult.Response.ToJson());
                return(new WriteConcernException(errorMessage, writeConcernResult));
            }

            return(null);
        }
Exemple #5
0
        public void constructor_should_should_map_writeConcernResult(object exceptionInfo, Type expectedExceptionType)
        {
            var response = new BsonDocument
            {
                {
                    "writeConcernError",
                    Enum.TryParse <ServerErrorCode>(exceptionInfo.ToString(), out var errorCode)
                        ? new BsonDocument("code", (int)errorCode)
                        : new BsonDocument("errmsg", exceptionInfo.ToString())
                }
            };
            var writeConcernResult    = new WriteConcernResult(response);
            var writeConcernException = new MongoWriteConcernException(_connectionId, "dummy", writeConcernResult);

            var result = writeConcernException.MappedWriteConcernResultException;

            if (expectedExceptionType != null)
            {
                result.GetType().Should().Be(expectedExceptionType);
            }
            else
            {
                result.Should().BeNull();
            }
        }
        /// <summary>
        /// Maps the specified writeConcernResult to a custom exception (if necessary).
        /// </summary>
        /// <param name="writeConcernResult">The write concern result.</param>
        /// <returns>
        /// The custom exception (or null if the writeConcernResult was not mapped to an exception).
        /// </returns>
        public static Exception Map(WriteConcernResult writeConcernResult)
        {
            var code = GetCode(writeConcernResult.Response);
            if (code.HasValue)
            {
                switch(code.Value)
                {
                    case 11000:
                    case 11001:
                    case 12582:
                        var errorMessage = string.Format(
                            "WriteConcern detected an error '{0}'. (Response was {1}).",
                            writeConcernResult.ErrorMessage, writeConcernResult.Response.ToJson());
                        return new MongoDuplicateKeyException(errorMessage, writeConcernResult);
                }
            }

            if (!writeConcernResult.Ok)
            {
                var errorMessage = string.Format(
                    "WriteConcern detected an error '{0}'. (Response was {1}).",
                    writeConcernResult.ErrorMessage, writeConcernResult.Response.ToJson());
                return new WriteConcernException(errorMessage, writeConcernResult);
            }

            if (writeConcernResult.HasLastErrorMessage)
            {
                var errorMessage = string.Format(
                    "WriteConcern detected an error '{0}'. (Response was {1}).",
                    writeConcernResult.LastErrorMessage, writeConcernResult.Response.ToJson());
                return new WriteConcernException(errorMessage, writeConcernResult);
            }

            return null;
        }
 public ObjectId AddCorporation(Corporation build)
 {
     build.Id = ObjectId.GenerateNewId();
     _corporation.Insert(build);
     WriteConcernResult x = new WriteConcernResult(response: build.ToBsonDocument());
     return build.Id;
 }
 void EnsureResultIsGood(WriteConcernResult writeConcernResult, string message, params object[] objs)
 {
     if (!writeConcernResult.Ok)
     {
         throw new ApplicationException(
             string.Format("The following operation didn't suceed: {0} - the result was: {1}",
                           string.Format(message, objs),
                           writeConcernResult.ErrorMessage));
     }
 }
        /// <summary>
        /// Maps the specified writeConcernResult to a custom exception (if necessary).
        /// </summary>
        /// <param name="writeConcernResult">The write concern result.</param>
        /// <returns>
        /// The custom exception (or null if the writeConcernResult was not mapped to an exception).
        /// </returns>
        public static Exception Map(WriteConcernResult writeConcernResult)
        {
            var code = GetCode(writeConcernResult.Response);

            if (code.HasValue)
            {
                switch (code.Value)
                {
                case 11000:
                case 11001:
                case 12582:
                    var errorMessage = string.Format(
                        "WriteConcern detected an error '{0}'. (Response was {1}).",
                        writeConcernResult.ErrorMessage, writeConcernResult.Response.ToJson());
                    return(new MongoDuplicateKeyException(errorMessage, writeConcernResult));
                }
            }

            if (!writeConcernResult.Ok)
            {
                var errorMessage = string.Format(
                    "WriteConcern detected an error '{0}'. (Response was {1}).",
                    writeConcernResult.ErrorMessage, writeConcernResult.Response.ToJson());
                return(new WriteConcernException(errorMessage, writeConcernResult));
            }

            bool   wnoteAndJnoteAreErrors = writeConcernResult.ServerInstance.BuildInfo.Version >= new Version(2, 6, 0);
            string lastErrorMessage       = null;

            if (writeConcernResult.HasLastErrorMessage)
            {
                lastErrorMessage = writeConcernResult.LastErrorMessage;
            }
            else if (wnoteAndJnoteAreErrors)
            {
                if (writeConcernResult.Response.Contains("jnote"))
                {
                    lastErrorMessage = writeConcernResult.Response["jnote"].ToString();
                }
                else if (writeConcernResult.Response.Contains("wnote"))
                {
                    lastErrorMessage = writeConcernResult.Response["wnote"].ToString();
                }
            }

            if (lastErrorMessage != null)
            {
                var errorMessage = string.Format(
                    "WriteConcern detected an error '{0}'. (Response was {1}).",
                    lastErrorMessage, writeConcernResult.Response.ToJson());
                return(new WriteConcernException(errorMessage, writeConcernResult));
            }

            return(null);
        }
        public void TestDoesNotThrowExceptionWhenEverythingIsKosherWithAWriteConcernResult()
        {
            var response = new BsonDocument
            {
                { "n", 1 },
                { "connectionId", 1 },
                { "ok", 1 }
            };

            var writeConcernResult = new WriteConcernResult(response);
            var ex = ExceptionMapper.Map(writeConcernResult);

            Assert.IsNull(ex);
        }
        public void TestThrowsDuplicateKeyExceptionForMongod(int code)
        {
            var response = new BsonDocument
            {
                { "ok", 1 },
                { "err", string.Format("E{0} duplicate key error index: test.foo.$_id_  dup key: {{ : 1.0 }}", code) },
                { "code", code },
                { "n", 0 },
                { "connectionId", 1 }
            };

            var writeConcernResult = new WriteConcernResult(response);
            var ex = ExceptionMapper.Map(writeConcernResult);

            Assert.IsNotNull(ex);
            Assert.IsInstanceOf<MongoDuplicateKeyException>(ex);
        }
 // constructors
 /// <summary>
 /// Initializes a new instance of the WriteConcernException class.
 /// </summary>
 /// <param name="message">The error message.</param>
 /// <param name="writeConcernResult">The command result.</param>
 public WriteConcernException(string message, WriteConcernResult writeConcernResult)
     : base(message, null, writeConcernResult.Response)
 {
     _writeConcernResult = Ensure.IsNotNull(writeConcernResult, "writeConcernResult");
 }
Exemple #13
0
 // constructors
 /// <summary>
 /// Initializes a new instance of the WriteConcernException class.
 /// </summary>
 /// <param name="message">The error message.</param>
 /// <param name="writeConcernResult">The command result.</param>
 public WriteConcernException(string message, WriteConcernResult writeConcernResult)
     : base(message, writeConcernResult)
 {
 }
Exemple #14
0
        void CheckResult(WriteConcernResult result, int expectedNumberOfAffectedDocuments)
        {
            if (!result.Ok)
            {
                throw new MongoWriteConcernException("Not OK result returned from the server", result);
            }

            if (result.DocumentsAffected != expectedNumberOfAffectedDocuments)
            {
                throw new MongoWriteConcernException(string.Format("DocumentsAffected != {0}", expectedNumberOfAffectedDocuments), result);
            }
        }
Exemple #15
0
 /// <summary>
 /// Initializes a new instance of the <see cref="MongoDuplicateKeyException"/> class.
 /// </summary>
 /// <param name="message">The error message.</param>
 /// <param name="commandResult">The command result.</param>
 public MongoDuplicateKeyException(ConnectionId connectionId, string message, WriteConcernResult commandResult)
     : base(connectionId, message, commandResult)
 {
 }
Exemple #16
0
        internal void SaveState()
        {
            var res = new WriteConcernResult();
            for (var i = 0; i < 3; i++)
            {
                res = Data.DbContext.Get().GameCollection.Save(State);
                if(res.Ok)return;

                Thread.Sleep(1000 * i);
            }
            Log.ErrorFormat("Could not save state of game {0} in DB: {1}",State.Id,res.ErrorMessage ?? res.LastErrorMessage ?? "");
        }
 /// <summary>
 /// Initializes a new instance of the <see cref="MongoWriteConcernException"/> class.
 /// </summary>
 /// <param name="info">The SerializationInfo.</param>
 /// <param name="context">The StreamingContext.</param>
 public MongoWriteConcernException(SerializationInfo info, StreamingContext context)
     : base(info, context)
 {
     _writeConcernResult = (WriteConcernResult)info.GetValue("_writeConcernResult", typeof(WriteConcernResult));
 }
 // constructors
 /// <summary>
 /// Initializes a new instance of the <see cref="MongoWriteConcernException"/> class.
 /// </summary>
 /// <param name="connectionId">The connection identifier.</param>
 /// <param name="message">The error message.</param>
 /// <param name="writeConcernResult">The command result.</param>
 public MongoWriteConcernException(ConnectionId connectionId, string message, WriteConcernResult writeConcernResult)
     : base(connectionId, message, null, writeConcernResult.Response)
 {
     _writeConcernResult = Ensure.IsNotNull(writeConcernResult, nameof(writeConcernResult));
 }
Exemple #19
0
        private static bool TryMapWriteConcernResultToException(ConnectionId connectionId, WriteConcernResult writeConcernResult, out Exception mappedException)
        {
            var responseDocument = writeConcernResult?.Response;

            if (responseDocument != null && responseDocument.TryGetValue("writeConcernError", out var writeConcernError))
            {
                if (writeConcernError.IsBsonDocument)
                {
                    var writeConcernErrorDocument = writeConcernError.AsBsonDocument;
                    mappedException =
                        ExceptionMapper.MapNotPrimaryOrNodeIsRecovering(connectionId, command: null, writeConcernErrorDocument, "errmsg") ??
                        ExceptionMapper.Map(connectionId, writeConcernErrorDocument);
                    return(true);
                }
            }

            mappedException = null;
            return(false);
        }
Exemple #20
0
 private static void AddErrorLabelsFromWriteConcernResult(MongoWriteConcernException exception, WriteConcernResult writeConcernResult)
 {
     // note: make a best effort to extract the error labels from the writeConcernResult, but never throw an exception
     if (writeConcernResult != null && writeConcernResult.Response != null)
     {
         if (writeConcernResult.Response.TryGetValue("writeConcernError", out var writeConcernError) &&
             writeConcernError.IsBsonDocument)
         {
             AddErrorLabelsFromCommandResult(exception, writeConcernError.AsBsonDocument);
         }
     }
 }
Exemple #21
0
 /// <summary>
 /// Initializes a new instance of the <see cref="MongoDuplicateKeyException"/> class.
 /// </summary>
 /// <param name="message">The error message.</param>
 /// <param name="commandResult">The command result.</param>
 public MongoDuplicateKeyException(string message, WriteConcernResult commandResult)
     : base(message, commandResult)
 {
 }
        public void TestThrowsDuplicateKeyExceptionForMongos(int code)
        {
            var response = new BsonDocument
            {
                { "ok", 1 },
                { "err", string.Format("E{0} duplicate key error index 1", code) },
                { "errObjects", new BsonArray
                    {
                        new BsonDocument
                        {
                            { "ok", 1 },
                            { "err", string.Format("E{0} duplicate key error index 1", code) },
                            { "code", code }
                        },
                        new BsonDocument
                        {
                            { "ok", 1 },
                            { "err", string.Format("E{0} duplicate key error index 2", code) },
                            { "code", code }
                        },
                    } }
            };

            var writeConcernResult = new WriteConcernResult(response);
            var ex = ExceptionMapper.Map(writeConcernResult);

            Assert.IsNotNull(ex);
            Assert.IsInstanceOf<MongoDuplicateKeyException>(ex);
        }
 /// <summary>
 /// Initializes a new instance of the <see cref="MongoDuplicateKeyException"/> class.
 /// </summary>
 /// <param name="message">The error message.</param>
 /// <param name="commandResult">The command result.</param>
 public MongoDuplicateKeyException(string message, WriteConcernResult commandResult)
     : base(message, commandResult)
 {
 }
 // constructors
 /// <summary>
 /// Initializes a new instance of the WriteConcernException class.
 /// </summary>
 /// <param name="message">The error message.</param>
 /// <param name="writeConcernResult">The command result.</param>
 public WriteConcernException(string message, WriteConcernResult writeConcernResult)
     : base(message, writeConcernResult)
 {
 }
 // constructors
 /// <summary>
 /// Initializes a new instance of the <see cref="MongoWriteConcernException"/> class.
 /// </summary>
 /// <param name="connectionId">The connection identifier.</param>
 /// <param name="message">The error message.</param>
 /// <param name="writeConcernResult">The command result.</param>
 public MongoWriteConcernException(ConnectionId connectionId, string message, WriteConcernResult writeConcernResult)
     : base(connectionId, message, null, writeConcernResult.Response)
 {
     _writeConcernResult = Ensure.IsNotNull(writeConcernResult, "writeConcernResult");
 }
 /// <summary>
 /// Initializes a new instance of the WriteConcernException class (this overload supports deserialization).
 /// </summary>
 /// <param name="info">The SerializationInfo.</param>
 /// <param name="context">The StreamingContext.</param>
 public WriteConcernException(SerializationInfo info, StreamingContext context)
     : base(info, context)
 {
     _writeConcernResult = new WriteConcernResult(BsonSerializer.Deserialize <BsonDocument>((byte[])info.GetValue("_query", typeof(byte[]))));
 }
 // private methods
 private void CheckExpectedResult(ExpectedWriteConcernResult expectedResult, WriteConcernResult result)
 {
     Assert.AreEqual(expectedResult.DocumentsAffected ?? 0, result.DocumentsAffected);
     Assert.AreEqual(expectedResult.HasLastErrorMessage ?? false, result.HasLastErrorMessage);
     if (expectedResult.LastErrorMessage != null)
     {
         Assert.AreEqual(expectedResult.LastErrorMessage, result.LastErrorMessage);
     }
     Assert.AreEqual(expectedResult.Upserted, result.Upserted);
     Assert.AreEqual(expectedResult.UpdatedExisting ?? false, result.UpdatedExisting);
 }
 private void ThrowIfNotOk(WriteConcernResult result)
 {
     if (!result.Ok)
     {
         throw new Exception("MongoDB write failed");
     }
 }
 public MongoDbEntityAddedEvent(ICommandRepository commandRepository, object entity, WriteConcernResult result)
     : base(commandRepository, entity)
 {
     WriteConcernResult = result;
 }
        public void TestThrowsWriteConcernExceptionWhenOkButHasLastErrorMessage()
        {
            var response = new BsonDocument
            {
                { "err", "oops" },
                { "code", 20 },
                { "n", 0 },
                { "connectionId", 1 },
                { "ok", 1 }
            };

            var writeConcernResult = new WriteConcernResult(response);
            var ex = ExceptionMapper.Map(writeConcernResult);

            Assert.IsNotNull(ex);
            Assert.IsInstanceOf<WriteConcernException>(ex);
        }
 /// <summary>
 /// Initializes a new instance of the <see cref="MongoWriteConcernException"/> class.
 /// </summary>
 /// <param name="info">The SerializationInfo.</param>
 /// <param name="context">The StreamingContext.</param>
 public MongoWriteConcernException(SerializationInfo info, StreamingContext context)
     : base(info, context)
 {
     _writeConcernResult = (WriteConcernResult)info.GetValue("_writeConcernResult", typeof(WriteConcernResult));
 }
 // constructors
 /// <summary>
 /// Initializes a new instance of the WriteConcernException class.
 /// </summary>
 /// <param name="message">The error message.</param>
 /// <param name="writeConcernResult">The command result.</param>
 public WriteConcernException(string message, WriteConcernResult writeConcernResult)
     : base(message, null, writeConcernResult.Response)
 {
     _writeConcernResult = Ensure.IsNotNull(writeConcernResult, "writeConcernResult");
 }
 /// <summary>
 /// Initializes a new instance of the <see cref="MongoDuplicateKeyException" /> class.
 /// </summary>
 /// <param name="connectionId">The connection identifier.</param>
 /// <param name="message">The error message.</param>
 /// <param name="commandResult">The command result.</param>
 public MongoDuplicateKeyException(ConnectionId connectionId, string message, WriteConcernResult commandResult)
     : base(connectionId, message, commandResult)
 {
 }
 /// <summary>
 /// Initializes a new instance of the WriteConcernException class (this overload supports deserialization).
 /// </summary>
 /// <param name="info">The SerializationInfo.</param>
 /// <param name="context">The StreamingContext.</param>
 public WriteConcernException(SerializationInfo info, StreamingContext context)
     : base(info, context)
 {
     _writeConcernResult = new WriteConcernResult(BsonSerializer.Deserialize<BsonDocument>((byte[])info.GetValue("_query", typeof(byte[]))));
 }