Ejemplo n.º 1
0
        public static RetryReason ResolveRetryReason(this CouchbaseException e)
        {
            switch (e)
            {
            case NotMyVBucketException _: return(RetryReason.KvNotMyVBucket);

            case DocumentLockedException _: return(RetryReason.KvLocked);

            case TemporaryFailureException _: return(RetryReason.KvTemporaryFailure);

            //case SocketNotAvailableException _: return RetryReason.SocketNotAvailable;
            //case SocketException _: return RetryReason.SocketClosedWhileInFlight;
            case DurableWriteInProgressException _: return(RetryReason.KvSyncWriteInProgress);

            case DurableWriteReCommitInProgressException _: return(RetryReason.KvSyncWriteReCommitInProgress);

            case ServiceNotAvailableException _: return(RetryReason.ServiceNotAvailable);

            case NodeNotAvailableException _: return(RetryReason.NodeNotAvailable);

            case KvErrorMapRetryException _: return(RetryReason.KvErrorMapRetryIndicated);

            //case ServiceResponseRetryException _: return RetryReason.ServiceResponseCodeIndicated;
            case PreparedStatementException _: return(RetryReason.QueryPreparedStatementFailure);

            case IndexFailureException _: return(RetryReason.QueryIndexNotFound);

            case SendQueueFullException _: return(RetryReason.SendQueueFull);

            default:
            {
                return(RetryReason.NoRetry);
            }
            }
        }
Ejemplo n.º 2
0
        public void TestCreateExceptions()
        {
            var fleeceException = CouchbaseException.Create(new C4Error(FLError.EncodeError)) as CouchbaseFleeceException;

            fleeceException.Should().NotBeNull();
            fleeceException.Error.Should().Be((int)FLError.EncodeError);
            fleeceException.Domain.Should().Be(CouchbaseLiteErrorType.Fleece);

            var sqliteException =
                CouchbaseException.Create(new C4Error(C4ErrorDomain.SQLiteDomain, (int)SQLiteStatus.Misuse)) as CouchbaseSQLiteException;

            sqliteException.Should().NotBeNull();
            sqliteException.BaseError.Should().Be(SQLiteStatus.Misuse);
            sqliteException.Error.Should().Be((int)SQLiteStatus.Misuse);
            sqliteException.Domain.Should().Be(CouchbaseLiteErrorType.SQLite);

            var webSocketException = CouchbaseException.Create(new C4Error(C4ErrorDomain.WebSocketDomain, 1003)) as CouchbaseWebsocketException;

            webSocketException.Error.Should().Be(CouchbaseLiteError.WebSocketDataError);
            webSocketException.Domain.Should().Be(CouchbaseLiteErrorType.CouchbaseLite);

            var posixException = CouchbaseException.Create(new C4Error(C4ErrorDomain.POSIXDomain, PosixBase.EACCES)) as CouchbasePosixException;

            posixException.Error.Should().Be(PosixBase.EACCES);
            posixException.Domain.Should().Be(CouchbaseLiteErrorType.POSIX);

            var networkException =
                CouchbaseException.Create(new C4Error(C4NetworkErrorCode.InvalidURL)) as CouchbaseNetworkException;

            networkException.Error.Should().Be(CouchbaseLiteError.InvalidUrl);
            networkException.Domain.Should().Be(CouchbaseLiteErrorType.CouchbaseLite);
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Checks whether or not a document with the given ID has any pending revisions to push
        /// </summary>
        /// <param name="documentID">The document ID</param>
        /// <returns>A bool which represents whether or not the document with the corresponding ID has one or more pending revisions.
        /// <c>true</c> means that one or more revisions have not been pushed to the remote yet,
        /// and <c>false</c> means that all revisions on the document have been pushed</returns>
        /// <exception cref="CouchbaseLiteException">Thrown if no push replication</exception>
        /// <exception cref="CouchbaseException">Thrown if an error condition is returned from LiteCore</exception>
        public bool IsDocumentPending([NotNull] string documentID)
        {
            CBDebug.MustNotBeNull(WriteLog.To.Sync, Tag, nameof(documentID), documentID);
            bool isDocPending = false;

            if (!IsPushing())
            {
                CBDebug.LogAndThrow(WriteLog.To.Sync,
                                    new CouchbaseLiteException(C4ErrorCode.Unsupported, CouchbaseLiteErrorMessage.PullOnlyPendingDocIDs),
                                    Tag, CouchbaseLiteErrorMessage.PullOnlyPendingDocIDs, true);
            }

            DispatchQueue.DispatchSync(() => {
                var errSetupRepl = SetupC4Replicator();
                if (errSetupRepl.code > 0)
                {
                    CBDebug.LogAndThrow(WriteLog.To.Sync, CouchbaseException.Create(errSetupRepl), Tag, errSetupRepl.ToString(), true);
                }
            });

            LiteCoreBridge.Check(err =>
            {
                isDocPending = Native.c4repl_isDocumentPending(_repl, documentID, err);
                return(isDocPending);
            });

            return(isDocPending);
        }
 internal ReplicatedDocument([NotNull] string docID, C4RevisionFlags flags, C4Error error,
                             bool isTransient)
 {
     Id          = docID;
     Flags       = flags.ToDocumentFlags();
     NativeError = error;
     Error       = error.domain == 0 ? null : CouchbaseException.Create(error);
     IsTransient = isTransient;
 }
        public void TestDatabaseInfo()
        {
            RunTestVariants(() => {
                Native.c4db_getDocumentCount(Db).Should().Be(0, "because the database is empty");
                Native.c4db_getLastSequence(Db).Should().Be(0, "because the database is empty");
                var publicID  = new C4UUID();
                var privateID = new C4UUID();
                C4Error err;
                var uuidSuccess = Native.c4db_getUUIDs(Db, &publicID, &privateID, &err);
                if (!uuidSuccess)
                {
                    throw CouchbaseException.Create(err);
                }

                var p1    = publicID;
                var p2    = privateID;
                var match = true;
                for (int i = 0; i < C4UUID.Size; i++)
                {
                    if (publicID.bytes[i] != privateID.bytes[i])
                    {
                        match = false;
                        break;
                    }
                }

                match.Should().BeFalse("because public UUID and private UUID should differ");
                (p1.bytes[6] & 0xF0).Should().Be(0x40, "because otherwise the UUID is non-conformant");
                (p1.bytes[8] & 0xC0).Should().Be(0x80, "because otherwise the UUID is non-conformant");
                (p2.bytes[6] & 0xF0).Should().Be(0x40, "because otherwise the UUID is non-conformant");
                (p2.bytes[8] & 0xC0).Should().Be(0x80, "because otherwise the UUID is non-conformant");

                // Make sure the UUIDs are persistent
                ReopenDB();
                var publicID2  = new C4UUID();
                var privateID2 = new C4UUID();
                uuidSuccess    = Native.c4db_getUUIDs(Db, &publicID2, &privateID2, &err);
                if (!uuidSuccess)
                {
                    throw CouchbaseException.Create(err);
                }

                for (int i = 0; i < C4UUID.Size; i++)
                {
                    publicID2.bytes[i].Should().Be(publicID.bytes[i]);
                    privateID2.bytes[i].Should().Be(privateID.bytes[i]);
                }
            });
        }
Ejemplo n.º 6
0
        public unsafe bool Execute(C4TryLogicDelegate1 block)
        {
            Debug.Assert(block != null);

            C4Error err;

            if (block(&err) || err.code == 0)
            {
                Exception = null;
                return(true);
            }

            Exception = CouchbaseException.Create(err);
            ThrowOrHandle();
            return(false);
        }
Ejemplo n.º 7
0
        private void UpdateStateProperties(C4ReplicatorStatus state)
        {
            Exception error = null;

            if (state.error.code > 0)
            {
                error = CouchbaseException.Create(state.error);
            }

            _rawStatus = state;
            var level    = (ReplicatorActivityLevel)state.level;
            var progress = new ReplicatorProgress(state.progress.unitsCompleted, state.progress.unitsTotal);

            Status = new ReplicatorStatus(level, progress, error);
            WriteLog.To.Sync.I(Tag, $"{this} is {state.level}, progress {state.progress.unitsCompleted}/{state.progress.unitsTotal}");
        }
Ejemplo n.º 8
0
        public unsafe FLSlice Execute(C4TryLogicDelegate4 block)
        {
            Debug.Assert(block != null);

            C4Error err;
            var     retVal = block(&err);

            if (retVal.buf != null || err.code == 0)
            {
                Exception = null;
                return(retVal);
            }

            Exception = CouchbaseException.Create(err);
            ThrowOrHandle();
            return(retVal);
        }
Ejemplo n.º 9
0
        public void TestCreateExceptions()
        {
            var fleeceException = CouchbaseException.Create(new C4Error(FLError.EncodeError)) as CouchbaseFleeceException;

            fleeceException.Should().NotBeNull();
            fleeceException.Error.Should().Be((int)FLError.EncodeError);
            fleeceException.Domain.Should().Be(CouchbaseLiteErrorType.Fleece);
            fleeceException = new CouchbaseFleeceException(FLError.JSONError);
            fleeceException = new CouchbaseFleeceException(FLError.JSONError, "json error");

            var sqliteException =
                CouchbaseException.Create(new C4Error(C4ErrorDomain.SQLiteDomain, (int)SQLiteStatus.Misuse)) as CouchbaseSQLiteException;

            sqliteException.Should().NotBeNull();
            sqliteException.BaseError.Should().Be(SQLiteStatus.Misuse);
            sqliteException.Error.Should().Be((int)SQLiteStatus.Misuse);
            sqliteException.Domain.Should().Be(CouchbaseLiteErrorType.SQLite);
            sqliteException = new CouchbaseSQLiteException(999991);
            sqliteException = new CouchbaseSQLiteException(999991, "new sql lite exception");

            var webSocketException = CouchbaseException.Create(new C4Error(C4ErrorDomain.WebSocketDomain, 1003)) as CouchbaseWebsocketException;

            webSocketException.Error.Should().Be(CouchbaseLiteError.WebSocketDataError);
            webSocketException.Domain.Should().Be(CouchbaseLiteErrorType.CouchbaseLite);
            webSocketException = new CouchbaseWebsocketException(10404);
            webSocketException = new CouchbaseWebsocketException(10404, "HTTP Not Found");

            var posixException = CouchbaseException.Create(new C4Error(C4ErrorDomain.POSIXDomain, PosixBase.EACCES)) as CouchbasePosixException;

            posixException.Error.Should().Be(PosixBase.EACCES);
            posixException.Domain.Should().Be(CouchbaseLiteErrorType.POSIX);
            posixException = new CouchbasePosixException(999992);
            posixException = new CouchbasePosixException(999992, "new posix lite exception");

            var networkException =
                CouchbaseException.Create(new C4Error(C4NetworkErrorCode.InvalidURL)) as CouchbaseNetworkException;

            networkException.Error.Should().Be(CouchbaseLiteError.InvalidUrl);
            networkException.Domain.Should().Be(CouchbaseLiteErrorType.CouchbaseLite);
            networkException = new CouchbaseNetworkException(HttpStatusCode.BadRequest);
            networkException = new CouchbaseNetworkException(C4NetworkErrorCode.InvalidURL);
            networkException = new CouchbaseNetworkException(C4NetworkErrorCode.InvalidURL, "You are trying to connect to an invalid url");

            var runtimeException = new RuntimeException("runtime exception");
        }
Ejemplo n.º 10
0
        public IImmutableSet <string> GetPendingDocumentIDs()
        {
            var result = new HashSet <string>();

            if (!IsPushing())
            {
                CBDebug.LogAndThrow(WriteLog.To.Sync,
                                    new CouchbaseLiteException(C4ErrorCode.Unsupported, CouchbaseLiteErrorMessage.PullOnlyPendingDocIDs),
                                    Tag, CouchbaseLiteErrorMessage.PullOnlyPendingDocIDs, true);
            }

            DispatchQueue.DispatchSync(() => {
                var errSetupRepl = SetupC4Replicator();
                if (errSetupRepl.code > 0)
                {
                    CBDebug.LogAndThrow(WriteLog.To.Sync, CouchbaseException.Create(errSetupRepl), Tag, errSetupRepl.ToString(), true);
                }
            });

            byte[] pendingDocIds = LiteCoreBridge.Check(err =>
            {
                return(Native.c4repl_getPendingDocIDs(_repl, err));
            });

            if (pendingDocIds != null)
            {
                _databaseThreadSafety.DoLocked(() => {
                    var flval = Native.FLValue_FromData(pendingDocIds, FLTrust.Trusted);
                    var flarr = Native.FLValue_AsArray(flval);
                    var cnt   = (int)Native.FLArray_Count(flarr);
                    for (int i = 0; i < cnt; i++)
                    {
                        var flv = Native.FLArray_Get(flarr, (uint)i);
                        result.Add(Native.FLValue_AsString(flv));
                    }

                    Array.Clear(pendingDocIds, 0, pendingDocIds.Length);
                    pendingDocIds = null;
                });
            }

            _pendingDocIds = result.ToImmutableHashSet <string>();
            return(_pendingDocIds);
        }
        public void ToString_WithErrorContext_Success(Type contextType)
        {
            // Arrange

            var context = (IErrorContext)Activator.CreateInstance(contextType) !;

            var ex = new CouchbaseException(context);

            // Act

            var result = ex.ToString();

            _testOutputHelper.WriteLine(result);

            // Assert

            Assert.NotNull(result);
            Assert.Contains("-Context Info-", result);
        }