-
Notifications
You must be signed in to change notification settings - Fork 4k
Add cmdlet for Resolving Hyak and AutoRest errors #3984
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
8219c7c
Adding in error handling cmdlet
markcowl d0d4933
Update formatting for errors
markcowl 14b36c4
Refining format and adding additonal detail to exceptions
markcowl 7547ddf
add additional test automation
markcowl acc9734
Adding help and additional tests for resolve-error cmdlet [#3648]
markcowl e80bf22
Merge branch 'preview' of github.com:azure/azure-powershell into rese…
markcowl 40ec995
Resolving review comments for adding space, descriptive text, aliases…
markcowl f2fdc9c
Adding verrtical space
markcowl 3e92457
Adding additional examples
markcowl 2a3b85f
Updating cmdlet verbs that require ShouldProcess list
markcowl File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
150 changes: 150 additions & 0 deletions
150
src/ResourceManager/Profile/Commands.Profile.Test/ErrorResolutionTests.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,150 @@ | ||
// ---------------------------------------------------------------------------------- | ||
// | ||
// Copyright Microsoft Corporation | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
// ---------------------------------------------------------------------------------- | ||
|
||
using Hyak.Common; | ||
using Microsoft.Azure.Commands.Profile.Errors; | ||
using Microsoft.Azure.Commands.ScenarioTest; | ||
using Microsoft.WindowsAzure.Commands.Common.Test.Mocks; | ||
using Microsoft.WindowsAzure.Commands.ScenarioTest; | ||
using Microsoft.WindowsAzure.Commands.Utilities.Common; | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Management.Automation; | ||
using System.Net; | ||
using System.Net.Http; | ||
using Xunit; | ||
|
||
namespace Microsoft.Azure.Commands.Profile.Test | ||
{ | ||
public class ErrorResolutionTests | ||
{ | ||
class TestHyakException : CloudException | ||
{ | ||
public TestHyakException(string message, CloudHttpRequestErrorInfo request, CloudHttpResponseErrorInfo response) : base(message) | ||
{ | ||
Request = request; | ||
Response = response; | ||
} | ||
} | ||
|
||
[Fact] | ||
[Trait(Category.AcceptanceType, Category.CheckIn)] | ||
public void DoesNotThrowWithNullError() | ||
{ | ||
TestExecutionHelpers.SetUpSessionAndProfile(); | ||
var cmdlet = new ResolveError(); | ||
var output = cmdlet.ExecuteCmdletInPipeline<AzureErrorRecord>("Resolve-Error"); | ||
Assert.True(output == null || output.Count == 0); | ||
output = cmdlet.ExecuteCmdletInPipeline<AzureErrorRecord>("Resolve-Error", new ErrorRecord[] { null, null }); | ||
Assert.True(output == null || output.Count == 0); | ||
output = cmdlet.ExecuteCmdletInPipeline<AzureErrorRecord>("Resolve-Error", new ErrorRecord[] { null, new ErrorRecord(new Exception(null), null, ErrorCategory.AuthenticationError, null) }); | ||
Assert.NotNull(output); | ||
Assert.Equal(1, output.Count); | ||
var record = output[0] as AzureExceptionRecord; | ||
Assert.NotNull(record); | ||
Assert.Equal(ErrorCategory.AuthenticationError, record.ErrorCategory.Category); | ||
} | ||
|
||
[Fact] | ||
[Trait(Category.AcceptanceType, Category.CheckIn)] | ||
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 Rest.HttpRequestMessageWrapper(request, ""), | ||
Response = new 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 | ||
}; | ||
|
||
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(errorResult.Exception.GetType(), typeof(Exception)); | ||
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(hyakResult.Exception.GetType(), typeof(TestHyakException)); | ||
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(autorestResult.Exception.GetType(), typeof(Microsoft.Rest.Azure.CloudException)); | ||
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); | ||
} | ||
|
||
[Fact] | ||
[Trait(Category.AcceptanceType, Category.CheckIn)] | ||
public void LastParameterFindsLastError() | ||
{ | ||
TestExecutionHelpers.SetUpSessionAndProfile(); | ||
var mock = new MockCommandRuntime(); | ||
var cmdlet = new ResolveError { CommandRuntime = mock }; | ||
var message = "RuntimeErrorMessage"; | ||
var exception = new Exception(message); | ||
cmdlet.ExecuteCmdletWithExceptionInPipeline<AzureErrorRecord>("Resolve-AzureRmError", exception, new KeyValuePair<string, object>("Last", null ) ); | ||
Assert.NotNull(mock.ErrorStream); | ||
Assert.Equal(1, mock.ErrorStream.Count); | ||
Assert.NotNull(mock.OutputPipeline); | ||
Assert.Equal(1, mock.OutputPipeline.Count); | ||
var record = mock.OutputPipeline[0] as AzureExceptionRecord; | ||
Assert.NotNull(record); | ||
Assert.NotNull(record.Exception); | ||
Assert.Equal(typeof(Exception), record.Exception.GetType()); | ||
Assert.Equal(message, record.Message); | ||
|
||
|
||
} | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
43 changes: 43 additions & 0 deletions
43
src/ResourceManager/Profile/Commands.Profile/Errors/AzureErrorRecord.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
// ---------------------------------------------------------------------------------- | ||
// | ||
// Copyright Microsoft Corporation | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
// ---------------------------------------------------------------------------------- | ||
|
||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Management.Automation; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
|
||
namespace Microsoft.Azure.Commands.Profile.Errors | ||
{ | ||
public class AzureErrorRecord | ||
{ | ||
public AzureErrorRecord(ErrorRecord record) | ||
{ | ||
InvocationInfo = record.InvocationInfo; | ||
ScriptStackTrace = record.ScriptStackTrace; | ||
ErrorCategory = record.CategoryInfo; | ||
ErrorDetails = record.ErrorDetails; | ||
} | ||
|
||
public ErrorDetails ErrorDetails { get; set; } | ||
|
||
public ErrorCategoryInfo ErrorCategory { get; set; } | ||
|
||
public InvocationInfo InvocationInfo { get; set; } | ||
|
||
public string ScriptStackTrace { get; set; } | ||
|
||
} | ||
} |
40 changes: 40 additions & 0 deletions
40
src/ResourceManager/Profile/Commands.Profile/Errors/AzureExceptionRecord.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
// ---------------------------------------------------------------------------------- | ||
// | ||
// Copyright Microsoft Corporation | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
// ---------------------------------------------------------------------------------- | ||
|
||
using System; | ||
using System.Management.Automation; | ||
|
||
namespace Microsoft.Azure.Commands.Profile.Errors | ||
{ | ||
public class AzureExceptionRecord : AzureErrorRecord | ||
{ | ||
public AzureExceptionRecord(Exception exception, ErrorRecord record, bool inner = false) : base(record) | ||
{ | ||
Message = exception.Message; | ||
HelpLink = exception.HelpLink; | ||
StackTrace = exception.StackTrace; | ||
Exception = exception; | ||
} | ||
|
||
public bool InnerException { get; set; } | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @markcowl nit: can we put an empty line between |
||
|
||
public Exception Exception { get; } | ||
|
||
public string Message { get; set; } | ||
|
||
public string StackTrace { get; set; } | ||
|
||
public string HelpLink { get; set; } | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@markcowl nit: can we put an empty line between
ErrorCategory
andInvocationInfo
?