Пример #1
0
        private MongoException PrepareException(WithTransactionErrorState state)
        {
            var mongoException = new MongoException("test");

            switch (state)
            {
            case WithTransactionErrorState.TransientTransactionError:
            {
                mongoException.AddErrorLabel(TransientTransactionErrorLabel);
                return(mongoException);
            }

            case WithTransactionErrorState.UnknownTransactionCommitResult:
            {
                mongoException.AddErrorLabel(UnknownTransactionCommitResultLabel);
                return(mongoException);
            }

            case WithTransactionErrorState.NoError:
                return(null);

            case WithTransactionErrorState.ErrorWithoutLabel:
            {
                return(mongoException);
            }
            }

            throw new ArgumentException("Not supported ErrorState", state.ToString());
        }
Пример #2
0
 // public static methods
 public static void AddRetryableWriteErrorLabelIfRequired(MongoException exception, SemanticVersion serverVersion)
 {
     if (ShouldRetryableWriteExceptionLabelBeAdded(exception, serverVersion))
     {
         exception.AddErrorLabel(RetryableWriteErrorLabel);
     }
 }
Пример #3
0
        private void LogAndThrow(string msg, MongoException e)
        {
            string completeMsg = msg + ", Message: " + e.Message + ", Trace: " + "/n" + e.StackTrace;

            Log.Error(completeMsg);
            throw new Exception(completeMsg);
        }
Пример #4
0
 // public static methods
 public static void AddRetryableWriteErrorLabelIfRequired(MongoException exception, int maxWireVersion)
 {
     if (ShouldRetryableWriteExceptionLabelBeAdded(exception, maxWireVersion))
     {
         exception.AddErrorLabel(RetryableWriteErrorLabel);
     }
 }
Пример #5
0
        protected void LogConnectionException(MongoException ex)
        {
            if (ex.InnerException is TimeoutException)
            {
                Log.DebugFormat("Encountered System.TimeoutException: {0}", ex.InnerException.Message);
            }

            Log.ErrorFormat("Unable to create client connection to MongoDB: {0}", ex.Message);
        }
Пример #6
0
        public void TryMe()
        {
            if (++_callCount < _maxThrows)
            {
                var ex = new MongoException("Error");

                ex.AddErrorLabel("TransientTransactionError");

                throw ex;
            }
        }
        private bool ShouldAddTransientTransactionError(MongoException exception)
        {
            if (_session.IsInTransaction)
            {
                if (exception is MongoConnectionException)
                {
                    return(true);
                }
            }

            return(false);
        }
Пример #8
0
        public string handleMongoExceptionAsync(HttpContext context, MongoException ex)
        {
            var message = "Huston we got a database problem";

            context.Response.ContentType = "application/json";
            context.Response.StatusCode  = (int)HttpStatusCode.BadRequest;
            if (Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT") == "Development")
            {
                message = ex.ToString();// ------------------------------------------FOR DEVELOPMENT PURPOSE
            }
            return(message);
        }
Пример #9
0
 /// <summary>
 /// 初始化 <see cref="RepositoryException"/> 类的新实例。
 /// </summary>
 /// <param name="ex">引发当前异常的 <see cref="MongoException"/> 异常。</param>
 public RepositoryException(MongoException ex)
     : base(RepositoryExceptionMessage, ex)
 {
     if (ex is MongoWriteException writeException)
     {
         ErrorCode = RepositoryErrorCodeHelper.FromMongoServerErrorCategory(writeException.WriteError.Category);
     }
     else
     {
         ErrorCode = RepositoryErrorCode.Unknown;
     }
 }
 public static bool IsDuplicateKey(this MongoException ex)
 {
     if (ex is MongoCommandException c && c.Code == 11000)
     {
         return(true);
     }
     if (ex is MongoWriteException w && w.WriteError.Category == ServerErrorCategory.DuplicateKey)
     {
         return(true);
     }
     return(false);
 }
        public bool CheckSync(Exception ex)
        {
            MongoException realEx = (MongoException)ex;

            if (realEx.HasErrorLabel("UnknownTransactionCommitResult") || realEx.HasErrorLabel("TransientTransactionError"))
            {
                return(true);
            }
            else
            {
                return(false);
            }
        }
            public void WhenAVersionStepFails_UpdatesBaselineToLastSucceededStep()
            {
                A.CallTo(() => _baseliner.GetBaseline())
                .Returns(1);

                var failingStep = _migrationSteps.Single(_ => _.Version == 3);
                var failure     = new MongoException("Failed!");

                failingStep.SetFailure(failure);

                Func <Task> action = () => _sut.Migrate();

                action.Should().Throw <MongoException>();

                var expectedExecutedStep = _migrationSteps.Single(_ => _.Version == 2);

                A.CallTo(() => _baseliner.SetBaseline(expectedExecutedStep))
                .MustHaveHappened();
            }
Пример #13
0
        public void AddRetryableWriteErrorLabelIfRequired_should_not_add_error_label_for_non_retryWrites_server(
            [Values(false, true)] bool isNetworkError)
        {
            MongoException exception = null;

            if (isNetworkError)
            {
                exception = (MongoException)CoreExceptionHelper.CreateException(typeof(MongoConnectionException));
            }
            else
            {
                exception = CoreExceptionHelper.CreateMongoCommandException((int)ServerErrorCode.HostNotFound);
            }

            RetryabilityHelper.AddRetryableWriteErrorLabelIfRequired(exception, Feature.RetryableWrites.LastNotSupportedVersion);

            var hasRetryableWriteErrorLabel = exception.HasErrorLabel("RetryableWriteError");

            hasRetryableWriteErrorLabel.Should().BeFalse();
        }
Пример #14
0
        public static int GetErrorCode(this MongoException exception)
        {
            int errorCode = UNKNOWN_MONGO_ERROR_CODE;

            var queryException = exception as MongoQueryException;

            if (queryException != null && queryException.QueryResult != null)
            {
                errorCode = GetMongoErrorCode(queryException.QueryResult);
            }

            var commandException = exception as MongoCommandException;

            if (commandException != null &&
                commandException.CommandResult != null &&
                commandException.CommandResult.Response != null)
            {
                errorCode = GetMongoErrorCode(commandException.CommandResult.Response);
            }

            return(errorCode);
        }
            public void WhenAVersionStepFails_Throws_AndDoesNotTryOtherSteps()
            {
                A.CallTo(() => _baseliner.GetBaseline())
                .Returns(1);

                var failingStep = _migrationSteps.Single(_ => _.Version == 3);
                var failure     = new MongoException("Failed!");

                failingStep.SetFailure(failure);

                Func <Task> action = () => _sut.Migrate();

                action.Should().Throw <MongoException>().Where(_ => _ == failure);

                var expectedUnexecutedSteps = _migrationSteps.Where(_ => _.Version > 3);

                foreach (var migrationStep in expectedUnexecutedSteps)
                {
                    migrationStep.Runs.Count().Should().Be(0);
                    A.CallTo(() => _baseliner.SetBaseline(migrationStep))
                    .MustNotHaveHappened();
                }
            }
Пример #16
0
 // private methods
 private void ReplaceTransientTransactionErrorWithUnknownTransactionCommitResult(MongoException exception)
 {
     exception.RemoveErrorLabel("TransientTransactionError");
     exception.AddErrorLabel("UnknownTransactionCommitResult");
 }
Пример #17
0
        private bool ShouldReplaceTransientTransactionErrorWithUnknownTransactionCommitResult(MongoException exception)
        {
            if (exception is MongoConnectionException)
            {
                return(true);
            }

            if (exception is MongoNotPrimaryException || exception is MongoNodeIsRecoveringException)
            {
                return(true);
            }

            var writeConcernException = exception as MongoWriteConcernException;

            if (writeConcernException != null)
            {
                var writeConcernError = writeConcernException.WriteConcernResult.Response?.GetValue("writeConcernError", null)?.AsBsonDocument;
                if (writeConcernError != null)
                {
                    var code = (ServerErrorCode)writeConcernError.GetValue("code", -1).ToInt32();
                    switch (code)
                    {
                    case ServerErrorCode.UnsatisfiableWriteConcern:
                    case ServerErrorCode.UnknownReplWriteConcern:
                        return(false);

                    default:
                        return(true);
                    }
                }
            }

            return(false);
        }
 /// <summary>
 /// GetThreatLevel of Generic MongoException
 /// </summary>
 /// <param name="exceptione"></param>
 /// <returns>Level</returns>
 public LogLevels.Levels GetThreatLevel(MongoException exceptione)
 {
     return(LogLevels.Levels.Warning);
 }