Example #1
0
        private bool ResponseIsErrorResponse(HttpResponseMessage responseMessage, Stream responseStream, PVOutputBaseResponse result)
        {
            PVOutputApiError apiError = ProcessHttpErrorResults(responseMessage, responseStream);

            if (apiError != null)
            {
                result.IsSuccess = false;
                result.Error     = apiError;
                return(true);
            }
            return(false);
        }
        public void PVOutputApiError_IsEquivalentTo_ReturnsEquivelanceNotEquals()
        {
            var error1 = new PVOutputApiError()
            {
                StatusCode = HttpStatusCode.Unauthorized, Message = "Donation mode required."
            };
            var error2 = new PVOutputApiError()
            {
                StatusCode = HttpStatusCode.Unauthorized, Message = "Donation mode required."
            };
            var error3 = new PVOutputApiError()
            {
                StatusCode = HttpStatusCode.BadRequest, Message = "Unknown."
            };

            Assert.Multiple(() =>
            {
                Assert.That(error1.IsEquivalentTo(error2), Is.True);
                Assert.That(error1.IsEquivalentTo(error3), Is.False);
            });
        }
Example #3
0
        private PVOutputApiError ProcessHttpErrorResults(HttpResponseMessage response, Stream responseStream)
        {
            if (response.IsSuccessStatusCode)
            {
                Logger.LogInformation(LoggingEvents.Handler_RequestStatusSuccesful, "[RequestSuccessful] Status: {StatusCode}", response.StatusCode);
                return(null);
            }

            var error = new PVOutputApiError();

            error.StatusCode = response.StatusCode;
            using (TextReader textReader = new StreamReader(responseStream))
            {
                var fullContent = textReader.ReadToEnd();

                if (!string.IsNullOrEmpty(fullContent))
                {
                    var splitterIndex = fullContent.IndexOf(':');

                    if (splitterIndex > -1)
                    {
                        error.Message = fullContent.Substring(splitterIndex + 1).Trim();
                    }
                    else
                    {
                        error.Message = fullContent;
                    }
                }
            }

            Logger.LogError(LoggingEvents.Handler_RequestStatusFailed, "[RequestFailed] Status: {StatusCode} Content: {Message} ", error.StatusCode, error.Message);

            if (Client.ThrowResponseExceptions)
            {
                throw new PVOutputException(error.StatusCode, error.Message);
            }

            return(error);
        }