protected override void ProcessRecord()
        {
            try
            {
                Validate.ValidateInternetConnection();
                ExecuteCmdlet();
                OnProcessRecord();
            }
            catch (CloudException ex)
            {
                var updatedEx = ex;

                if (ex.Response != null && ex.Response.Content != null)
                {
                    var message = FindDetailedMessage(ex.Response.Content);

                    if (message != null)
                    {
                        updatedEx = new CloudException(message, ex);
                    }
                }

                WriteExceptionError(updatedEx);
            }
            catch (Exception ex)
            {
                WriteExceptionError(ex);
            }
        }
        /// <summary>
        /// Create a CloudException from a failed response.
        /// </summary>
        /// <param name="request">The HTTP request.</param>
        /// <param name="requestContent">The HTTP request content.</param>
        /// <param name="response">The HTTP response.</param>
        /// <param name="responseContent">The HTTP response content.</param>
        /// <param name="innerException">Optional inner exception.</param>
        /// <param name="parseError">
        /// Function to parse the response content and return the error code
        /// and error message as a Tuple.
        /// </param>
        /// <returns>A CloudException representing the failure.</returns>
        private static CloudException Create(
            HttpRequestMessage request,
            string requestContent,
            HttpResponseMessage response,
            string responseContent,
            Exception innerException,
            Func<string, Tuple<string, string>> parseError)
        {
            // Get the error code and message
            Tuple<string, string> tuple = parseError(responseContent);
            string code = tuple.Item1;
            string message = tuple.Item2;
            
            // Get the most descriptive message that we can
            string exceptionMessage =
                (code != null && message != null) ? code + ": " + message :
                (message != null) ? message :
                (code != null) ? code :
                (responseContent != null) ? responseContent :
                (response != null && response.ReasonPhrase != null) ? response.ReasonPhrase :
                (response != null) ? response.StatusCode.ToString() :
                new InvalidOperationException().Message;

            // Create the exception
            CloudException exception = new CloudException(exceptionMessage, innerException);
            exception.ErrorCode = code;
            exception.ErrorMessage = message;
            exception.Request = CloudHttpRequestErrorInfo.Create(request, requestContent);
            exception.Response = CloudHttpResponseErrorInfo.Create(response, responseContent);

            return exception;
        }
        internal static CloudRecordState CreateErrorStateFromCloudException(CloudException e, string errorId, object targetObject)
        {
            ErrorRecordState state = CreateErrorStateFromHttpStatusCode(e.Response.StatusCode);
            ErrorRecord er = RemoteAppCollectionErrorState.CreateErrorRecordFromException(e, errorId, targetObject, state.Category);
            CloudRecordState cloudRecord = new CloudRecordState()
            {
                state = state,
                er = er
            };

            return cloudRecord;
        }
        /// <summary>
        /// Create a CloudException from a failed response.
        /// </summary>
        /// <param name="request">The HTTP request.</param>
        /// <param name="requestContent">The HTTP request content.</param>
        /// <param name="response">The HTTP response.</param>
        /// <param name="responseContent">The HTTP response content.</param>
        /// <param name="innerException">Optional inner exception.</param>
        /// <returns>A CloudException representing the failure.</returns>
        public static CloudException Create(
            HttpRequestMessage request,
            string requestContent,
            HttpResponseMessage response,
            string responseContent,
            Exception innerException = null)
        {
            // Get the error code and message
            CloudError cloudError = ParseXmlOrJsonError(responseContent);
            string code = cloudError.Code;
            string message = cloudError.Message;

            // Get the most descriptive message that we can
            string exceptionMessage =
                (code != null && message != null) ? code + ": " + message :
                (message != null) ? message :
                (code != null) ? code :
                !string.IsNullOrEmpty(responseContent) ? responseContent :
                (response != null && response.ReasonPhrase != null) ? response.ReasonPhrase :
                (response != null) ? response.StatusCode.ToString() :
                new InvalidOperationException().Message;

            // Create the exception
            CloudException exception = new CloudException(exceptionMessage, innerException);
            exception.ErrorCode = code;
            exception.ErrorMessage = message;
            exception.Request = CloudHttpRequestErrorInfo.Create(request, requestContent);
            exception.Response = CloudHttpResponseErrorInfo.Create(response, responseContent);

            return exception;
        }
 private void WriteOrThrowAadExceptionMessage(CloudException aadEx)
 {
     WriteDebugMessage(aadEx.Message);
 }
        protected override void ProcessRecord()
        {
            try
            {
                Validate.ValidateInternetConnection();
                ProcessRecord();
                OnProcessRecord();
            }
            catch (AggregateException ex)
            {
                // When the OM encounters an error, it'll throw an AggregateException with a nested BatchException.
                // BatchExceptions have special handling to extract detailed failure information.  When an AggregateException
                // is encountered, loop through the inner exceptions.  If there's a nested BatchException, perform the 
                // special handling.  Otherwise, just write out the error.
                AggregateException flattened = ex.Flatten();
                foreach (Exception inner in flattened.InnerExceptions)
                {
                    BatchException asBatch = inner as BatchException;
                    if (asBatch != null)
                    {
                        HandleBatchException(asBatch);
                    }
                    else
                    {
                        WriteExceptionError(inner);
                    }
                }
            }
            catch (BatchException ex)
            {
                HandleBatchException(ex);
            }
            catch (CloudException ex)
            {
                var updatedEx = ex;

                if (ex.Response != null && ex.Response.Content != null)
                {
                    var message = FindDetailedMessage(ex.Response.Content);

                    if (message != null)
                    {
                        updatedEx = new CloudException(message, ex);
                    }
                }

                WriteExceptionError(updatedEx);
            }
            catch (Exception ex)
            {
                WriteExceptionError(ex);
            }
        }