Ejemplo n.º 1
0
        public RestException(System.Net.Http.HttpResponseMessage response)
        {
            StatusCode = response.StatusCode;
            //CloneWithContent will not work here since the content is disposed after sendAsync
            //Besides, it seems there is no need for the request content cloned here.
            RequestMessage  = response.RequestMessage.Clone();
            ResponseBody    = response.Content.ReadAsStringAsync().Result;
            ResponseHeaders = response.Headers;

            RequestId       = response.GetFirstHeader("x-ms-request-id");
            ClientRequestId = response.GetFirstHeader("x-ms-client-request-id");

            try
            {
                // try to parse the body as JSON, and see if a code and message are in there.
                var json = Microsoft.Azure.PowerShell.Cmdlets.DataProtection.Runtime.Json.JsonNode.Parse(ResponseBody) as Microsoft.Azure.PowerShell.Cmdlets.DataProtection.Runtime.Json.JsonObject;

                // error message could be in properties.statusMessage
                { message = If(json?.Property("properties"), out var p) &&
                            If(p?.PropertyT <Microsoft.Azure.PowerShell.Cmdlets.DataProtection.Runtime.Json.JsonString>("statusMessage"), out var sm)
                    ? (string)sm : (string)Message; }

                // see if there is an error block in the body
                json = json?.Property("error") ?? json;

                { Code = If(json?.PropertyT <Microsoft.Azure.PowerShell.Cmdlets.DataProtection.Runtime.Json.JsonString>("code"), out var c) ? (string)c : (string)StatusCode.ToString(); }
                { message = If(json?.PropertyT <Microsoft.Azure.PowerShell.Cmdlets.DataProtection.Runtime.Json.JsonString>("message"), out var m) ? (string)m : (string)Message; }
                { Action = If(json?.PropertyT <Microsoft.Azure.PowerShell.Cmdlets.DataProtection.Runtime.Json.JsonString>("action"), out var a) ? (string)a : (string)Action; }
            }
#if DEBUG
            catch (System.Exception E)
            {
                System.Console.Error.WriteLine($"{E.GetType().Name}/{E.Message}/{E.StackTrace}");
            }
#else
            catch
            {
                // couldn't get the code/message from the body response.
                // we'll create one below.
            }
#endif
            if (string.IsNullOrEmpty(message))
            {
                if (StatusCode >= System.Net.HttpStatusCode.BadRequest && StatusCode < System.Net.HttpStatusCode.InternalServerError)
                {
                    message = $"The server responded with a Request Error, Status: {StatusCode}";
                }
                else if (StatusCode >= System.Net.HttpStatusCode.InternalServerError)
                {
                    message = $"The server responded with a Server Error, Status: {StatusCode}";
                }
                else
                {
                    message = $"The server responded with an unrecognized response, Status: {StatusCode}";
                }
            }
        }
        public RestException(System.Net.Http.HttpResponseMessage response)
        {
            StatusCode      = response.StatusCode;
            RequestMessage  = response.RequestMessage.CloneWithContent().Result;
            ResponseBody    = response.Content.ReadAsStringAsync().Result;
            ResponseHeaders = response.Headers;

            RequestId       = response.GetFirstHeader("x-ms-request-id");
            ClientRequestId = response.GetFirstHeader("x-ms-client-request-id");

            try
            {
                // try to parse the body as JSON, and see if a code and message are in there.
                var json = Microsoft.Rest.ClientRuntime.Json.JsonNode.Parse(ResponseBody) as Microsoft.Rest.ClientRuntime.Json.JsonObject;
                { Code = If(json?.PropertyT <Microsoft.Rest.ClientRuntime.Json.JsonString>("code"), out var c) ? (string)c : (string)StatusCode.ToString(); }
                { message = If(json?.PropertyT <Microsoft.Rest.ClientRuntime.Json.JsonString>("message"), out var m) ? (string)m : (string)Message; }
                { Action = If(json?.PropertyT <Microsoft.Rest.ClientRuntime.Json.JsonString>("action"), out var a) ? (string)a : (string)Action; }
            }
#if DEBUG
            catch (System.Exception E)
            {
                System.Console.Error.WriteLine($"{E.GetType().Name}/{E.Message}/{E.StackTrace}");
            }
#else
            catch
            {
                // couldn't get the code/message from the body response.
                // we'll create one below.
            }
#endif
            if (string.IsNullOrEmpty(message))
            {
                if (StatusCode >= System.Net.HttpStatusCode.BadRequest && StatusCode < System.Net.HttpStatusCode.InternalServerError)
                {
                    message = $"The server responded with a Request Error, Status: {StatusCode}";
                }
                else if (StatusCode >= System.Net.HttpStatusCode.InternalServerError)
                {
                    message = $"The server responded with a Server Error, Status: {StatusCode}";
                }
                else
                {
                    message = $"The server responded with an unrecognized response, Status: {StatusCode}";
                }
            }
        }