Ejemplo n.º 1
0
        // Token: 0x060018F4 RID: 6388 RVA: 0x00056B88 File Offset: 0x00054D88
        private GetLinkPreviewResponse CreateErrorResponse(HttpRequestException requestException)
        {
            WebException ex = requestException.InnerException as WebException;

            if (ex != null)
            {
                return(this.CreateErrorResponse(ex));
            }
            string name             = requestException.GetType().Name;
            string exceptionMessage = GetLinkPreview.GetExceptionMessage(requestException);

            return(this.CreateErrorResponse(name, exceptionMessage));
        }
//        private static string GetMessage(HttpException ex)
//        {
//            var errors = new StringBuilder();
//            errors.AppendLine();
//#if DEBUG
//            errors.AppendLine($"[{ex.GetType().Name}]");
//#endif

//            switch (ex.WebEventCode)
//            {
//                // Maximum request length exceeded
//                case 3004:
//                    errors.AppendLine("حجم درخواست از حداکثر طول مجاز تجاوز نموده است.");
//                    break;
//                default:
//                    errors.AppendLine($"{ex.WebEventCode}: {ex.Message}");
//                    break;
//            }

//            return errors.ToString();
//        }

        private static string GetMessage(HttpRequestException ex)
        {
            var errors = new StringBuilder();

            errors.AppendLine();
#if DEBUG
            errors.AppendLine($"[{ex.GetType().Name}]");
#endif

            //switch (ex.) {
            //    // Maximum request length exceeded
            //    case 3004:
            //        errors.AppendLine("حجم درخواست از حداکثر طول مجاز تجاوز نموده است.");
            //        break;
            //    default:
            //        errors.AppendLine($"{ex.WebEventCode}: {ex.Message}");
            //        break;
            //}
            errors.AppendLine("در زمان ارسال درخواست به سرور خطایی رخ داده است");
            return(errors.ToString());
        }
        public async Task PostAsync_ShouldThrowExceptionIfResponseIsNotSuccessful()
        {
            //Arrange
            _httpMessageHandler.Protected().Setup <Task <HttpResponseMessage> >("SendAsync",
                                                                                ItExpr.IsAny <HttpRequestMessage>(),
                                                                                ItExpr.IsAny <CancellationToken>())
            .ReturnsAsync(new HttpResponseMessage()
            {
                StatusCode = HttpStatusCode.InternalServerError
            });

            var httpClient = new HttpClient(_httpMessageHandler.Object)
            {
                BaseAddress = new Uri("http://test.com/"),
            };

            _httpClientFactory.Setup(x => x.CreateClient("Test")).Returns(httpClient);

            var expectedResult = new HttpRequestException("Response status code does not indicate success: 500 (Internal Server Error).");

            //Act
            var result = await Assert.ThrowsExceptionAsync <HttpRequestException>(async() => await _httpClientAdapter.PostAsync("Test", "/tests", new { Id = 1, Name = "Name" }));

            //Assert
            _httpMessageHandler.Protected().Verify(
                "SendAsync",
                Times.Once(),
                ItExpr.Is <HttpRequestMessage>(request =>
                                               request.Method == HttpMethod.Post
                                               //&& request.Content == new StringContent("{\"Id\":1,\"Name\":\"Name\"}", Encoding.UTF8, "application/json")
                                               && request.RequestUri == new Uri("http://test.com/tests")
                                               ), ItExpr.IsAny <CancellationToken>());

            result.Should().BeOfType(expectedResult.GetType());
            result.Message.Should().Be(expectedResult.Message);
        }
Ejemplo n.º 4
0
        public async Task <CompilerResponse> GetCompilationResponse(string script, string entityType, IDictionary <string, string> references, string requestId = "")
        {
            DiagnosticsETWProvider.Instance.LogCompilerHostClientMessage(requestId, _eventSource, "Get Compilation : Waiting on semaphore ...");

            await _semaphoreObject.WaitAsync();

            DiagnosticsETWProvider.Instance.LogCompilerHostClientMessage(requestId, _eventSource, "Get Compilation : Entered critical section ...");

            try
            {
                // If for any reason, compiler host is not running due to failures in process monitor, launch it before making a call.
                if (!_isComplierHostRunning)
                {
                    await LaunchCompilerHostProcess(requestId);
                }

                HttpRequestMessage requestMessage = new HttpRequestMessage(HttpMethod.Post, $"{_compilerHostUrl}/api/compilerhost")
                {
                    Content = new StringContent(JsonConvert.SerializeObject(PrepareRequestBody(script, entityType, references)), Encoding.UTF8, "application/json")
                };

                if (!string.IsNullOrWhiteSpace(requestId))
                {
                    requestMessage.Headers.Add(HeaderConstants.RequestIdHeaderName, requestId);
                }

                HttpResponseMessage responseMessage = await _httpClient.SendAsync(requestMessage);

                if (!responseMessage.IsSuccessStatusCode)
                {
                    string errorResponse = await responseMessage.Content.ReadAsStringAsync();

                    HttpRequestException ex = new HttpRequestException($"Status Code : {responseMessage.StatusCode}, Content : {errorResponse}");
                    DiagnosticsETWProvider.Instance.LogCompilerHostClientException(requestId, _eventSource, string.Empty, ex.GetType().ToString(), ex.ToString());
                    throw ex;
                }

                return(await responseMessage.Content.ReadAsAsyncCustom <CompilerResponse>());
            }
            finally
            {
                _semaphoreObject.Release();
                DiagnosticsETWProvider.Instance.LogCompilerHostClientMessage(requestId, _eventSource, "Get Compilation : semaphore released.");
            }
        }
Ejemplo n.º 5
0
        private async Task <bool> CheckForHealthPing(string requestId)
        {
            HttpRequestMessage requestMessage = new HttpRequestMessage(HttpMethod.Get, $"{_compilerHostUrl}/api/compilerhost/healthping");

            if (!string.IsNullOrWhiteSpace(requestId))
            {
                requestMessage.Headers.Add(HeaderConstants.RequestIdHeaderName, requestId);
            }

            HttpResponseMessage responseMessage = await _httpClient.SendAsync(requestMessage);

            if (!responseMessage.IsSuccessStatusCode)
            {
                string errorResponse = await responseMessage.Content.ReadAsStringAsync();

                HttpRequestException ex = new HttpRequestException($"Status Code : {responseMessage.StatusCode}, Content : {errorResponse}");
                DiagnosticsETWProvider.Instance.LogCompilerHostClientWarning(requestId, _eventSource, "Compiler host health ping failed", ex.GetType().ToString(), ex.ToString());
                throw ex;
            }

            return(true);
        }