Exemplo n.º 1
0
        public void WriteObject <T>(
            IRestResponse <T> response,
            DevOpsModelTarget onErrorTarget,
            ErrorCategory onErrorCategory,
            object onErrorTargetObject,
            string onErrorReason          = null,
            bool useExceptionTypeChecking = true)
        {
            if (this.IsDebug)
            {
                // ReSharper disable once ExceptionNotDocumented
                this.SetPsVariable("ResponseBody", response);
            }

            if (response.IsSuccessful)
            {
                var responseType = response.Data.GetType();
                if (responseType.IsGenericType && responseType.GetGenericTypeDefinition() == typeof(ResponseModel <>))
                {
                    // ReSharper disable once ExceptionNotDocumented
                    var responseObject = responseType.GetProperty("Value")?.GetValue(response.Data);

                    // ReSharper disable once ExceptionNotDocumented
                    this.WriteObject(responseObject, true);
                }
                else
                {
                    // ReSharper disable once ExceptionNotDocumented
                    this.WriteObject(response.Data);
                }
            }
            else
            {
                if (useExceptionTypeChecking)
                {
                    this.ProcessErrorResponse(response, onErrorTarget, onErrorCategory, onErrorTargetObject, onErrorReason);
                }
                else
                {
                    this.WriteErrorInternal(
                        response,
                        onErrorTarget,
                        onErrorCategory,
                        onErrorTargetObject,
                        onErrorReason);
                }
            }
        }
Exemplo n.º 2
0
        protected void ProcessErrorResponse(
            IRestResponse response,
            DevOpsModelTarget onErrorTarget,
            ErrorCategory onErrorCategory,
            object onErrorTargetObject,
            string onErrorReason = null)
        {
            switch (response.ErrorException)
            {
            case JsonSerializationException jse:
            {
                if (jse.Message.StartsWith("Cannot deserialize the current JSON object"))
                {
                    this.WriteError(
                        response.ErrorException,
                        this.BuildStandardErrorId(onErrorTarget, "ObjectDeserializationFailed"),
                        ErrorCategory.ReadError,
                        onErrorTargetObject);
                    break;
                }

                goto default;
            }

            case JsonReaderException jre:
            {
                if (jre.Message.StartsWith("Unexpected character encountered while parsing value:") &&
                    response.Content.Contains("Access Denied: The Personal Access Token used has expired."))
                {
                    this.WriteError(
                        response.ErrorException,
                        this.BuildStandardErrorId(onErrorTarget, "Your PAT Token has expired!!"),
                        ErrorCategory.AuthenticationError,
                        onErrorTargetObject);
                    break;
                }

                goto default;
            }

            default:
            {
                this.WriteErrorInternal(response, onErrorTarget, onErrorCategory, onErrorTargetObject, onErrorReason);

                break;
            }
            }
        }
Exemplo n.º 3
0
        /// <summary>
        /// Writes the error internal.
        /// </summary>
        /// <param name="response">The response.</param>
        /// <param name="onErrorTarget">The on error target.</param>
        /// <param name="onErrorCategory">The on error category.</param>
        /// <param name="onErrorTargetObject">The on error target object.</param>
        /// <param name="onErrorReason">The on error reason.</param>
        private void WriteErrorInternal(
            IRestResponse response,
            DevOpsModelTarget onErrorTarget,
            ErrorCategory onErrorCategory,
            object onErrorTargetObject,
            string onErrorReason)
        {
            string errorId;

            if (string.IsNullOrWhiteSpace(onErrorReason))
            {
                errorId = this.BuildStandardErrorId(onErrorTarget);
            }
            else
            {
                errorId = this.BuildStandardErrorId(onErrorTarget, onErrorReason);
            }

            this.WriteError(response.ErrorException ?? new Exception("Unknown Error"), errorId, onErrorCategory, onErrorTargetObject);
        }
 /// <summary>
 /// Use this method to build a standardized ID for error records returned to the user.
 /// </summary>
 /// <param name="cmdlet">The cmdlet executing at the time the error was encountered.</param>
 /// <param name="resourceTarget">The type of resources being operated on at the time the error occured</param>
 /// <param name="errorReason">The Reason the error occured.  If no reason is provided then "UnknownError" will be used
 /// instead.</param>
 /// <returns>The standard error ID</returns>
 /// <remarks>The standard ID will be formatted as such: "AzureDevOpsMgmt.Cmdlet.{Resource Target Type}.{Cmdlet Class
 /// Name}.{Failure Reason}</remarks>
 public static string BuildStandardErrorId(
     this PSCmdlet cmdlet,
     DevOpsModelTarget resourceTarget,
     string errorReason = "UnknownError") =>
 CmdletHelpers.BuildStandardErrorIdInternal(
     $"{resourceTarget.ToString()}.{cmdlet.GetType().Name}.{errorReason}");