Skip to content

Commit 334eee8

Browse files
Update logic to show the error messages coming from Get-FunctionsMetadata
1 parent 8656ad2 commit 334eee8

File tree

3 files changed

+65
-12
lines changed

3 files changed

+65
-12
lines changed

src/RequestProcessor.cs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -384,7 +384,11 @@ private StreamingMessage ProcessFunctionMetadataRequest(StreamingMessage request
384384
StreamingMessage.ContentOneofCase.FunctionMetadataResponse,
385385
out StatusResult status);
386386

387-
response.FunctionMetadataResponse.FunctionMetadataResults.AddRange(WorkerIndexingHelper.IndexFunctions(request.FunctionsMetadataRequest.FunctionAppDirectory));
387+
var rpcLogger = new RpcLogger(_msgStream);
388+
rpcLogger.SetContext(request.RequestId, null);
389+
390+
//response.FunctionMetadataResponse.FunctionMetadataResults.AddRange(WorkerIndexingHelper.IndexFunctions(request.FunctionsMetadataRequest.FunctionAppDirectory));
391+
response.FunctionMetadataResponse.FunctionMetadataResults.AddRange(WorkerIndexingHelper.IndexFunctions(request.FunctionsMetadataRequest.FunctionAppDirectory, rpcLogger));
388392

389393
return response;
390394
}

src/WorkerIndexing/WorkerIndexingHelper.cs

Lines changed: 57 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,18 @@
33
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
44
//
55

6+
using Microsoft.Azure.Functions.PowerShellWorker.Utility;
67
using Microsoft.Azure.WebJobs.Script.Grpc.Messages;
78
using Newtonsoft.Json;
89
using System;
910
using System.Collections.Generic;
11+
using System.Collections.ObjectModel;
1012
using System.IO;
1113
using System.Management.Automation;
1214
using System.Management.Automation.Runspaces;
15+
using LogLevel = Microsoft.Azure.WebJobs.Script.Grpc.Messages.RpcLog.Types.Level;
16+
using System.Text;
17+
using Microsoft.Azure.Functions.PowerShellWorker.PowerShell;
1318

1419
namespace Microsoft.Azure.Functions.PowerShellWorker.WorkerIndexing
1520
{
@@ -19,8 +24,10 @@ internal class WorkerIndexingHelper
1924
//const string GetFunctionsMetadataCmdletName = "AzureFunctions.PowerShell.SDK\\Get-FunctionsMetadata";
2025
const string GetFunctionsMetadataCmdletName = "Get-FunctionsMetadata";
2126
const string AzureFunctionsPowerShellSDKModuleName = "AzureFunctions.PowerShell.SDK";
27+
private static readonly ErrorRecordFormatter _errorRecordFormatter = new ErrorRecordFormatter();
2228

23-
internal static IEnumerable<RpcFunctionMetadata> IndexFunctions(string baseDir)
29+
//internal static IEnumerable<RpcFunctionMetadata> IndexFunctions(string baseDir)
30+
internal static IEnumerable<RpcFunctionMetadata> IndexFunctions(string baseDir, ILogger logger)
2431
{
2532
List<RpcFunctionMetadata> indexedFunctions = new List<RpcFunctionMetadata>();
2633

@@ -50,24 +57,63 @@ internal static IEnumerable<RpcFunctionMetadata> IndexFunctions(string baseDir)
5057
System.Management.Automation.PowerShell _powershell = System.Management.Automation.PowerShell.Create();
5158
_powershell.Runspace = runspace;
5259

53-
var modulePath = Path.Join(AppDomain.CurrentDomain.BaseDirectory, "Modules", AzureFunctionsPowerShellSDKModuleName);
54-
// Console.WriteLine("modulePath; {0}", modulePath);
55-
// Console.WriteLine("Importing module...");
56-
_powershell.AddCommand("Import-Module").AddParameter("Name", modulePath)
57-
.AddParameter("Force", true);
58-
59-
// Console.WriteLine("Call Get-FunctionsMetadata");
60-
_powershell.AddCommand(GetFunctionsMetadataCmdletName).AddArgument(baseDir);
6160
string outputString = string.Empty;
62-
foreach (PSObject rawMetadata in _powershell.Invoke())
61+
Exception exception = null;
62+
Collection<PSObject> results = null;
63+
var cmdletExecutionHadErrors = false;
64+
var exceptionThrown = false;
65+
string errorMsg = null;
66+
67+
try
68+
{
69+
_powershell.AddCommand(GetFunctionsMetadataCmdletName).AddArgument(baseDir);
70+
results = _powershell.Invoke();
71+
cmdletExecutionHadErrors = _powershell.HadErrors;
72+
}
73+
catch (Exception ex)
74+
{
75+
exceptionThrown = true;
76+
exception = ex;
77+
throw;
78+
}
79+
finally
80+
{
81+
if (exceptionThrown)
82+
{
83+
errorMsg = string.Format(PowerShellWorkerStrings.ErrorsWhileExecutingGetFunctionsMetadata, exception.ToString());
84+
}
85+
else if (cmdletExecutionHadErrors)
86+
{
87+
var errorCollection = _powershell.Streams.Error;
88+
89+
var stringBuilder = new StringBuilder();
90+
foreach (var errorRecord in errorCollection)
91+
{
92+
var message = _errorRecordFormatter.Format(errorRecord);
93+
stringBuilder.AppendLine(message);
94+
}
95+
96+
errorMsg = string.Format(PowerShellWorkerStrings.ErrorsWhileExecutingGetFunctionsMetadata, stringBuilder.ToString());
97+
}
98+
99+
if (errorMsg != null)
100+
{
101+
logger.Log(isUserOnlyLog: true, LogLevel.Error, errorMsg, exception);
102+
throw new Exception(errorMsg);
103+
}
104+
105+
_powershell.Commands.Clear();
106+
}
107+
108+
// TODO: The GetFunctionsMetadataCmdlet should never return more than one result. Make sure that this is the case and remove this code.
109+
foreach (PSObject rawMetadata in results)
63110
{
64111
if (outputString != string.Empty)
65112
{
66113
throw new Exception(PowerShellWorkerStrings.GetFunctionsMetadataMultipleResultsError);
67114
}
68115
outputString = rawMetadata.ToString();
69116
}
70-
_powershell.Commands.Clear();
71117

72118
List<FunctionInformation> functionInformations = JsonConvert.DeserializeObject<List<FunctionInformation>>(outputString);
73119

src/resources/PowerShellWorkerStrings.resx

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -385,4 +385,7 @@
385385
<data name="InvalidBindingInfoDirection" xml:space="preserve">
386386
<value>Invalid binding direction. Binding name: {0}</value>
387387
</data>
388+
<data name="ErrorsWhileExecutingGetFunctionsMetadata" xml:space="preserve">
389+
<value>Errors reported while executing Get-FunctionsMetadata cmldet. {0}.</value>
390+
</data>
388391
</root>

0 commit comments

Comments
 (0)