Skip to content

Commit 89b1465

Browse files
committed
Error handling improvements
1 parent 42e5029 commit 89b1465

File tree

6 files changed

+98
-6
lines changed

6 files changed

+98
-6
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ public HostStatus GetHostStatus()
9090
if (lastError != null)
9191
{
9292
status.Errors = new Collection<string>();
93-
status.Errors.Add(lastError.Message);
93+
status.Errors.Add(Utility.FlattenException(lastError));
9494
}
9595

9696
return status;

src/WebJobs.Script/Host/ScriptHost.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -465,6 +465,7 @@ private static ScriptType ParseScriptType(string scriptFilePath)
465465
switch (extension)
466466
{
467467
case "csx":
468+
case "cs":
468469
return ScriptType.CSharp;
469470
case "js":
470471
return ScriptType.Javascript;

src/WebJobs.Script/Utility.cs

Lines changed: 39 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,11 @@
33

44
using System;
55
using System.Configuration;
6+
using System.Text;
67

78
namespace Microsoft.Azure.WebJobs.Script
89
{
9-
internal static class Utility
10+
public static class Utility
1011
{
1112
public static string GetFunctionShortName(string functionName)
1213
{
@@ -21,14 +22,47 @@ public static string GetFunctionShortName(string functionName)
2122

2223
public static string FlattenException(Exception ex)
2324
{
24-
string formattedError = ex.Message;
25+
StringBuilder flattenedErrorsBuilder = new StringBuilder();
26+
string lastError = null;
2527

26-
while ((ex = ex.InnerException) != null)
28+
if (ex is AggregateException)
2729
{
28-
formattedError += ". " + ex.Message;
30+
ex = ex.InnerException;
2931
}
3032

31-
return formattedError;
33+
do
34+
{
35+
StringBuilder currentErrorBuilder = new StringBuilder();
36+
if (!string.IsNullOrEmpty(ex.Source))
37+
{
38+
currentErrorBuilder.AppendFormat("{0}: ", ex.Source);
39+
}
40+
41+
currentErrorBuilder.Append(ex.Message);
42+
43+
if (!ex.Message.EndsWith("."))
44+
{
45+
currentErrorBuilder.Append(".");
46+
}
47+
48+
// sometimes inner exceptions are exactly the same
49+
// so first check before duplicating
50+
string currentError = currentErrorBuilder.ToString();
51+
if (lastError == null ||
52+
string.Compare(lastError.Trim(), currentError.Trim()) != 0)
53+
{
54+
if (flattenedErrorsBuilder.Length > 0)
55+
{
56+
flattenedErrorsBuilder.Append(" ");
57+
}
58+
flattenedErrorsBuilder.Append(currentError);
59+
}
60+
61+
lastError = currentError;
62+
}
63+
while ((ex = ex.InnerException) != null);
64+
65+
return flattenedErrorsBuilder.ToString();
3266
}
3367

3468
public static string GetAppSettingOrEnvironmentValue(string name)

src/src.ruleset

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,5 +19,7 @@
1919
<Rule Id="CA1062" Action="None" />
2020
<Rule Id="CA1303" Action="None" /> <!-- Enable when we move literals to resx -->
2121
<Rule Id="CA1800" Action="None" />
22+
<Rule Id="CA1307" Action="None" />
23+
<Rule Id="CA1305" Action="None" />
2224
</Rules>
2325
</RuleSet>
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
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;
5+
using Microsoft.Azure.WebJobs.Script;
6+
using Xunit;
7+
8+
namespace WebJobs.Script.Tests
9+
{
10+
public class UtilityTests
11+
{
12+
[Fact]
13+
public void FlattenException_AggregateException_ReturnsExpectedResult()
14+
{
15+
ApplicationException ex1 = new ApplicationException("Incorrectly configured setting 'Foo'");
16+
ex1.Source = "Acme.CloudSystem";
17+
18+
// a dupe of the first
19+
ApplicationException ex2 = new ApplicationException("Incorrectly configured setting 'Foo'");
20+
ex1.Source = "Acme.CloudSystem";
21+
22+
AggregateException aex = new AggregateException("One or more errors occurred.", ex1, ex2);
23+
24+
string formattedResult = Utility.FlattenException(aex);
25+
Assert.Equal("Acme.CloudSystem: Incorrectly configured setting 'Foo'.", formattedResult);
26+
}
27+
28+
[Fact]
29+
public void FlattenException_SingleException_ReturnsExpectedResult()
30+
{
31+
ApplicationException ex = new ApplicationException("Incorrectly configured setting 'Foo'");
32+
ex.Source = "Acme.CloudSystem";
33+
34+
string formattedResult = Utility.FlattenException(ex);
35+
Assert.Equal("Acme.CloudSystem: Incorrectly configured setting 'Foo'.", formattedResult);
36+
}
37+
38+
[Fact]
39+
public void FlattenException_MultipleInnerExceptions_ReturnsExpectedResult()
40+
{
41+
ApplicationException ex1 = new ApplicationException("Exception message 1");
42+
ex1.Source = "Source1";
43+
44+
ApplicationException ex2 = new ApplicationException("Exception message 2.", ex1);
45+
ex2.Source = "Source2";
46+
47+
ApplicationException ex3 = new ApplicationException("Exception message 3", ex2);
48+
49+
string formattedResult = Utility.FlattenException(ex3);
50+
Assert.Equal("Exception message 3. Source2: Exception message 2. Source1: Exception message 1.", formattedResult);
51+
}
52+
}
53+
}

test/WebJobs.Script.Tests/WebJobs.Script.Tests.csproj

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
<AssemblyName>WebJobs.Script.Tests</AssemblyName>
1414
<TargetFrameworkVersion>v4.6</TargetFrameworkVersion>
1515
<FileAlignment>512</FileAlignment>
16+
<StyleCopTreatErrorsAsWarnings>false</StyleCopTreatErrorsAsWarnings>
1617
<NuGetPackageImportStamp>
1718
</NuGetPackageImportStamp>
1819
<TargetFrameworkProfile />
@@ -232,6 +233,7 @@
232233
<Compile Include="TestHelpers.cs" />
233234
<Compile Include="TestInvoker.cs" />
234235
<Compile Include="TestTraceWriter.cs" />
236+
<Compile Include="UtilityTests.cs" />
235237
<Compile Include="WindowsBatchEndToEndTests.cs" />
236238
</ItemGroup>
237239
<ItemGroup>

0 commit comments

Comments
 (0)