Пример #1
0
        private static void ThrowConflict(ExceptionSchema schema, BlittableJsonReaderObject json)
        {
            if (schema.Type.Contains(nameof(DocumentConflictException))) // temporary!
            {
                throw DocumentConflictException.From(json);
            }

            throw new ConcurrencyException(schema.Message);
        }
Пример #2
0
        public static Exception Get(ExceptionSchema schema, HttpStatusCode code, Exception inner = null)
        {
            var message      = schema.Message;
            var typeAsString = schema.Type;

            if (code == HttpStatusCode.Conflict)
            {
                if (typeAsString.Contains(nameof(DocumentConflictException)))
                {
                    return(DocumentConflictException.From(message));
                }

                return(new ConcurrencyException(message));
            }

            // We throw the same error for different status codes: GatewayTimeout,RequestTimeout,BadGateway,ServiceUnavailable.
            var error = $"{schema.Error}{Environment.NewLine}" +
                        $"The server at {schema.Url} responded with status code: {code}.";

            var type = GetType(typeAsString);

            if (type == null)
            {
                return(new RavenException(error, inner));
            }

            Exception exception;

            try
            {
                exception = (Exception)Activator.CreateInstance(type, BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public, null, new[] { error }, null, null);
            }
            catch (Exception)
            {
                return(new RavenException(error));
            }

            if (typeof(RavenException).IsAssignableFrom(type) == false)
            {
                return(new RavenException(error, exception));
            }

            return(exception);
        }
Пример #3
0
 public static Exception Get(ExceptionSchema schema, HttpStatusCode code) => Get(schema.Message, schema.Error, schema.Type, code);
Пример #4
0
        private static void ThrowConflict(ExceptionSchema schema, BlittableJsonReaderObject json)
        {
            if (schema.Type.Contains(nameof(DocumentConflictException))) // temporary!
            {
                throw DocumentConflictException.From(json);
            }

            string expectedCv, actualCv, docId;

            if (schema.Type.Contains(nameof(ClusterTransactionConcurrencyException)))
            {
                var ctxConcurrencyException = new ClusterTransactionConcurrencyException(schema.Message);

                if (json.TryGet(nameof(ClusterTransactionConcurrencyException.Id), out docId))
                {
                    ctxConcurrencyException.Id = docId;
                }

                if (json.TryGet(nameof(ClusterTransactionConcurrencyException.ExpectedChangeVector), out expectedCv))
                {
                    ctxConcurrencyException.ExpectedChangeVector = expectedCv;
                }

                if (json.TryGet(nameof(ClusterTransactionConcurrencyException.ActualChangeVector), out actualCv))
                {
                    ctxConcurrencyException.ActualChangeVector = actualCv;
                }

                if (json.TryGet(nameof(ClusterTransactionConcurrencyException.ConcurrencyViolations), out BlittableJsonReaderArray violations) == false)
                {
                    throw ctxConcurrencyException;
                }

                ctxConcurrencyException.ConcurrencyViolations = new ClusterTransactionConcurrencyException.ConcurrencyViolation[violations.Length];

                for (var i = 0; i < violations.Length; i++)
                {
                    if (!(violations[i] is BlittableJsonReaderObject violation))
                    {
                        continue;
                    }

                    var current = ctxConcurrencyException.ConcurrencyViolations[i] = new ClusterTransactionConcurrencyException.ConcurrencyViolation();

                    if (violation.TryGet(nameof(ClusterTransactionConcurrencyException.ConcurrencyViolation.Id), out string id))
                    {
                        current.Id = id;
                    }

                    if (violation.TryGet(nameof(ClusterTransactionConcurrencyException.ConcurrencyViolation.Type), out ClusterTransactionConcurrencyException.ViolationOnType type))
                    {
                        current.Type = type;
                    }

                    if (violation.TryGet(nameof(ClusterTransactionConcurrencyException.ConcurrencyViolation.Expected), out long expected))
                    {
                        current.Expected = expected;
                    }

                    if (violation.TryGet(nameof(ClusterTransactionConcurrencyException.ConcurrencyViolation.Actual), out long actual))
                    {
                        current.Actual = actual;
                    }
                }

                throw ctxConcurrencyException;
            }

            var concurrencyException = new ConcurrencyException(schema.Message);

            if (json.TryGet(nameof(ConcurrencyException.Id), out docId))
            {
                concurrencyException.Id = docId;
            }
            if (json.TryGet(nameof(ConcurrencyException.ExpectedChangeVector), out expectedCv))
            {
                concurrencyException.ExpectedChangeVector = expectedCv;
            }
            if (json.TryGet(nameof(ConcurrencyException.ActualChangeVector), out actualCv))
            {
                concurrencyException.ActualChangeVector = actualCv;
            }

            throw concurrencyException;
        }