|
| 1 | +// |
| 2 | +// Copyright (c) Microsoft. All rights reserved. |
| 3 | +// Licensed under the MIT license. See LICENSE file in the project root for full license information. |
| 4 | +// |
| 5 | + |
| 6 | +using System.Management.Automation; |
| 7 | +using Microsoft.Azure.Functions.PowerShellWorker.Utility; |
| 8 | +using LogLevel = Microsoft.Azure.WebJobs.Script.Grpc.Messages.RpcLog.Types.Level; |
| 9 | + |
| 10 | +namespace Microsoft.Azure.Functions.PowerShellWorker.PowerShell |
| 11 | +{ |
| 12 | + internal class ErrorAnalysisLogger |
| 13 | + { |
| 14 | + public static void Log(ILogger logger, ErrorRecord errorRecord, bool isException) |
| 15 | + { |
| 16 | + switch (errorRecord.FullyQualifiedErrorId) |
| 17 | + { |
| 18 | + case KnownErrorId.CommandNotFound: |
| 19 | + LogCommandNotFoundWarning(logger, errorRecord, isException); |
| 20 | + break; |
| 21 | + |
| 22 | + case KnownErrorId.ModuleNotFound: |
| 23 | + LogModuleNotFoundWarning(logger, errorRecord, isException); |
| 24 | + break; |
| 25 | + } |
| 26 | + } |
| 27 | + |
| 28 | + private static void LogCommandNotFoundWarning(ILogger logger, ErrorRecord errorRecord, bool isException) |
| 29 | + { |
| 30 | + var publicMessage = isException |
| 31 | + ? PowerShellWorkerStrings.CommandNotFoundException_Exception |
| 32 | + : PowerShellWorkerStrings.CommandNotFoundException_Error; |
| 33 | + |
| 34 | + var userMessage = string.Format( |
| 35 | + PowerShellWorkerStrings.CommandNotFoundUserWarning, |
| 36 | + errorRecord.CategoryInfo.TargetName); |
| 37 | + |
| 38 | + LogWarning(logger, publicMessage, userMessage); |
| 39 | + } |
| 40 | + |
| 41 | + private static void LogModuleNotFoundWarning(ILogger logger, ErrorRecord errorRecord, bool isException) |
| 42 | + { |
| 43 | + var publicMessage = isException |
| 44 | + ? PowerShellWorkerStrings.ModuleNotFound_Exception |
| 45 | + : PowerShellWorkerStrings.ModuleNotFound_Error; |
| 46 | + |
| 47 | + var userMessage = string.Format( |
| 48 | + PowerShellWorkerStrings.ModuleNotFoundUserWarning, |
| 49 | + errorRecord.CategoryInfo.TargetName); |
| 50 | + |
| 51 | + LogWarning(logger, publicMessage, userMessage); |
| 52 | + } |
| 53 | + |
| 54 | + private static void LogWarning(ILogger logger, string publicMessage, string userMessage) |
| 55 | + { |
| 56 | + logger.Log(isUserOnlyLog: false, LogLevel.Warning, publicMessage); |
| 57 | + logger.Log(isUserOnlyLog: true, LogLevel.Warning, userMessage); |
| 58 | + } |
| 59 | + |
| 60 | + // These error IDs is what PowerShell currently uses, even though this is not documented nor promised. |
| 61 | + // If this ever changes in future, the ErrorAnalysisLogger tests are supposed to catch that, |
| 62 | + // and these IDs or the detection logic will have to be updated. |
| 63 | + private static class KnownErrorId |
| 64 | + { |
| 65 | + public const string CommandNotFound = "CommandNotFoundException"; |
| 66 | + public const string ModuleNotFound = "Modules_ModuleNotFound,Microsoft.PowerShell.Commands.ImportModuleCommand"; |
| 67 | + } |
| 68 | + } |
| 69 | +} |
0 commit comments