Skip to content

Commit 4f48c19

Browse files
authored
Log CommandNotFoundException raised by function code (#355)
Trace "Command not found" events
1 parent 0a945ad commit 4f48c19

File tree

3 files changed

+16
-0
lines changed

3 files changed

+16
-0
lines changed

src/PowerShell/PowerShellManager.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -253,6 +253,11 @@ internal Hashtable InvokeFunction(
253253
}
254254
catch (RuntimeException e)
255255
{
256+
if (e.ErrorRecord.FullyQualifiedErrorId == "CommandNotFoundException")
257+
{
258+
_logger.Log(isUserOnlyLog: false, LogLevel.Warning, PowerShellWorkerStrings.CommandNotFoundException_Exception);
259+
}
260+
256261
Logger.Log(isUserOnlyLog: true, LogLevel.Error, GetFunctionExceptionMessage(e));
257262
throw;
258263
}

src/PowerShell/StreamHandler.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,11 @@ public void ErrorDataAdding(object sender, DataAddingEventArgs e)
3333
{
3434
if(e.ItemAdded is ErrorRecord record)
3535
{
36+
if (record.FullyQualifiedErrorId == "CommandNotFoundException")
37+
{
38+
_logger.Log(isUserOnlyLog: false, LogLevel.Warning, PowerShellWorkerStrings.CommandNotFoundException_Error);
39+
}
40+
3641
_logger.Log(isUserOnlyLog: true, LogLevel.Error, $"ERROR: {_errorRecordFormatter.Format(record)}", record.Exception);
3742
}
3843
}

src/resources/PowerShellWorkerStrings.resx

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -298,4 +298,10 @@
298298
<data name="FailedToEnumerateDependencySnapshotContent" xml:space="preserve">
299299
<value>Failed to enumerate dependency snapshot '{0}' content: {1}</value>
300300
</data>
301+
<data name="CommandNotFoundException_Error" xml:space="preserve">
302+
<value>CommandNotFoundException detected (error).</value>
303+
</data>
304+
<data name="CommandNotFoundException_Exception" xml:space="preserve">
305+
<value>CommandNotFoundException detected (exception).</value>
306+
</data>
301307
</root>

0 commit comments

Comments
 (0)