public AzureRestExceptionRecord(Microsoft.Rest.Azure.CloudException exception, ErrorRecord record, bool inner = false) : base(exception, record, inner)
 {
     ServerMessage  = string.Format($"{exception.Body.Code}: {exception.Body.Message} ({exception.Body.Details})");
     ServerResponse = new HttpResponseInfo(exception.Response);
     RequestMessage = new HttpRequestInfo(exception.Request);
     RequestId      = exception.RequestId;
 }
Пример #2
0
        public void HandlesNullValuesInArmExceptions()
        {
            var runtime       = new MockCommandRuntime();
            var hyakException = new TestHyakException(null, null, null);

            var autorestException = new Microsoft.Rest.Azure.CloudException();

            var cmdlet = new ResolveError
            {
                Error = new[]
                {
                    new ErrorRecord(new Exception(), null, ErrorCategory.AuthenticationError, null),
                    new ErrorRecord(hyakException, null, ErrorCategory.ConnectionError, null),
                    new ErrorRecord(autorestException, null, ErrorCategory.InvalidOperation, null),
                },
                CommandRuntime = runtime
            };

            Assert.Throws <NotImplementedException>(() => cmdlet.ExecuteCmdlet());
            Assert.NotNull(runtime.OutputPipeline);
            Assert.Equal(3, runtime.OutputPipeline.Count);
            var errorResult = runtime.OutputPipeline[0] as AzureExceptionRecord;

            Assert.NotNull(errorResult);
            Assert.Equal(ErrorCategory.AuthenticationError, errorResult.ErrorCategory.Category);
            Assert.NotNull(errorResult.Exception);
            Assert.Equal(typeof(Exception), errorResult.Exception.GetType());
            var hyakResult = runtime.OutputPipeline[1] as AzureRestExceptionRecord;

            Assert.NotNull(hyakResult);
            Assert.Equal(ErrorCategory.ConnectionError, hyakResult.ErrorCategory.Category);
            Assert.NotNull(errorResult.Exception);
            Assert.Equal(typeof(TestHyakException), hyakResult.Exception.GetType());
            var autorestResult = runtime.OutputPipeline[2] as AzureRestExceptionRecord;

            Assert.NotNull(autorestResult);
            Assert.Equal(ErrorCategory.InvalidOperation, autorestResult.ErrorCategory.Category);
            Assert.NotNull(autorestResult.Exception);
            Assert.Equal(typeof(Microsoft.Rest.Azure.CloudException), autorestResult.Exception.GetType());
        }
Пример #3
0
        public void HandlesExceptionError()
        {
            var runtime = new MockCommandRuntime();
            var request = new HttpRequestMessage(HttpMethod.Get, new Uri("https://www.contoso.com/resource?api-version-1.0"));

            request.Headers.Add("x-ms-request-id", "HyakRequestId");
            var response      = new HttpResponseMessage(HttpStatusCode.BadRequest);
            var hyakException = new TestHyakException("exception message", CloudHttpRequestErrorInfo.Create(request), CloudHttpResponseErrorInfo.Create(response))
            {
                Error = new Hyak.Common.CloudError {
                    Code = "HyakCode", Message = "HyakError"
                }
            };

            var autorestException = new Microsoft.Rest.Azure.CloudException("exception message")
            {
                Body = new Microsoft.Rest.Azure.CloudError {
                    Code = "AutorestCode", Message = "Autorest message"
                },
                Request   = new Microsoft.Rest.HttpRequestMessageWrapper(request, ""),
                Response  = new Microsoft.Rest.HttpResponseMessageWrapper(response, ""),
                RequestId = "AutoRestRequestId"
            };

            var cmdlet = new ResolveError
            {
                Error = new[]
                {
                    new ErrorRecord(new Exception("exception message"), "errorCode", ErrorCategory.AuthenticationError, this),
                    new ErrorRecord(hyakException, "errorCode", ErrorCategory.ConnectionError, this),
                    new ErrorRecord(autorestException, "errorCode", ErrorCategory.InvalidOperation, this),
                },
                CommandRuntime = runtime
            };

            Assert.Throws <NotImplementedException>(() => cmdlet.ExecuteCmdlet());
            Assert.NotNull(runtime.OutputPipeline);
            Assert.Equal(3, runtime.OutputPipeline.Count);
            var errorResult = runtime.OutputPipeline[0] as AzureExceptionRecord;

            Assert.NotNull(errorResult);
            Assert.Equal(ErrorCategory.AuthenticationError, errorResult.ErrorCategory.Category);
            Assert.NotNull(errorResult.Exception);
            Assert.Equal(typeof(Exception), errorResult.Exception.GetType());
            Assert.Equal("exception message", errorResult.Exception.Message);
            var hyakResult = runtime.OutputPipeline[1] as AzureRestExceptionRecord;

            Assert.NotNull(hyakResult);
            Assert.Equal(ErrorCategory.ConnectionError, hyakResult.ErrorCategory.Category);
            Assert.NotNull(errorResult.Exception);
            Assert.Equal(typeof(TestHyakException), hyakResult.Exception.GetType());
            Assert.Equal("exception message", hyakResult.Exception.Message);
            Assert.NotNull(hyakResult.RequestMessage);
            Assert.Equal(HttpMethod.Get.ToString(), hyakResult.RequestMessage.Verb);
            Assert.Equal(new Uri("https://www.contoso.com/resource?api-version-1.0"), hyakResult.RequestMessage.Uri);
            Assert.NotNull(hyakResult.ServerResponse);
            Assert.Equal(HttpStatusCode.BadRequest.ToString(), hyakResult.ServerResponse.ResponseStatusCode);
            var autorestResult = runtime.OutputPipeline[2] as AzureRestExceptionRecord;

            Assert.NotNull(autorestResult);
            Assert.Equal(ErrorCategory.InvalidOperation, autorestResult.ErrorCategory.Category);
            Assert.NotNull(autorestResult.Exception);
            Assert.Equal(typeof(Microsoft.Rest.Azure.CloudException), autorestResult.Exception.GetType());
            Assert.Equal("exception message", autorestResult.Exception.Message);
            Assert.NotNull(autorestResult.RequestMessage);
            Assert.Equal(HttpMethod.Get.ToString(), autorestResult.RequestMessage.Verb);
            Assert.Equal(new Uri("https://www.contoso.com/resource?api-version-1.0"), autorestResult.RequestMessage.Uri);
            Assert.NotNull(autorestResult.ServerResponse);
            Assert.Equal(HttpStatusCode.BadRequest.ToString(), autorestResult.ServerResponse.ResponseStatusCode);
            Assert.Equal("AutoRestRequestId", autorestResult.RequestId);
            Assert.Contains("AutorestCode", autorestResult.ServerMessage);
            Assert.Contains("Autorest message", autorestResult.ServerMessage);
        }