From 9f50b9d5f3aa574eff8c680cb15fcd2012f3a9dd Mon Sep 17 00:00:00 2001 From: "Erich(Renyong) Wang" Date: Tue, 12 Jan 2021 10:30:42 +0800 Subject: [PATCH 1/2] refine exception definitions --- .../Exceptions/AzPSArgumentException.cs | 3 ++- .../Exceptions/AzPSArgumentNullException.cs | 3 ++- .../AzPSArgumentOutOfRangeException.cs | 3 ++- src/Common/Exceptions/AzPSCloudException.cs | 25 ++++--------------- .../AzPSInvalidOperationException.cs | 3 ++- 5 files changed, 13 insertions(+), 24 deletions(-) diff --git a/src/Common/Exceptions/AzPSArgumentException.cs b/src/Common/Exceptions/AzPSArgumentException.cs index f81bdf0e1f..c5ee400ac6 100644 --- a/src/Common/Exceptions/AzPSArgumentException.cs +++ b/src/Common/Exceptions/AzPSArgumentException.cs @@ -14,11 +14,12 @@ using System; using System.IO; +using System.Management.Automation; using System.Runtime.CompilerServices; namespace Microsoft.Azure.Commands.Common.Exceptions { - public class AzPSArgumentException : ArgumentException, IContainsAzPSErrorData + public class AzPSArgumentException : PSArgumentException, IContainsAzPSErrorData { private string ErrorParamName { diff --git a/src/Common/Exceptions/AzPSArgumentNullException.cs b/src/Common/Exceptions/AzPSArgumentNullException.cs index e6585dbb58..a2ea50e4f6 100644 --- a/src/Common/Exceptions/AzPSArgumentNullException.cs +++ b/src/Common/Exceptions/AzPSArgumentNullException.cs @@ -14,11 +14,12 @@ using System; using System.IO; +using System.Management.Automation; using System.Runtime.CompilerServices; namespace Microsoft.Azure.Commands.Common.Exceptions { - public class AzPSArgumentNullException : ArgumentNullException, IContainsAzPSErrorData + public class AzPSArgumentNullException : PSArgumentNullException, IContainsAzPSErrorData { private string ErrorParamName { diff --git a/src/Common/Exceptions/AzPSArgumentOutOfRangeException.cs b/src/Common/Exceptions/AzPSArgumentOutOfRangeException.cs index 90bd339bc0..489ca72630 100644 --- a/src/Common/Exceptions/AzPSArgumentOutOfRangeException.cs +++ b/src/Common/Exceptions/AzPSArgumentOutOfRangeException.cs @@ -14,11 +14,12 @@ using System; using System.IO; +using System.Management.Automation; using System.Runtime.CompilerServices; namespace Microsoft.Azure.Commands.Common.Exceptions { - public class AzPSArgumentOutOfRangeException : ArgumentOutOfRangeException, IContainsAzPSErrorData + public class AzPSArgumentOutOfRangeException : PSArgumentOutOfRangeException, IContainsAzPSErrorData { private string ErrorParamName { diff --git a/src/Common/Exceptions/AzPSCloudException.cs b/src/Common/Exceptions/AzPSCloudException.cs index ecd8cc9e95..16120478f6 100644 --- a/src/Common/Exceptions/AzPSCloudException.cs +++ b/src/Common/Exceptions/AzPSCloudException.cs @@ -21,36 +21,21 @@ namespace Microsoft.Azure.Commands.Common.Exceptions { - public class AzPSCloudException : Exception, IContainsAzPSErrorData + public class AzPSCloudException : CloudException, IContainsAzPSErrorData { - private HttpResponseMessageWrapper httpResponseMessageWrapper; - - public HttpRequestMessageWrapper Request { get; set; } - /// /// Gets information about the associated HTTP response. /// - public HttpResponseMessageWrapper Response + public new HttpResponseMessageWrapper Response { - get { return httpResponseMessageWrapper; } + get { return base.Response; } set { - httpResponseMessageWrapper = value; - HttpStatusCode = (int?)httpResponseMessageWrapper?.StatusCode; + base.Response = value; + HttpStatusCode = (int?)value?.StatusCode; } } - /// - /// Gets or sets the response object. - /// - public CloudError Body { get; set; } - - /// - /// Gets or sets the value that uniquely identifies a request - /// made against the service. - /// - public string RequestId { get; set; } - public ErrorKind ErrorKind { get => Data.GetValue(AzurePSErrorDataKeys.ErrorKindKey); diff --git a/src/Common/Exceptions/AzPSInvalidOperationException.cs b/src/Common/Exceptions/AzPSInvalidOperationException.cs index f85235846e..bdefd0dfd8 100644 --- a/src/Common/Exceptions/AzPSInvalidOperationException.cs +++ b/src/Common/Exceptions/AzPSInvalidOperationException.cs @@ -14,11 +14,12 @@ using System; using System.IO; +using System.Management.Automation; using System.Runtime.CompilerServices; namespace Microsoft.Azure.Commands.Common.Exceptions { - public class AzPSInvalidOperationException : InvalidOperationException, IContainsAzPSErrorData + public class AzPSInvalidOperationException : PSInvalidOperationException, IContainsAzPSErrorData { public ErrorKind ErrorKind { From 6b6a8f513edfbedf3656a96e4e4575f4e34de191 Mon Sep 17 00:00:00 2001 From: "Erich(Renyong) Wang" Date: Tue, 12 Jan 2021 11:25:38 +0800 Subject: [PATCH 2/2] update --- .../Exceptions/AzPSArgumentException.cs | 35 ++++++++++++++++--- 1 file changed, 31 insertions(+), 4 deletions(-) diff --git a/src/Common/Exceptions/AzPSArgumentException.cs b/src/Common/Exceptions/AzPSArgumentException.cs index c5ee400ac6..c85a9636cf 100644 --- a/src/Common/Exceptions/AzPSArgumentException.cs +++ b/src/Common/Exceptions/AzPSArgumentException.cs @@ -54,11 +54,10 @@ public string ErrorFileName public AzPSArgumentException( string message, string paramName, - Exception innerException = null, string desensitizedMessage = null, [CallerLineNumber] int lineNumber = 0, [CallerFilePath] string filePath = null) - : this(message, paramName, ErrorKind.UserError, innerException, desensitizedMessage, lineNumber, filePath) + : this(message, paramName, ErrorKind.UserError, desensitizedMessage, lineNumber, filePath) { } @@ -66,11 +65,10 @@ public AzPSArgumentException( string message, string paramName, ErrorKind errorKind, - Exception innerException = null, string desensitizedMessage = null, [CallerLineNumber] int lineNumber = 0, [CallerFilePath] string filePath = null) - :base(message, paramName, innerException) + :base(message, paramName) { ErrorParamName = paramName; ErrorKind = errorKind; @@ -82,5 +80,34 @@ public AzPSArgumentException( ErrorFileName = Path.GetFileNameWithoutExtension(filePath); } } + + public AzPSArgumentException( + string message, + Exception innerException, + string desensitizedMessage = null, + [CallerLineNumber] int lineNumber = 0, + [CallerFilePath] string filePath = null) + : this(message, innerException, ErrorKind.UserError, desensitizedMessage, lineNumber, filePath) + { + } + + public AzPSArgumentException( + string message, + Exception innerException, + ErrorKind errorKind, + string desensitizedMessage = null, + [CallerLineNumber] int lineNumber = 0, + [CallerFilePath] string filePath = null) + : base(message, innerException) + { + ErrorKind = errorKind; + DesensitizedErrorMessage = desensitizedMessage; + ErrorLineNumber = lineNumber; + + if (!string.IsNullOrEmpty(filePath)) + { + ErrorFileName = Path.GetFileNameWithoutExtension(filePath); + } + } } }