Exemplo n.º 1
0
        private async Task <RavenJToken> CheckForErrorsAndReturnCachedResultIfAnyAsync(bool readErrorString)
        {
            if (Response.IsSuccessStatusCode)
            {
                return(null);
            }
            if (ResponseStatusCode == HttpStatusCode.Unauthorized ||
                ResponseStatusCode == HttpStatusCode.NotFound ||
                ResponseStatusCode == HttpStatusCode.Conflict)
            {
                if (factory.CanLogRequest)
                {
                    factory.OnLogRequest(owner, new RequestResultArgs
                    {
                        DurationMilliseconds = CalculateDuration(),
                        Method     = Method,
                        HttpResult = (int)ResponseStatusCode,
                        Status     = RequestStatus.ErrorOnServer,
                        Result     = ResponseStatusCode.ToString(),
                        Url        = Url,
                        PostedData = postedData
                    });
                }


                throw ErrorResponseException.FromResponseMessage(Response, readErrorString);
            }

            if (ResponseStatusCode == HttpStatusCode.NotModified &&
                CachedRequestDetails != null)
            {
                factory.UpdateCacheTime(this);
                var result = factory.GetCachedResponse(this, ResponseHeaders);

                // here we explicitly need to get Response.Headers, and NOT ResponseHeaders because we are
                // getting the value _right now_ from the secondary, and don't care about the 304, the force check
                // is still valid
                HandleReplicationStatusChanges(ResponseHeaders, primaryUrl, operationUrl);

                if (factory.CanLogRequest)
                {
                    factory.OnLogRequest(owner, new RequestResultArgs
                    {
                        DurationMilliseconds = CalculateDuration(),
                        Method     = Method,
                        HttpResult = (int)ResponseStatusCode,
                        Status     = RequestStatus.Cached,
                        Result     = result.ToString(),
                        Url        = Url,
                        PostedData = postedData
                    });
                }


                return(result);
            }


            using (var sr = new StreamReader(await Response.GetResponseStreamWithHttpDecompression().ConfigureAwait(false)))
            {
                var readToEnd = sr.ReadToEnd();

                if (factory.CanLogRequest)
                {
                    factory.OnLogRequest(owner, new RequestResultArgs
                    {
                        DurationMilliseconds = CalculateDuration(),
                        Method     = Method,
                        HttpResult = (int)ResponseStatusCode,
                        Status     = RequestStatus.Cached,
                        Result     = readToEnd,
                        Url        = Url,
                        PostedData = postedData
                    });
                }

                if (string.IsNullOrWhiteSpace(readToEnd))
                {
                    throw ErrorResponseException.FromResponseMessage(Response);
                }

                RavenJObject ravenJObject;
                try
                {
                    ravenJObject = RavenJObject.Parse(readToEnd);
                }
                catch (Exception e)
                {
                    throw new ErrorResponseException(Response, readToEnd, e);
                }
                if (ravenJObject.ContainsKey("IndexDefinitionProperty"))
                {
                    throw new IndexCompilationException(ravenJObject.Value <string>("Message"))
                          {
                              IndexDefinitionProperty = ravenJObject.Value <string>("IndexDefinitionProperty"),
                              ProblematicText         = ravenJObject.Value <string>("ProblematicText")
                          };
                }
                //else if (ravenJObject.ContainsKey())
                if (ResponseStatusCode == HttpStatusCode.BadRequest && ravenJObject.ContainsKey("Message"))
                {
                    throw new BadRequestException(ravenJObject.Value <string>("Message"), ErrorResponseException.FromResponseMessage(Response));
                }
                if (ravenJObject.ContainsKey("Error"))
                {
                    var errorValue = ravenJObject.Value <string>("Error");
                    if (errorValue != null && errorValue.StartsWith("System.TimeoutException"))
                    {
                        throw new TimeoutException(errorValue.Substring("System.TimeoutException: ".Length));
                    }

                    var sb = new StringBuilder();
                    foreach (var prop in ravenJObject)
                    {
                        if (prop.Key == "Error")
                        {
                            continue;
                        }

                        sb.Append(prop.Key).Append(": ").AppendLine(prop.Value.ToString(Formatting.Indented));
                    }

                    if (sb.Length > 0)
                    {
                        sb.AppendLine();
                    }
                    sb.Append(errorValue);

                    throw new ErrorResponseException(Response, sb.ToString(), readToEnd);
                }
                throw new ErrorResponseException(Response, readToEnd);
            }
        }
Exemplo n.º 2
0
        public override string ToString()
        {
            string message = $"RequestContent Type - {RequestContentType}; RequestMethod - {RequestMethod}; RequestUrl - {RequestUri};";

            message += $"RequestTimeStamp - {RequestTimestamp?.ToShortDateString()}{RequestTimestamp?.ToShortTimeString()};";
            message += $"Response TimeStamp - {ResponseTimestamp?.ToShortDateString()}{ResponseTimestamp?.ToShortTimeString()};";
            message += $"ResponseContentType -  {ResponseContentType} ReponseStatusCode - {ResponseStatusCode.ToString()}";

            return(message);
        }