Skip to content

Commit 2f8c529

Browse files
committed
aggregate errors
1 parent fa5fe39 commit 2f8c529

File tree

3 files changed

+51
-2
lines changed

3 files changed

+51
-2
lines changed

src/DotNetWorker/Context/Features/DefaultModelBindingFeature.cs

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
1-
// Copyright (c) .NET Foundation. All rights reserved.
1+
g// Copyright (c) .NET Foundation. All rights reserved.
22
// Licensed under the MIT License. See License.txt in the project root for license information.
33

44
using System;
55
using System.Collections.Generic;
6+
using System.Linq;
67
using Microsoft.Azure.Functions.Worker.Converters;
8+
using Microsoft.Azure.Functions.Worker.Definition;
9+
using Microsoft.Azure.Functions.Worker.Diagnostics.Exceptions;
710

811
namespace Microsoft.Azure.Functions.Worker.Context.Features
912
{
@@ -31,6 +34,7 @@ public DefaultModelBindingFeature(IEnumerable<IConverter> converters)
3134
_parameterValues = new object?[context.FunctionDefinition.Parameters.Length];
3235
_inputBound = true;
3336

37+
List<string>? errors = null;
3438
for (int i = 0; i < _parameterValues.Length; i++)
3539
{
3640
FunctionParameter param = context.FunctionDefinition.Parameters[i];
@@ -51,9 +55,21 @@ public DefaultModelBindingFeature(IEnumerable<IConverter> converters)
5155
}
5256
else if (source is not null)
5357
{
54-
throw new InvalidCastException($"Cannot convert input parameter '{param.Name}' for Function '{context.FunctionDefinition.Name}' to type '{param.Type.FullName}' from type '{source.GetType().FullName}'.");
58+
// Don't initialize this list unless we have to
59+
if (errors is null)
60+
{
61+
errors = new List<string>();
62+
}
63+
64+
errors.Add($"Cannot convert input parameter '{param.Name}' to type '{param.Type.FullName}' from type '{source.GetType().FullName}'.");
5565
}
5666
}
67+
68+
// found errors
69+
if (errors is not null)
70+
{
71+
throw new FunctionInputConverterException($"Error converting {errors.Count} input parameters for Function '{context.FunctionDefinition.Name}': {string.Join(" ", errors)}");
72+
}
5773

5874
return _parameterValues;
5975
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
using System.Threading.Tasks;
6+
7+
namespace Microsoft.Azure.Functions.Worker.Diagnostics.Exceptions
8+
{
9+
internal class FunctionInputConverterException : FunctionWorkerException
10+
{
11+
internal FunctionInputConverterException(string message) : base(message) { }
12+
13+
internal FunctionInputConverterException(string message, Exception innerException) : base(message, innerException) { }
14+
}
15+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
using System.Threading.Tasks;
6+
7+
namespace Microsoft.Azure.Functions.Worker.Diagnostics
8+
{
9+
/// <summary>
10+
/// Internal exception that is surfaced to the user
11+
/// </summary>
12+
internal class FunctionWorkerException : Exception
13+
{
14+
internal FunctionWorkerException(string message) : base(message) { }
15+
16+
internal FunctionWorkerException(string message, Exception innerException) : base(message, innerException) { }
17+
}
18+
}

0 commit comments

Comments
 (0)