Skip to content

Commit 4ba5225

Browse files
committed
Making function level initialization error handling more robust
1 parent c93dac2 commit 4ba5225

File tree

18 files changed

+287
-143
lines changed

18 files changed

+287
-143
lines changed

src/WebJobs.Script.WebHost/Controllers/AdminController.cs

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
// Licensed under the MIT License. See License.txt in the project root for license information.
33

44
using System.Collections.Generic;
5+
using System.Collections.ObjectModel;
56
using System.Linq;
67
using System.Net;
78
using System.Net.Http;
@@ -52,5 +53,31 @@ public HttpResponseMessage Invoke(string name, [FromBody] FunctionInvocation inv
5253

5354
return new HttpResponseMessage(HttpStatusCode.Accepted);
5455
}
56+
57+
[HttpGet]
58+
[Route("admin/functions/{name}")]
59+
public FunctionStatus Get(string name)
60+
{
61+
FunctionStatus status = new FunctionStatus();
62+
Collection<string> functionErrors = null;
63+
64+
FunctionDescriptor function = _scriptHostManager.Instance.Functions.FirstOrDefault(p => p.Name.ToLowerInvariant() == name.ToLowerInvariant());
65+
if (function == null)
66+
{
67+
// if the function doesn't exist in the host, see if it the function
68+
// has errors
69+
if (_scriptHostManager.Instance.FunctionErrors.TryGetValue(name, out functionErrors))
70+
{
71+
status.Errors = functionErrors;
72+
}
73+
else
74+
{
75+
// we don't know anything about this function
76+
throw new HttpResponseException(HttpStatusCode.NotFound);
77+
}
78+
}
79+
80+
return status;
81+
}
5582
}
5683
}
420 Bytes
Binary file not shown.
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// Copyright (c) .NET Foundation. All rights reserved.
2+
// Licensed under the MIT License. See License.txt in the project root for license information.
3+
4+
using System.Collections.ObjectModel;
5+
using Newtonsoft.Json;
6+
7+
namespace WebJobs.Script.WebHost.Models
8+
{
9+
public class FunctionStatus
10+
{
11+
/// <summary>
12+
/// Gets or sets the collection of initialization errors for the function.
13+
/// </summary>
14+
[JsonProperty(PropertyName = "errors", DefaultValueHandling = DefaultValueHandling.Ignore)]
15+
public Collection<string> Errors { get; set; }
16+
}
17+
}

src/WebJobs.Script.WebHost/WebJobs.Script.WebHost.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -245,6 +245,7 @@
245245
<Compile Include="Handlers\EnsureHostRunningHandler.cs" />
246246
<Compile Include="HostSecrets.cs" />
247247
<Compile Include="Models\FunctionInvocation.cs" />
248+
<Compile Include="Models\FunctionStatus.cs" />
248249
<Compile Include="Properties\AssemblyInfo.cs" />
249250
<Compile Include="SecretManager.cs" />
250251
<Compile Include="WebHooks\DynamicWebHookReceiverConfig.cs" />

src/WebJobs.Script/Binding/BlobBinding.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,11 @@ public class BlobBinding : FunctionBinding
1616

1717
public BlobBinding(ScriptHostConfiguration config, string name, string path, FileAccess access, bool isTrigger) : base(config, name, "blob", access, isTrigger)
1818
{
19+
if (string.IsNullOrEmpty(path))
20+
{
21+
throw new ArgumentException("The blob path cannot be null or empty.");
22+
}
23+
1924
Path = path;
2025
_pathBindingTemplate = BindingTemplate.FromString(Path);
2126
}

src/WebJobs.Script/Binding/EventHubBinding.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,11 @@ public class EventHubBinding : FunctionBinding
1717
public EventHubBinding(ScriptHostConfiguration config, string name, string eventHubName, FileAccess access, bool isTrigger) :
1818
base(config, name, "eventhub", access, isTrigger)
1919
{
20+
if (string.IsNullOrEmpty(eventHubName))
21+
{
22+
throw new ArgumentException("The event hub path cannot be null or empty.");
23+
}
24+
2025
EventHubName = eventHubName;
2126
_eventHubNameBindingTemplate = BindingTemplate.FromString(EventHubName);
2227
}

src/WebJobs.Script/Binding/QueueBinding.cs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,12 @@ public class QueueBinding : FunctionBinding
1515
private readonly BindingTemplate _queueNameBindingTemplate;
1616

1717
public QueueBinding(ScriptHostConfiguration config, string name, string queueName, FileAccess access, bool isTrigger) : base(config, name, "queue", access, isTrigger)
18-
{
18+
{
19+
if (string.IsNullOrEmpty(queueName))
20+
{
21+
throw new ArgumentException("The queue name cannot be null or empty.");
22+
}
23+
1924
QueueName = queueName;
2025
_queueNameBindingTemplate = BindingTemplate.FromString(QueueName);
2126
}

src/WebJobs.Script/Binding/ServiceBusBinding.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,11 @@ public class ServiceBusBinding : FunctionBinding
1616

1717
public ServiceBusBinding(ScriptHostConfiguration config, string name, string queueOrTopicName, FileAccess access, bool isTrigger) : base(config, name, "serviceBus", access, isTrigger)
1818
{
19+
if (string.IsNullOrEmpty(queueOrTopicName))
20+
{
21+
throw new ArgumentException("A valid queue or topic name must be specified.");
22+
}
23+
1924
QueueOrTopicName = queueOrTopicName;
2025
_queueOrTopicNameBindingTemplate = BindingTemplate.FromString(QueueOrTopicName);
2126
}

src/WebJobs.Script/Binding/TableBinding.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,11 @@ public class TableBinding : FunctionBinding
2323

2424
public TableBinding(ScriptHostConfiguration config, string name, string tableName, string partitionKey, string rowKey, FileAccess access, TableQuery tableQuery = null) : base(config, name, "queue", access, false)
2525
{
26+
if (string.IsNullOrEmpty(tableName))
27+
{
28+
throw new ArgumentException("The table name cannot be null or empty.");
29+
}
30+
2631
TableName = tableName;
2732
PartitionKey = partitionKey;
2833
RowKey = rowKey;

src/WebJobs.Script/Description/ScriptFunctionInvokerBase.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,11 @@ internal ScriptFunctionInvokerBase(ScriptHost host, FunctionMetadata functionMet
3030

3131
public TraceWriter TraceWriter { get; private set; }
3232

33-
private static TraceWriter CreateTraceWriter(ScriptHostConfiguration scriptContig, string functionName)
33+
private static TraceWriter CreateTraceWriter(ScriptHostConfiguration scriptConfig, string functionName)
3434
{
35-
if (scriptContig.FileLoggingEnabled)
35+
if (scriptConfig.FileLoggingEnabled)
3636
{
37-
string logFilePath = Path.Combine(scriptContig.RootLogPath, "Function", functionName);
37+
string logFilePath = Path.Combine(scriptConfig.RootLogPath, "Function", functionName);
3838
return new FileTraceWriter(logFilePath, TraceLevel.Verbose);
3939
}
4040

0 commit comments

Comments
 (0)