Skip to content

Commit 698576f

Browse files
committed
Replaced error writing to new logging framework
1 parent 193a5aa commit 698576f

Some content is hidden

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

42 files changed

+66
-71
lines changed

src/Commands/Admin/AddTenantTheme.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ protected override void ExecuteCmdlet()
4141
}
4242
else
4343
{
44-
WriteError(new ErrorRecord(new Exception($"Theme exists"), "THEMEEXISTS", ErrorCategory.ResourceExists, Identity.Name));
44+
LogError("Theme exists");
4545
}
4646
}
4747
else

src/Commands/Admin/NewSite.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -190,12 +190,12 @@ protected override void ExecuteCmdlet()
190190
}
191191
catch (Exception ex)
192192
{
193-
WriteError(ex);
193+
LogError(ex);
194194
}
195195
}
196196
else
197197
{
198-
WriteError(new PSInvalidOperationException("Creating a new teamsite requires an underlying Microsoft 365 group. In order to create this we need to acquire an access token for the Microsoft Graph. This is not possible using ACS App Only connections."));
198+
LogError(new PSInvalidOperationException("Creating a new teamsite requires an underlying Microsoft 365 group. In order to create this we need to acquire an access token for the Microsoft Graph. This is not possible using ACS App Only connections."));
199199
}
200200
}
201201
else

src/Commands/AzureAD/GetAzureADAppPermission.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ protected override void ExecuteCmdlet()
2323
var app = Identity.GetApp(GraphRequestHelper);
2424
if (app == null)
2525
{
26-
WriteError(new PSArgumentException("Azure AD App not found"));
26+
LogError(new PSArgumentException("Azure AD App not found"));
2727
}
2828
WriteObject(ConvertToPSObject(app));
2929
}

src/Commands/AzureAD/SetAzureADGroup.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -98,12 +98,12 @@ protected override void ExecuteCmdlet()
9898
catch (Exception e)
9999
{
100100
while (e.InnerException != null) e = e.InnerException;
101-
WriteError(new ErrorRecord(e, "GROUPUPDATEFAILED", ErrorCategory.InvalidOperation, this));
101+
LogError(e);
102102
}
103103
}
104104
else
105105
{
106-
WriteError(new ErrorRecord(new Exception("Group not found"), "GROUPNOTFOUND", ErrorCategory.ObjectNotFound, this));
106+
LogError("Group not found");
107107
}
108108
}
109109
}

src/Commands/Base/BasePSCmdlet.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -130,16 +130,16 @@ public bool ParameterSpecified(string parameterName)
130130
/// Allows logging an error
131131
/// </summary>
132132
/// <param name="exception">The exception to log as an error</param>
133-
internal void WriteError(Exception exception)
133+
internal void LogError(Exception exception)
134134
{
135-
WriteError(exception.Message);
135+
LogError(exception.Message);
136136
}
137137

138138
/// <summary>
139139
/// Allows logging an error
140140
/// </summary>
141141
/// <param name="message">The message to log</param>
142-
internal void WriteError(string message)
142+
internal void LogError(string message)
143143
{
144144
Utilities.Logging.LoggingUtility.Error(this, message, correlationId: CorrelationId);
145145
}

src/Commands/Base/GetAccessToken.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ protected override void ExecuteCmdlet()
8484

8585
if (accessTokenValue == null)
8686
{
87-
WriteError(new PSArgumentException("Unable to retrieve access token"));
87+
LogError(new PSArgumentException("Unable to retrieve access token"));
8888
}
8989
if (ListPermissionScopes.IsPresent)
9090
{

src/Commands/Base/GetManagedAppId.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ protected override void ProcessRecord()
2020
}
2121
else
2222
{
23-
WriteError(new ErrorRecord(new Exception("AppId not found"), "APPIDNOTFOUND", ErrorCategory.AuthenticationError, this));
23+
LogError("AppId not found");
2424
}
2525
}
2626
}

src/Commands/Base/GetStoredCredential.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ protected override void ProcessRecord()
1818
}
1919
else
2020
{
21-
WriteError(new ErrorRecord(new System.Exception("Credentials not found"), "CREDSNOTFOUND", ErrorCategory.AuthenticationError, this));
21+
LogError(new System.Exception("Credentials not found"));
2222
}
2323
}
2424
}

src/Commands/Base/PipeBinds/ContentTypePipeBind.cs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -240,20 +240,20 @@ internal PnPCore.IContentType GetContentTypeOrThrow(string paramName, PnP.Core.S
240240
=> GetContentType(context, searchInSiteHierarchy)
241241
?? throw new PSArgumentException(NotFoundMessage(searchInSiteHierarchy), paramName);
242242

243-
internal ContentType GetContentTypeOrError(Cmdlet cmdlet, string paramName, Web web, bool searchInSiteHierarchy = true)
243+
internal ContentType GetContentTypeOrError(BasePSCmdlet cmdlet, string paramName, Web web, bool searchInSiteHierarchy = true)
244244
{
245245
var ct = GetContentType(web, searchInSiteHierarchy);
246246
if (ct is null)
247-
cmdlet.WriteError(new ErrorRecord(new PSArgumentException(NotFoundMessage(searchInSiteHierarchy), paramName), "CONTENTTYPEDOESNOTEXIST", ErrorCategory.InvalidArgument, this));
247+
cmdlet.LogError(new PSArgumentException(NotFoundMessage(searchInSiteHierarchy), paramName));
248248
return ct;
249249
}
250250

251-
internal PnPCore.IContentType GetContentTypeOrError(Cmdlet cmdlet, string paramName, PnP.Core.Services.PnPContext context, bool searchInSiteHierarchy = true)
251+
internal PnPCore.IContentType GetContentTypeOrError(BasePSCmdlet cmdlet, string paramName, PnP.Core.Services.PnPContext context, bool searchInSiteHierarchy = true)
252252
{
253253
var ct = GetContentType(context, searchInSiteHierarchy);
254254
if (ct is null)
255255
{
256-
cmdlet.WriteError(new ErrorRecord(new PSArgumentException(NotFoundMessage(searchInSiteHierarchy), paramName), "CONTENTTYPEDOESNOTEXIST", ErrorCategory.InvalidArgument, this));
256+
cmdlet.LogError(new PSArgumentException(NotFoundMessage(searchInSiteHierarchy), paramName));
257257
}
258258
return ct;
259259
}
@@ -262,19 +262,19 @@ internal ContentType GetContentTypeOrThrow(string paramName, List list)
262262
=> GetContentType(list)
263263
?? throw new PSArgumentException(NotFoundMessage(list), paramName);
264264

265-
internal ContentType GetContentTypeOrError(Cmdlet cmdlet, string paramName, List list)
265+
internal ContentType GetContentTypeOrError(BasePSCmdlet cmdlet, string paramName, List list)
266266
{
267267
var ct = GetContentType(list);
268268
if (ct is null)
269-
cmdlet.WriteError(new ErrorRecord(new PSArgumentException(NotFoundMessage(list), paramName), "CONTENTTYPEDOESNOTEXIST", ErrorCategory.InvalidArgument, this));
269+
cmdlet.LogError(new PSArgumentException(NotFoundMessage(list), paramName));
270270
return ct;
271271
}
272272

273-
internal PnPCore.IContentType GetContentTypeOrError(Cmdlet cmdlet, string paramName, PnPCore.IList list)
273+
internal PnPCore.IContentType GetContentTypeOrError(BasePSCmdlet cmdlet, string paramName, PnPCore.IList list)
274274
{
275275
var ct = GetContentType(list);
276276
if (ct is null)
277-
cmdlet.WriteError(new ErrorRecord(new PSArgumentException(NotFoundMessage(list), paramName), "CONTENTTYPEDOESNOTEXIST", ErrorCategory.InvalidArgument, this));
277+
cmdlet.LogError(new PSArgumentException(NotFoundMessage(list), paramName));
278278
return ct;
279279
}
280280

src/Commands/Base/PnPConnectedCmdlet.cs

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -113,13 +113,8 @@ protected override void ProcessRecord()
113113
{
114114
ex.Data["CorrelationId"] = Connection.Context.TraceCorrelationId;
115115
ex.Data["TimeStampUtc"] = DateTime.UtcNow;
116-
var errorDetails = new ErrorDetails(errorMessage);
117116

118-
errorDetails.RecommendedAction = "Use Get-PnPException for more details.";
119-
var errorRecord = new ErrorRecord(ex, "EXCEPTION", ErrorCategory.WriteError, null);
120-
errorRecord.ErrorDetails = errorDetails;
121-
122-
WriteError(errorRecord);
117+
LogError(errorMessage);
123118
}
124119

125120
}

src/Commands/Base/RemoveManagedAppId.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,13 @@ protected override void ProcessRecord()
2323
{
2424
if (!Utilities.CredentialManager.RemoveAppid(uri.ToString()))
2525
{
26-
WriteError(new ErrorRecord(new Exception($"AppId for {Url} not removed"), "APPIDNOTREMOVED", ErrorCategory.WriteError, Url));
26+
LogError($"AppId for {Url} not removed");
2727
}
2828
}
2929
}
3030
else
3131
{
32-
WriteError(new ErrorRecord(new Exception($"AppId not found for {Url}"), "APPIDNOTFOUND", ErrorCategory.ObjectNotFound, Url));
32+
LogError($"AppId not found for {Url}");
3333
}
3434
}
3535
}

src/Commands/Base/RemoveStoredCredential.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,13 @@ protected override void ProcessRecord()
2121
{
2222
if (!Utilities.CredentialManager.RemoveCredential(Name))
2323
{
24-
WriteError(new ErrorRecord(new System.Exception($"Credential {Name} not removed"), "CREDENTIALNOTREMOVED", ErrorCategory.WriteError, Name));
24+
LogError($"Credential {Name} not removed");
2525
}
2626
}
2727
}
2828
else
2929
{
30-
WriteError(new ErrorRecord(new System.Exception($"Credential {Name} not found"), "CREDENTIALNOTFOUND", ErrorCategory.ObjectNotFound, Name));
30+
LogError($"Credential {Name} not found");
3131
}
3232
}
3333
}

src/Commands/ContentTypes/GetContentTypePublishingStatus.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ protected override void ExecuteCmdlet()
2424

2525
if (ct == null)
2626
{
27-
WriteError(new ErrorRecord(new Exception($"Invalid content type id."), "INVALIDCTID", ErrorCategory.InvalidArgument, ContentType));
27+
LogError("Invalid content type id.");
2828
return;
2929
}
3030

src/Commands/ContentTypes/PublishContentType.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ protected override void ExecuteCmdlet()
2424

2525
if (ct == null)
2626
{
27-
WriteError(new ErrorRecord(new Exception($"Invalid content type id."), "INVALIDCTID", ErrorCategory.InvalidArgument, ContentType));
27+
LogError("Invalid content type id.");
2828
return;
2929
}
3030

src/Commands/ContentTypes/UnpublishContentType.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ protected override void ExecuteCmdlet()
2323

2424
if (ct == null)
2525
{
26-
WriteError(new ErrorRecord(new Exception($"Invalid content type id."), "INVALIDCTID", ErrorCategory.InvalidArgument, ContentType));
26+
LogError("Invalid content type id.");
2727
return;
2828
}
2929

src/Commands/Files/SetFolderPermission.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ protected override void ExecuteCmdlet()
5656
// Ensure the folder has been found
5757
if (folder == null)
5858
{
59-
WriteError(new ErrorRecord(new Exception("Folder not found"), "1", ErrorCategory.ObjectNotFound, null));
59+
LogError("Folder not found");
6060
return;
6161
}
6262

@@ -66,7 +66,7 @@ protected override void ExecuteCmdlet()
6666
// Validate that the ListItemAllFields contains the Id which represents the ListItem ID equivallent for this folder
6767
if (folder.ListItemAllFields.ServerObjectIsNull.GetValueOrDefault(true) || folder.ListItemAllFields.Id <= 0)
6868
{
69-
WriteError(new ErrorRecord(new Exception("ListItemId on folder not found"), "1", ErrorCategory.InvalidData, null));
69+
LogError("ListItemId on folder not found");
7070
return;
7171
}
7272

@@ -156,7 +156,7 @@ protected override void ExecuteCmdlet()
156156
}
157157
else
158158
{
159-
WriteError(new ErrorRecord(new Exception("Principal not found"), "1", ErrorCategory.ObjectNotFound, null));
159+
LogError("Principal not found");
160160
}
161161
}
162162
}

src/Commands/Graph/InvokeGraphMethod.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ protected override void ExecuteCmdlet()
109109
}
110110
catch (Exception ex)
111111
{
112-
WriteError(ex);
112+
LogError(ex);
113113
}
114114
}
115115

src/Commands/Graph/RemoveAvailableSiteClassification.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,14 +48,14 @@ protected override void ExecuteCmdlet()
4848
}
4949
else
5050
{
51-
WriteError(new ErrorRecord(new InvalidOperationException("At least one classification is required. If you want to disable classifications, use Disable-PnPSiteClassification."), "SITECLASSIFICATIONS_ARE_REQUIRED", ErrorCategory.InvalidOperation, null));
51+
LogError("At least one classification is required. If you want to disable classifications, use Disable-PnPSiteClassification.");
5252
}
5353
}
5454
catch (ApplicationException ex)
5555
{
5656
if (ex.Message == @"Missing DirectorySettingTemplate for ""Group.Unified""")
5757
{
58-
WriteError(new ErrorRecord(new InvalidOperationException("Site Classification is not enabled for this tenant"), "SITECLASSIFICATION_NOT_ENABLED", ErrorCategory.ResourceUnavailable, null));
58+
LogError("Site Classification is not enabled for this tenant");
5959
}
6060
}
6161
}

src/Commands/Lists/SetListPermission.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ protected override void ExecuteCmdlet()
8787
}
8888
else
8989
{
90-
WriteError(new ErrorRecord(new Exception("Principal not found"), "1", ErrorCategory.ObjectNotFound, null));
90+
LogError(new Exception("Principal not found"));
9191
}
9292
}
9393
}

src/Commands/PowerPlatform/PowerAutomate/GetFlow.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ protected override void ExecuteCmdlet()
7676
}
7777
catch (Exception e)
7878
{
79-
WriteError(new ErrorRecord(new Exception("Make sure you have granted access to Azure AD App to Interact with Power Platform, To help understand the required permissions visit https://pnp.github.io/powershell/articles/determinepermissions.html#help-i-cant-figure-out-which-permissions-i-need"), e.Message, ErrorCategory.AuthenticationError, null));
79+
LogError($"Error: {e.Message}. Make sure you have granted access to your Entra AD App to Interact with Power Platform, To help understand the required permissions visit https://pnp.github.io/powershell/articles/determinepermissions.html#help-i-cant-figure-out-which-permissions-i-need");
8080
}
8181
}
8282
}

src/Commands/Provisioning/Site/AddDataRowsToSiteTemplate.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ protected override void ExecuteCmdlet()
6464

6565
var template = ProvisioningHelper.LoadSiteTemplateFromFile(Path, TemplateProviderExtensions, (e) =>
6666
{
67-
WriteError(new ErrorRecord(e, "TEMPLATENOTVALID", ErrorCategory.SyntaxError, null));
67+
LogError(e);
6868
});
6969

7070
if (template == null)

src/Commands/Provisioning/Site/AddFileToSiteTemplate.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ protected override void ProcessRecord()
5252
// Load the template
5353
var template = ProvisioningHelper.LoadSiteTemplateFromFile(Path, TemplateProviderExtensions, (e) =>
5454
{
55-
WriteError(new ErrorRecord(e, "TEMPLATENOTVALID", ErrorCategory.SyntaxError, null));
55+
LogError(e);
5656
});
5757

5858
if (template == null)

src/Commands/Provisioning/Site/AddListFoldersToSiteTemplate.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ protected override void ExecuteCmdlet()
4242
// Load the template
4343
var template = ProvisioningHelper.LoadSiteTemplateFromFile(Path, TemplateProviderExtensions, (e) =>
4444
{
45-
WriteError(new ErrorRecord(e, "TEMPLATENOTVALID", ErrorCategory.SyntaxError, null));
45+
LogError(e);
4646
});
4747

4848
if (template == null)

src/Commands/Provisioning/Site/InvokeSiteTemplate.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,7 @@ protected override void ExecuteCmdlet()
150150
if (provisioningTemplate == null)
151151
{
152152
// If we don't have the template, raise an error and exit
153-
WriteError(new ErrorRecord(new Exception("The -Path parameter targets an invalid repository or template object."), "WRONG_PATH", ErrorCategory.SyntaxError, null));
153+
LogError("The -Path parameter targets an invalid repository or template object.");
154154
return;
155155
}
156156

src/Commands/Provisioning/Site/ReadSiteTemplate.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,23 +38,23 @@ protected override void ProcessRecord()
3838
}
3939
WriteObject(ProvisioningHelper.LoadSiteTemplateFromFile(Path, TemplateProviderExtensions, (e) =>
4040
{
41-
WriteError(new ErrorRecord(e, "TEMPLATENOTVALID", ErrorCategory.SyntaxError, null));
41+
LogError(e);
4242
}));
4343
break;
4444
}
4545
case ParameterSet_XML:
4646
{
4747
WriteObject(ProvisioningHelper.LoadSiteTemplateFromString(Xml, TemplateProviderExtensions, (e) =>
4848
{
49-
WriteError(new ErrorRecord(e, "TEMPLATENOTVALID", ErrorCategory.SyntaxError, null));
49+
LogError(e);
5050
}));
5151
break;
5252
}
5353
case ParameterSet_STREAM:
5454
{
5555
WriteObject(ProvisioningHelper.LoadSiteTemplatesFromStream(Stream, TemplateProviderExtensions, (e) =>
5656
{
57-
WriteError(new ErrorRecord(e, "TEMPLATENOTVALID", ErrorCategory.SyntaxError, null));
57+
LogError(e);
5858
}), true);
5959
break;
6060
}

src/Commands/Provisioning/Site/RemoveFileFromSiteTemplate.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ protected override void ProcessRecord()
3232
// Load the template
3333
ProvisioningTemplate template = ProvisioningHelper.LoadSiteTemplateFromFile(Path, TemplateProviderExtensions, (e) =>
3434
{
35-
WriteError(new ErrorRecord(e, "TEMPLATENOTVALID", ErrorCategory.SyntaxError, null));
35+
LogError(e);
3636
});
3737

3838
if (template == null)

src/Commands/Provisioning/Site/SaveSiteTemplate.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ protected override void ProcessRecord()
3535
{
3636
var templateObject = Template.GetTemplate(SessionState.Path.CurrentFileSystemLocation.Path, (e) =>
3737
{
38-
WriteError(new ErrorRecord(e, "TEMPLATENOTVALID", ErrorCategory.SyntaxError, null));
38+
LogError(e);
3939
});
4040

4141
// Determine the output file name and path
@@ -101,7 +101,7 @@ private void ProcessFiles(ProvisioningTemplate template, string templateFileName
101101
{
102102
var templateFile = ProvisioningHelper.LoadSiteTemplateFromFile(templateFileName, null, (e) =>
103103
{
104-
WriteError(new ErrorRecord(e, "TEMPLATENOTVALID", ErrorCategory.SyntaxError, null));
104+
LogError(e);
105105
});
106106
if (template.Tenant?.AppCatalog != null)
107107
{

src/Commands/Provisioning/Tenant/AddSiteTemplate.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ protected override void ProcessRecord()
2121
{
2222
TenantTemplate.Templates.Add(SiteTemplate);
2323
} else {
24-
WriteError(new ErrorRecord(new Exception($"Template with ID {SiteTemplate.Id} already exists in template"), "DUPLICATETEMPLATE", ErrorCategory.InvalidData, SiteTemplate));
24+
LogError($"Template with ID {SiteTemplate.Id} already exists in template");
2525
}
2626
}
2727
}

src/Commands/Provisioning/Tenant/AddTenantSequence.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ protected override void ProcessRecord()
2424
}
2525
else
2626
{
27-
WriteError(new ErrorRecord(new Exception($"Sequence with ID {Sequence.ID} already exists in template"), "DUPLICATESEQUENCEID", ErrorCategory.InvalidData, Sequence));
27+
LogError($"Sequence with ID {Sequence.ID} already exists in template");
2828
}
2929
}
3030
}

0 commit comments

Comments
 (0)