Skip to content

Commit 31762e4

Browse files
committed
Fix issue with Batch error formatting
- Also fix New-AzBatchTask parameter requirements.
1 parent ce532d6 commit 31762e4

File tree

73 files changed

+113
-130
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

73 files changed

+113
-130
lines changed

src/Batch/Batch/ApplicationPackages/GetBatchApplicationPackageCommand.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ public class GetBatchApplicationPackageCommand : BatchCmdletBase
3939
[ValidateNotNullOrEmpty]
4040
public string ApplicationVersion { get; set; }
4141

42-
public override void ExecuteCmdlet()
42+
protected override void ExecuteCmdletImpl()
4343
{
4444
if (string.IsNullOrEmpty(this.ApplicationVersion))
4545
{

src/Batch/Batch/ApplicationPackages/NewBatchApplicationPackageCommand.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ public class NewBatchApplicationPackageCommand : BatchCmdletBase
6060
[ValidateNotNullOrEmpty]
6161
public SwitchParameter ActivateOnly { get; set; }
6262

63-
public override void ExecuteCmdlet()
63+
protected override void ExecuteCmdletImpl()
6464
{
6565
PSApplicationPackage response = BatchClient.UploadAndActivateApplicationPackage(
6666
this.ResourceGroupName,

src/Batch/Batch/ApplicationPackages/RemoveBatchApplicationPackageCommand.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ public class RemoveBatchApplicationPackageCommand : BatchCmdletBase
4343
[ValidateNotNullOrEmpty]
4444
public string ApplicationVersion { get; set; }
4545

46-
public override void ExecuteCmdlet()
46+
protected override void ExecuteCmdletImpl()
4747
{
4848
WriteVerboseWithTimestamp(Resources.BeginMAMLCall, mamlCall);
4949
BatchClient.DeleteApplicationPackage(this.ResourceGroupName, this.AccountName, this.ApplicationName, this.ApplicationVersion);

src/Batch/Batch/Applications/GetBatchApplicationCommand.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ public class GetBatchApplicationCommand : BatchCmdletBase
3838
[Alias("ApplicationId")]
3939
public string ApplicationName { get; set; }
4040

41-
public override void ExecuteCmdlet()
41+
protected override void ExecuteCmdletImpl()
4242
{
4343
if (string.IsNullOrEmpty(this.ApplicationName))
4444
{

src/Batch/Batch/Applications/NewBatchApplicationCommand.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ public class NewBatchApplicationCommand : BatchCmdletBase
4949
[ValidateNotNullOrEmpty]
5050
public string DisplayName { get; set; }
5151

52-
public override void ExecuteCmdlet()
52+
protected override void ExecuteCmdletImpl()
5353
{
5454
PSApplication response = BatchClient.AddApplication(this.ResourceGroupName, this.AccountName, this.ApplicationName, this.AllowUpdates, this.DisplayName);
5555
WriteObject(response);

src/Batch/Batch/Applications/RemoveBatchApplicationCommand.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ public class RemoveBatchApplicationCommand : BatchCmdletBase
3838
[Alias("ApplicationId")]
3939
public string ApplicationName { get; set; }
4040

41-
public override void ExecuteCmdlet()
41+
protected override void ExecuteCmdletImpl()
4242
{
4343
WriteVerboseWithTimestamp(Resources.BeginMAMLCall, mamlCall);
4444
BatchClient.DeleteApplication(this.ResourceGroupName, this.AccountName, this.ApplicationName);

src/Batch/Batch/Applications/SetBatchApplicationCommand.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ public class SetBatchApplicationCommand : BatchCmdletBase
5050
[ValidateNotNullOrEmpty]
5151
public bool? AllowUpdates { get; set; }
5252

53-
public override void ExecuteCmdlet()
53+
protected override void ExecuteCmdletImpl()
5454
{
5555
WriteVerboseWithTimestamp(Resources.BeginMAMLCall, mamlCall);
5656
BatchClient.UpdateApplication(this.ResourceGroupName, this.AccountName, this.ApplicationName, this.AllowUpdates, this.DefaultVersion, this.DisplayName);

src/Batch/Batch/BatchAccounts/GetBatchAccountCommand.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ public class GetBatchAccountCommand : BatchCmdletBase
3535
[Parameter(Position = 2, ValueFromPipelineByPropertyName = true)]
3636
public Hashtable Tag { get; set; }
3737

38-
public override void ExecuteCmdlet()
38+
protected override void ExecuteCmdletImpl()
3939
{
4040
if (string.IsNullOrEmpty(this.AccountName))
4141
{

src/Batch/Batch/BatchAccounts/GetBatchAccountKeysCommand.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ public class GetBatchAccountKeysCommand : BatchCmdletBase
3636
/// Get the keys associated with the specified account. If only the account name is passed in, then
3737
/// look up its resource group and construct a new BatchAccountContext to hold everything.
3838
/// </summary>
39-
public override void ExecuteCmdlet()
39+
protected override void ExecuteCmdletImpl()
4040
{
4141
BatchAccountContext context = BatchClient.GetKeys(this.ResourceGroupName, this.AccountName);
4242
WriteObject(context);

src/Batch/Batch/BatchAccounts/GetBatchAccountNodeAgentSkuCommand.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ public int MaxCount
3636
set { this.maxCount = value; }
3737
}
3838

39-
public override void ExecuteCmdlet()
39+
protected override void ExecuteCmdletImpl()
4040
{
4141
foreach (PSNodeAgentSku nodeAgentSku in BatchClient.ListNodeAgentSkus(this.BatchContext, this.Filter, this.MaxCount, this.AdditionalBehaviors))
4242
{

src/Batch/Batch/BatchAccounts/NewBatchAccountCommand.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ public class NewBatchAccountCommand : BatchCmdletBase
5858
[Parameter(ValueFromPipelineByPropertyName = true)]
5959
public Hashtable Tag { get; set; }
6060

61-
public override void ExecuteCmdlet()
61+
protected override void ExecuteCmdletImpl()
6262
{
6363
AccountCreateParameters parameters = new AccountCreateParameters(this.ResourceGroupName, this.AccountName, this.Location)
6464
{

src/Batch/Batch/BatchAccounts/NewBatchAccountKeyCommand.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ public string KeyType
5252
}
5353
}
5454

55-
public override void ExecuteCmdlet()
55+
protected override void ExecuteCmdletImpl()
5656
{
5757
BatchAccountContext context = BatchClient.RegenerateKeys(this.ResourceGroupName, this.AccountName, this.keyType);
5858
WriteObject(context);

src/Batch/Batch/BatchAccounts/RemoveBatchAccountCommand.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ public class RemoveBatchAccountCommand : BatchCmdletBase
3838
[Parameter]
3939
public SwitchParameter Force { get; set; }
4040

41-
public override void ExecuteCmdlet()
41+
protected override void ExecuteCmdletImpl()
4242
{
4343
ConfirmAction(
4444
Force.IsPresent,

src/Batch/Batch/BatchAccounts/SetBatchAccountCommand.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ public class SetBatchAccountCommand : BatchCmdletBase
4141
[ValidateNotNullOrEmpty]
4242
public string AutoStorageAccountId { get; set; }
4343

44-
public override void ExecuteCmdlet()
44+
protected override void ExecuteCmdletImpl()
4545
{
4646
BatchAccountContext context = BatchClient.UpdateAccount(this.ResourceGroupName, this.AccountName, this.Tag, this.AutoStorageAccountId);
4747
WriteObject(context);

src/Batch/Batch/BatchCmdletBase.cs

Lines changed: 37 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,13 @@
1919
using Microsoft.WindowsAzure.Commands.Common;
2020
using Newtonsoft.Json.Linq;
2121
using System;
22+
using System.Linq;
23+
using System.Text;
2224
using BatchClient = Microsoft.Azure.Commands.Batch.Models.BatchClient;
2325

2426
namespace Microsoft.Azure.Commands.Batch
2527
{
26-
public class BatchCmdletBase : AzureRMCmdlet
28+
public abstract class BatchCmdletBase : AzureRMCmdlet
2729
{
2830
private BatchClient batchClient;
2931

@@ -43,62 +45,63 @@ public BatchClient BatchClient
4345
set { batchClient = value; }
4446
}
4547

46-
protected virtual void OnProcessRecord()
47-
{
48-
// Intentionally left blank
49-
}
48+
protected abstract void ExecuteCmdletImpl();
5049

5150
public override void ExecuteCmdlet()
5251
{
5352
try
5453
{
5554
Validate.ValidateInternetConnection();
56-
ProcessRecord();
57-
OnProcessRecord();
55+
ExecuteCmdletImpl();
5856
}
59-
catch (AggregateException ex)
57+
catch (BatchException ex)
6058
{
61-
// When the OM encounters an error, it'll throw a BatchException.
62-
// BatchExceptions have special handling to extract detailed failure information. When an AggregateException
63-
// is encountered, loop through the inner exceptions. If there's a nested BatchException, perform the
64-
// special handling. Otherwise, just write out the error.
65-
AggregateException flattened = ex.Flatten();
66-
foreach (Exception inner in flattened.InnerExceptions)
59+
if (ex?.RequestInformation != null)
6760
{
68-
BatchException asBatch = inner as BatchException;
69-
if (asBatch != null)
70-
{
71-
HandleBatchException(asBatch);
72-
}
73-
else
61+
StringBuilder sb = new StringBuilder();
62+
sb.AppendLine($"HttpStatusCode: " + ex.RequestInformation.HttpStatusCode);
63+
sb.AppendLine($"StatusMessage: " + ex.RequestInformation.HttpStatusMessage);
64+
sb.AppendLine($"ClientRequestId: {ex.RequestInformation.ClientRequestId}");
65+
sb.AppendLine($"RequestId: {ex.RequestInformation.ServiceRequestId}");
66+
67+
if (ex.RequestInformation.BatchError != null)
7468
{
75-
WriteExceptionError(inner);
69+
sb.AppendLine();
70+
sb.AppendLine($"Error code: {ex.RequestInformation.BatchError.Code}");
71+
if (ex.RequestInformation.BatchError.Message != null)
72+
{
73+
sb.AppendLine($"Message: {ex.RequestInformation.BatchError.Message.Value}");
74+
}
75+
76+
if (ex.RequestInformation.BatchError.Values != null && ex.RequestInformation.BatchError.Values.Any())
77+
{
78+
sb.AppendLine("Error details:");
79+
foreach (var item in ex.RequestInformation.BatchError.Values)
80+
{
81+
sb.AppendLine($"{item.Key}: {item.Value}");
82+
}
83+
}
7684
}
85+
86+
throw new BatchException(ex.RequestInformation, sb.ToString(), ex.InnerException);
7787
}
78-
}
79-
catch (BatchException ex)
80-
{
81-
HandleBatchException(ex);
88+
89+
throw;
8290
}
8391
catch (CloudException ex)
8492
{
85-
var updatedEx = ex;
86-
87-
if (ex.Response != null && ex.Response.Content != null)
93+
if (ex.Response?.Content != null)
8894
{
8995
var message = FindDetailedMessage(ex.Response.Content);
9096

9197
if (message != null)
9298
{
93-
updatedEx = new CloudException(message, ex);
99+
var updatedEx = new CloudException(message, ex);
100+
throw updatedEx;
94101
}
95102
}
96103

97-
WriteExceptionError(updatedEx);
98-
}
99-
catch (Exception ex)
100-
{
101-
WriteExceptionError(ex);
104+
throw;
102105
}
103106
}
104107

@@ -110,7 +113,6 @@ public override void ExecuteCmdlet()
110113
/// <returns></returns>
111114
internal static string FindDetailedMessage(string content)
112115
{
113-
// TODO: Revise after Task 2362107 is completed on the server side
114116
string message = null;
115117

116118
if (CloudException.IsJson(content))
@@ -146,24 +148,5 @@ internal static string FindDetailedMessage(string content)
146148

147149
return message;
148150
}
149-
150-
/// <summary>
151-
/// Extracts failure details from the BatchException object to create a more informative error message for the user.
152-
/// </summary>
153-
/// <param name="ex">The BatchException object</param>
154-
private void HandleBatchException(BatchException ex)
155-
{
156-
if (ex != null)
157-
{
158-
if (ex.RequestInformation != null && ex.RequestInformation.BatchError != null)
159-
{
160-
WriteExceptionError(new BatchException(ex.RequestInformation, ex.ToString(), ex.InnerException));
161-
}
162-
else
163-
{
164-
WriteExceptionError(ex);
165-
}
166-
}
167-
}
168151
}
169152
}

src/Batch/Batch/BatchObjectModelCmdletBase.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ namespace Microsoft.Azure.Commands.Batch
2222
/// <summary>
2323
/// Base class for cmdlets that use the Batch OM.
2424
/// </summary>
25-
public class BatchObjectModelCmdletBase : BatchCmdletBase
25+
public abstract class BatchObjectModelCmdletBase : BatchCmdletBase
2626
{
2727
/// <summary>
2828
/// Collection of custom behaviors to perform on service calls

src/Batch/Batch/Certificates/GetBatchCertificateCommand.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ public int MaxCount
4949
[ValidateNotNullOrEmpty]
5050
public string Select { get; set; }
5151

52-
public override void ExecuteCmdlet()
52+
protected override void ExecuteCmdletImpl()
5353
{
5454
ListCertificateOptions options = new ListCertificateOptions(this.BatchContext, this.AdditionalBehaviors)
5555
{

src/Batch/Batch/Certificates/NewBatchCertificateCommand.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ public class NewBatchCertificateCommand : BatchObjectModelCmdletBase
4141
[ValidateNotNullOrEmpty]
4242
public SecureString Password { get; set; }
4343

44-
public override void ExecuteCmdlet()
44+
protected override void ExecuteCmdletImpl()
4545
{
4646
NewCertificateParameters parameters = new NewCertificateParameters(this.BatchContext, this.FilePath, this.RawData,
4747
this.AdditionalBehaviors)

src/Batch/Batch/Certificates/RemoveBatchCertificateCommand.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ public class RemoveBatchCertificateCommand : BatchObjectModelCmdletBase
3333
[ValidateNotNullOrEmpty]
3434
public string Thumbprint { get; set; }
3535

36-
public override void ExecuteCmdlet()
36+
protected override void ExecuteCmdletImpl()
3737
{
3838
CertificateOperationParameters parameters = new CertificateOperationParameters(this.BatchContext,
3939
this.ThumbprintAlgorithm, this.Thumbprint, this.AdditionalBehaviors);

src/Batch/Batch/Certificates/StopBatchCertificateDeletionCommand.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ public class StopBatchCertificateDeletionCommand : BatchObjectModelCmdletBase
3131
[ValidateNotNullOrEmpty]
3232
public string Thumbprint { get; set; }
3333

34-
public override void ExecuteCmdlet()
34+
protected override void ExecuteCmdletImpl()
3535
{
3636
CertificateOperationParameters parameters = new CertificateOperationParameters(this.BatchContext,
3737
this.ThumbprintAlgorithm, this.Thumbprint, this.AdditionalBehaviors);

src/Batch/Batch/ComputeNodeUsers/NewBatchComputeNodeUserCommand.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ public class NewBatchComputeNodeUserCommand : BatchObjectModelCmdletBase
5555
[Parameter]
5656
public SwitchParameter IsAdmin { get; set; }
5757

58-
public override void ExecuteCmdlet()
58+
protected override void ExecuteCmdletImpl()
5959
{
6060
NewComputeNodeUserParameters parameters = new NewComputeNodeUserParameters(this.BatchContext, this.PoolId, this.ComputeNodeId,
6161
this.ComputeNode, this.AdditionalBehaviors)

src/Batch/Batch/ComputeNodeUsers/RemoveBatchComputeNodeUserCommand.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ public class RemoveBatchComputeNodeUserCommand : BatchObjectModelCmdletBase
3939
[ValidateNotNullOrEmpty]
4040
public string Name { get; set; }
4141

42-
public override void ExecuteCmdlet()
42+
protected override void ExecuteCmdletImpl()
4343
{
4444
ComputeNodeUserOperationParameters parameters = new ComputeNodeUserOperationParameters(this.BatchContext, this.PoolId, this.ComputeNodeId,
4545
this.Name, this.AdditionalBehaviors);

src/Batch/Batch/ComputeNodeUsers/SetBatchComputeNodeUserCommand.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ public class SetBatchComputeNodeUserCommand : BatchObjectModelCmdletBase
4747
[ValidateNotNullOrEmpty]
4848
public DateTime ExpiryTime { get; set; }
4949

50-
public override void ExecuteCmdlet()
50+
protected override void ExecuteCmdletImpl()
5151
{
5252
UpdateComputeNodeUserParameters parameters = new UpdateComputeNodeUserParameters(this.BatchContext,
5353
this.PoolId, this.ComputeNodeId, this.Name, this.AdditionalBehaviors)

src/Batch/Batch/ComputeNodes/DisableBatchComputeNodeSchedulingCommand.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ public class DisableBatchComputeNodeSchedulingCommand : BatchObjectModelCmdletBa
4141
[ValidateNotNullOrEmpty]
4242
public DisableComputeNodeSchedulingOption? DisableSchedulingOption { get; set; }
4343

44-
public override void ExecuteCmdlet()
44+
protected override void ExecuteCmdletImpl()
4545
{
4646
DisableComputeNodeSchedulingParameters parameters = new DisableComputeNodeSchedulingParameters(this.BatchContext,
4747
this.PoolId, this.Id, this.ComputeNode, this.AdditionalBehaviors)

src/Batch/Batch/ComputeNodes/EnableBatchComputeNodeSchedulingCommand.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ public class EnableBatchComputeNodeSchedulingCommand : BatchObjectModelCmdletBas
3535
[ValidateNotNullOrEmpty]
3636
public PSComputeNode ComputeNode { get; set; }
3737

38-
public override void ExecuteCmdlet()
38+
protected override void ExecuteCmdletImpl()
3939
{
4040
ComputeNodeOperationParameters parameters = new ComputeNodeOperationParameters(this.BatchContext, this.PoolId,
4141
this.Id, this.ComputeNode, this.AdditionalBehaviors);

src/Batch/Batch/ComputeNodes/GetBatchComputeNodeCommand.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ public int MaxCount
5656
[ValidateNotNullOrEmpty]
5757
public string Select { get; set; }
5858

59-
public override void ExecuteCmdlet()
59+
protected override void ExecuteCmdletImpl()
6060
{
6161
ListComputeNodeOptions options = new ListComputeNodeOptions(this.BatchContext, this.PoolId, this.Pool, this.AdditionalBehaviors)
6262
{

src/Batch/Batch/ComputeNodes/GetBatchComputeNodeRemoteLoginSettingsCommand.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ public class GetBatchComputeNodeRemoteLoginSettingsCommand : BatchObjectModelCmd
3737
[ValidateNotNullOrEmpty]
3838
public PSComputeNode ComputeNode { get; set; }
3939

40-
public override void ExecuteCmdlet()
40+
protected override void ExecuteCmdletImpl()
4141
{
4242
ComputeNodeOperationParameters parameters = new ComputeNodeOperationParameters(this.BatchContext, this.PoolId, this.ComputeNodeId, this.ComputeNode, this.AdditionalBehaviors);
4343

src/Batch/Batch/ComputeNodes/RemoveBatchComputeNodeCommand.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ public class RemoveBatchComputeNodeCommand : BatchObjectModelCmdletBase
4949
[Parameter]
5050
public SwitchParameter Force { get; set; }
5151

52-
public override void ExecuteCmdlet()
52+
protected override void ExecuteCmdletImpl()
5353
{
5454
string computeNodeIds = ComputeNode == null ? string.Join(",", this.Ids) : ComputeNode.Id;
5555
RemoveComputeNodeParameters parameters = new RemoveComputeNodeParameters(this.BatchContext, this.PoolId,

src/Batch/Batch/ComputeNodes/ResetBatchComputeNodeCommand.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ public class ResetBatchComputeNodeCommand : BatchObjectModelCmdletBase
4141
[ValidateNotNullOrEmpty]
4242
public ComputeNodeReimageOption? ReimageOption { get; set; }
4343

44-
public override void ExecuteCmdlet()
44+
protected override void ExecuteCmdletImpl()
4545
{
4646
ReimageComputeNodeParameters parameters = new ReimageComputeNodeParameters(this.BatchContext, this.PoolId,
4747
this.Id, this.ComputeNode, this.AdditionalBehaviors)

0 commit comments

Comments
 (0)