Exemplo n.º 1
0
        public static StorageException FromResponseMessage(HttpResponseMessage response)
        {
            var sb = new StringBuilder("Status code: ").Append(response.StatusCode).AppendLine();

            string responseString = null;

            if (response.Content != null)
            {
                var readAsStringAsync = RequestExecutor.ReadAsStreamUncompressedAsync(response);
                if (readAsStringAsync.IsCompleted)
                {
                    using (var stream = readAsStringAsync.Result)
                        using (var streamReader = new StreamReader(stream))
                        {
                            responseString = streamReader.ReadToEnd();
                            sb.AppendLine(responseString);
                        }
                }
            }

            return(new StorageException(response, sb.ToString())
            {
                ResponseString = responseString
            });
        }
Exemplo n.º 2
0
        public static async Task Throw(JsonOperationContext context, HttpResponseMessage response, Action <StringBuilder> additionalErrorInfo = null)
        {
            if (response == null)
            {
                throw new ArgumentNullException(nameof(response));
            }

            using (var stream = await RequestExecutor.ReadAsStreamUncompressedAsync(response).ConfigureAwait(false))
                using (var json = await GetJson(context, response, stream).ConfigureAwait(false))
                {
                    var schema = GetExceptionSchema(response, json);

                    if (response.StatusCode == HttpStatusCode.Conflict)
                    {
                        ThrowConflict(schema, json);
                        return;
                    }

                    var type = GetType(schema.Type);
                    if (type == null)
                    {
                        throw RavenException.Generic(schema.Error, json);
                    }

                    Exception exception;
                    try
                    {
                        string message;
                        if (additionalErrorInfo != null)
                        {
                            var sb = new StringBuilder(schema.Error);
                            additionalErrorInfo(sb);
                            message = sb.ToString();
                        }
                        else
                        {
                            message = schema.Error;
                        }
                        exception = (Exception)Activator.CreateInstance(type, BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public, null, new[] { message }, null, null);
                    }
                    catch (Exception)
                    {
                        throw RavenException.Generic(schema.Error, json);
                    }

                    if (typeof(RavenException).IsAssignableFrom(type) == false)
                    {
                        throw new RavenException(schema.Error, exception);
                    }

                    if (type == typeof(IndexCompilationException))
                    {
                        var indexCompilationException = (IndexCompilationException)exception;
                        json.TryGet(nameof(IndexCompilationException.IndexDefinitionProperty), out indexCompilationException.IndexDefinitionProperty);
                        json.TryGet(nameof(IndexCompilationException.ProblematicText), out indexCompilationException.ProblematicText);

                        throw indexCompilationException;
                    }

                    throw exception;
                }
        }