Exemple #1
0
        /// <summary>
        /// Checks the <see cref="Response"/> for errors and throws an exception if necessary.
        /// </summary>
        protected void ThrowExceptionIfNecessary(Response response, string requestPath)
        {
            int statusCode = (int)response.StatusCode;

            // status codes in the 100 or 300 range are not expected
            if (statusCode < 200 || statusCode >= 300)
            {
                string body = response.Body;
                if (body != null && !IsJson(response))
                {
                    ResponseException cause = new ResponseException(response);
                    if (statusCode == (int)HttpStatusCode.NotFound)
                    {
                        throw new NotFoundException("The requested resource was not found; invalid path: " + requestPath, cause);
                    }
                    throw new CommunicationException(cause);
                }
                throw new ResponseException(response);
            }
        }
Exemple #2
0
        /// <summary>
        /// Checks the response for errors and throws an exception if necessary.
        /// </summary>
        protected void ThrowExceptionIfNecessary(HttpStatusCode statusCode, Stream stream, IEnumerable <IResponseHeader> headers, string requestPath)
        {
            int intStatusCode = (int)statusCode;

            // status codes in the 100 or 300 range are not expected
            if (intStatusCode < 200 || intStatusCode >= 300)
            {
                var    sr   = new StreamReader(stream);
                string body = sr.ReadToEnd();

                if (body.IsNullOrEmpty() || !IsJson(headers))
                {
                    ResponseException cause = new ResponseException(statusCode, body, headers);
                    if (intStatusCode == (int)HttpStatusCode.NotFound)
                    {
                        throw new NotFoundException("The requested resource was not found; invalid path: " + requestPath, cause);
                    }
                    throw new CommunicationException(cause);
                }
                throw new ResponseException(statusCode, body, headers);
            }
        }