Skip to content

Commit 41d2fcd

Browse files
Refactor error handling and improve exception messages across multiple admin commands (#5098)
* Refactor error handling and improve exception messages across multiple admin commands * Update error message Co-authored-by: Copilot <[email protected]> --------- Co-authored-by: Copilot <[email protected]>
1 parent 7461b0b commit 41d2fcd

17 files changed

+40
-36
lines changed

src/Commands/Admin/AddTenantRestrictedSearchAllowedList.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ protected override void ExecuteCmdlet()
3636
}
3737
else
3838
{
39-
throw new ArgumentException("Parameter set cannot be resolved using the specified named parameters.");
39+
throw new InvalidOperationException("Parameter set cannot be resolved using the specified named parameters.");
4040
}
4141

4242
if (_sitelist == null)

src/Commands/Admin/GetContainerTypeConfiguration.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ protected override void ExecuteCmdlet()
1616
{
1717
if (Identity == Guid.Empty)
1818
{
19-
throw new ArgumentException($"Identity {Identity} value is invalid");
19+
throw new PSArgumentException($"Identity cannot be an empty GUID. Please provide a valid non-empty GUID.");
2020
}
2121
ClientResult<SPContainerTypeConfigurationProperties> sPOContainerTypeConfigurationByContainerTypeId = Tenant.GetSPOContainerTypeConfigurationByContainerTypeId(Identity);
2222
AdminContext.ExecuteQueryRetry();

src/Commands/Admin/GetStorageEntity.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ protected override void ExecuteCmdlet()
9090
{
9191
var storageEntitiesDict = JsonSerializer.Deserialize<Dictionary<string, Dictionary<string, string>>>(storageEntitiesIndex);
9292

93-
var storageEntities = new List<StorageEntity>();
93+
var storageEntities = new List<StorageEntity>(storageEntitiesDict.Count);
9494
foreach (var key in storageEntitiesDict.Keys)
9595
{
9696
var storageEntity = new StorageEntity

src/Commands/Admin/GetTenantDeletedSite.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ protected override void ExecuteCmdlet()
3737
bool flag = Identity != null && !string.IsNullOrEmpty(Identity.Url) && UrlUtilities.IsPersonalSiteUrl(Identity.Url);
3838
if (Identity == null || string.IsNullOrEmpty(Identity.Url) || flag)
3939
{
40-
List<DeletedSiteProperties> list = new List<DeletedSiteProperties>();
40+
List<DeletedSiteProperties> list = [];
4141
uint siteRowLimit = Limit;
4242
bool flag2 = siteRowLimit == 0;
4343
bool flag3 = false;
@@ -95,7 +95,7 @@ private bool RequestDeletedSitePropertiesFromSharePoint(Func<string, SPODeletedS
9595
SPODeletedSitePropertiesEnumerable spoDeletedSitePropertiesEnumerable = getDeletedSitePropertiesFunc(text);
9696
if (spoDeletedSitePropertiesEnumerable == null)
9797
{
98-
throw new ArgumentNullException("Something went wrong fetching deleted sites");
98+
throw new InvalidOperationException("Failed to retrieve deleted sites from SharePoint Online");
9999
}
100100
AdminContext.Load(spoDeletedSitePropertiesEnumerable);
101101
AdminContext.Load(spoDeletedSitePropertiesEnumerable, (SPODeletedSitePropertiesEnumerable sp) => sp.NextStartIndexFromSharePoint);
@@ -146,7 +146,7 @@ private bool RequestDeletedSiteProperties(Func<int, SPODeletedSitePropertiesEnum
146146
AdminContext.ExecuteQueryRetry();
147147
if (spoDeletedSitePropertiesEnumerable == null)
148148
{
149-
throw new ArgumentNullException("Something went wrong fetching deleted sites");
149+
throw new InvalidOperationException("Failed to retrieve deleted sites from SharePoint Online");
150150
}
151151
checked
152152
{

src/Commands/Admin/RemoveTenantSite.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ protected override void ExecuteCmdlet()
3939
dodelete = ShouldContinue("You are trying to delete the root site collection. Be aware that you need to contact Office 365 Support in order to create a new root site collection. Also notice that some CSOM and REST operations require the root site collection to be present. Removing this site can affect all your remote processing code, even when accessing non-root site collections.", Resources.Confirm);
4040
}
4141

42-
if (dodelete && (Force || ShouldContinue(string.Format(Resources.RemoveSiteCollection0, Url), Resources.Confirm)))
42+
if (dodelete && (Force || ShouldContinue($"Remove site collection '{Url}'?", Resources.Confirm)))
4343
{
4444

4545
Func<TenantOperationMessage, bool> timeoutFunction = TimeoutFunction;

src/Commands/Admin/RemoveTenantSyncClientRestriction.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ protected override void ExecuteCmdlet()
1313
{
1414
Tenant.IsUnmanagedSyncClientForTenantRestricted = false;
1515
Tenant.BlockMacSync = false;
16-
Tenant.ExcludedFileExtensionsForSyncClient = new List<string>();
16+
Tenant.ExcludedFileExtensionsForSyncClient = [];
1717
Tenant.OptOutOfGrooveBlock = false;
1818
Tenant.OptOutOfGrooveSoftBlock = false;
1919
Tenant.DisableReportProblemDialog = false;

src/Commands/Admin/SetTenant.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1690,7 +1690,7 @@ protected override void ExecuteCmdlet()
16901690
else
16911691
{
16921692
// Build a GUID array by resolving each provided site URL to its SiteProperties and extracting the Id
1693-
var siteIdList = new List<Guid>();
1693+
var siteIdList = new List<Guid>(KnowledgeAgentSelectedSitesList.Length);
16941694
var tenantForLookup = new Tenant(AdminContext);
16951695

16961696
foreach (var siteUrl in KnowledgeAgentSelectedSitesList)

src/Commands/Admin/SetTenantSyncClientRestriction.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ protected override void ExecuteCmdlet()
6868
break;
6969

7070
default:
71-
throw new PSArgumentException(string.Format(Resources.GrooveBlockOptionNotSupported, nameof(GrooveBlockOption), GrooveBlockOption), nameof(GrooveBlockOption));
71+
throw new PSArgumentException($"Parameter '{nameof(GrooveBlockOption)}' does not support value '{GrooveBlockOption}'. Supported values are: OptOut, HardOptin, SoftOptin", nameof(GrooveBlockOption));
7272
}
7373
}
7474
AdminContext.ExecuteQueryRetry();

src/Commands/Apps/GetApplicationCustomizer.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ public class GetApplicationCustomizer : PnPWebRetrievalsCmdlet<UserCustomAction>
2727

2828
protected override void ExecuteCmdlet()
2929
{
30-
List<UserCustomAction> actions = new List<UserCustomAction>();
30+
List<UserCustomAction> actions = [];
3131

3232
if (Scope == CustomActionScope.All || Scope == CustomActionScope.Web)
3333
{

src/Commands/Apps/GetAzureADAppSitePermission.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ protected override void ExecuteCmdlet()
5454
var permissions = this.GraphRequestHelper.GetResultCollection<AzureADAppPermissionInternal>($"v1.0/sites/{siteId}/permissions?$select=Id");
5555
if (permissions.Any())
5656
{
57-
var results = new List<AzureADAppPermission>();
57+
var results = new List<AzureADAppPermission>(permissions.Count());
5858
foreach (var permission in permissions)
5959
{
6060
// Request the permission individually so it will include the roles

0 commit comments

Comments
 (0)