Skip to content

Commit 2b4ce55

Browse files
committed
Include function names in exception messages
1 parent d1a1c27 commit 2b4ce55

File tree

4 files changed

+29
-40
lines changed

4 files changed

+29
-40
lines changed

CodeFirstStoreFunctions/FunctionDiscovery.cs

Lines changed: 12 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,8 @@ private FunctionDescriptor CreateFunctionDescriptor(MethodInfo method)
6666
if (storeFunctionKind == StoreFunctionKind.ScalarUserDefinedFunction &&
6767
(functionAttribute == null || functionAttribute.NamespaceName != "CodeFirstDatabaseSchema"))
6868
{
69-
throw new InvalidOperationException("Scalar store functions must be decorated with the 'DbFunction' attribute with the 'CodeFirstDatabaseSchema' namespace.");
69+
throw new InvalidOperationException(
70+
$"Scalar store functions must be decorated with the 'DbFunction' attribute with the 'CodeFirstDatabaseSchema' namespace. Function: '{method.Name}'");
7071
}
7172

7273
var unwrapperReturnType =
@@ -102,9 +103,7 @@ private IEnumerable<ParameterDescriptor> GetParameters(MethodInfo method, StoreF
102103
if (parameter.IsOut || parameter.ParameterType.IsByRef)
103104
{
104105
throw new InvalidOperationException(
105-
string.Format(
106-
"The parameter '{0}' is an out or ref parameter. To map Input/Output database parameters use the 'ObjectParameter' as the parameter type.",
107-
parameter.Name));
106+
$"The parameter '{parameter.Name}' of function '{method.Name}' is an out or ref parameter. To map Input/Output database parameters use the 'ObjectParameter' as the parameter type.");
108107
}
109108

110109
var paramTypeAttribute =
@@ -119,9 +118,7 @@ private IEnumerable<ParameterDescriptor> GetParameters(MethodInfo method, StoreF
119118
if (paramTypeAttribute == null)
120119
{
121120
throw new InvalidOperationException(
122-
string.Format(
123-
"Cannot infer type for parameter '{0}'. All ObjectParameter parameters must be decorated with the ParameterTypeAttribute.",
124-
parameter.Name));
121+
$"Cannot infer type for parameter '{parameter.Name}' of funtion '{method.Name}'. All ObjectParameter parameters must be decorated with the ParameterTypeAttribute.");
125122
}
126123

127124
parameterType = paramTypeAttribute.Type;
@@ -136,20 +133,15 @@ private IEnumerable<ParameterDescriptor> GetParameters(MethodInfo method, StoreF
136133

137134
if (parameterEdmType == null)
138135
{
139-
throw
140-
new InvalidOperationException(
141-
string.Format(
142-
"The type '{0}' of the parameter '{1}' of function '{2}' is invalid. Parameters can only be of a type that can be converted to an Edm scalar type",
143-
unwrappedParameterType.FullName, parameter.Name, method.Name));
136+
throw new InvalidOperationException(
137+
$"The type '{unwrappedParameterType.FullName}' of the parameter '{parameter.Name}' of function '{method.Name}' is invalid. Parameters can only be of a type that can be converted to an Edm scalar type");
144138
}
145139

146140
if (storeFunctionKind == StoreFunctionKind.ScalarUserDefinedFunction &&
147141
parameterEdmType.BuiltInTypeKind != BuiltInTypeKind.PrimitiveType)
148142
{
149143
throw new InvalidOperationException(
150-
string.Format(
151-
"The parameter '{0}' is of the '{1}' type which is not an Edm primitive type. Types of parameters of store scalar functions must be Edm primitive types.",
152-
parameter.Name, parameterEdmType));
144+
$"The parameter '{parameter.Name}' of function '{method.Name}' is of the '{parameterEdmType}' type which is not an Edm primitive type. Types of parameters of store scalar functions must be Edm primitive types.");
153145
}
154146

155147
yield return new ParameterDescriptor(parameter.Name, parameterEdmType,
@@ -162,22 +154,20 @@ private EdmType[] GetReturnTypes(string methodName, Type methodReturnType,
162154
{
163155
Debug.Assert(methodReturnType != null, "methodReturnType is null");
164156

165-
var resultTypes = functionDetailsAttribute != null ? functionDetailsAttribute.ResultTypes : null;
157+
var resultTypes = functionDetailsAttribute?.ResultTypes;
166158

167159
if (storeFunctionKind != StoreFunctionKind.StoredProcedure && resultTypes != null)
168160
{
169161
throw new InvalidOperationException(
170-
"The DbFunctionDetailsAttribute.ResultTypes property should be used only for stored procedures returning multiple resultsets and must be null for composable function imports.");
162+
$"The DbFunctionDetailsAttribute.ResultTypes property should be used only for stored procedures returning multiple resultsets and must be null for composable function imports. Function: '{methodName}'");
171163
}
172164

173165
resultTypes = resultTypes == null || resultTypes.Length == 0 ? null : resultTypes;
174166

175167
if (resultTypes != null && resultTypes[0] != methodReturnType)
176168
{
177169
throw new InvalidOperationException(
178-
string.Format(
179-
"The ObjectResult<T> item type returned by the method '{0}' is '{1}' but the first type specified in the `DbFunctionDetailsAttribute.ResultTypes` is '{2}'. The ObjectResult<T> item type must match the first type from the `DbFunctionDetailsAttribute.ResultTypes` array.",
180-
methodName, methodReturnType.FullName, resultTypes[0].FullName));
170+
$"The ObjectResult<T> item type returned by the function '{methodName}' is '{methodReturnType.FullName}' but the first type specified in the `DbFunctionDetailsAttribute.ResultTypes` is '{resultTypes[0].FullName}'. The ObjectResult<T> item type must match the first type from the `DbFunctionDetailsAttribute.ResultTypes` array.");
181171
}
182172

183173
var edmResultTypes = (resultTypes ?? new[] {methodReturnType}).Select(GetReturnEdmItemType).ToArray();
@@ -186,9 +176,7 @@ private EdmType[] GetReturnTypes(string methodName, Type methodReturnType,
186176
edmResultTypes[0].BuiltInTypeKind != BuiltInTypeKind.PrimitiveType)
187177
{
188178
throw new InvalidOperationException(
189-
string.Format(
190-
"The type '{0}' returned by the method '{1}' cannot be mapped to an Edm primitive type. Scalar user defined functions have to return types that can be mapped to Edm primitive types.",
191-
methodReturnType.FullName, methodName));
179+
$"The type '{methodReturnType.FullName}' returned by the function '{methodName}' cannot be mapped to an Edm primitive type. Scalar user defined functions have to return types that can be mapped to Edm primitive types.");
192180
}
193181

194182
return edmResultTypes;
@@ -219,7 +207,7 @@ private EdmType GetReturnEdmItemType(Type type)
219207
}
220208
}
221209

222-
throw new InvalidOperationException(string.Format("No EdmType found for type '{0}'.", type.FullName));
210+
throw new InvalidOperationException($"No EdmType found for type '{type.FullName}'.");
223211
}
224212

225213
private static EdmType GetEdmPrimitiveTypeForClrType(Type clrType)

CodeFirstStoreFunctions/FunctionsConvention.cs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -113,9 +113,7 @@ private static void CreateReturnParameters(DbModel model, FunctionDescriptor fun
113113
if (matchingEntitySets.Length == 0)
114114
{
115115
throw new InvalidOperationException(
116-
string.Format(
117-
"The model does not contain EntitySet for the '{0}' entity type.",
118-
returnType.FullName));
116+
$"The model does not contain EntitySet for the '{returnType.FullName}' entity type.");
119117
}
120118

121119
Debug.Assert(matchingEntitySets.Length == 1, "Invalid model (MEST)");

CodeFirstStoreFunctions/StoreFunctionBuilder.cs

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,7 @@ public EdmFunction Create(FunctionDescriptor functionDescriptor)
3434
if (_schema == null && functionDescriptor.DatabaseSchema == null)
3535
{
3636
throw new InvalidOperationException(
37-
string.Format(
38-
"Database schema is not defined for function '{0}'. Either set a default database schema or use the DbFunctionEx attribute with non-null DatabaseSchema value.",
39-
functionDescriptor.Name));
37+
$"Database schema is not defined for function '{functionDescriptor.Name}'. Either set a default database schema or use the DbFunctionEx attribute with non-null DatabaseSchema value.");
4038
}
4139

4240
var functionPayload =
@@ -46,10 +44,10 @@ public EdmFunction Create(FunctionDescriptor functionDescriptor)
4644
.Parameters
4745
.Select(
4846
p => FunctionParameter.Create(
49-
p.Name,
47+
p.Name,
5048
GetStorePrimitiveType(p),
51-
p.IsOutParam
52-
? ParameterMode.InOut
49+
p.IsOutParam
50+
? ParameterMode.InOut
5351
: ParameterMode.In)).ToArray(),
5452

5553
ReturnParameters = CreateFunctionReturnParameters(functionDescriptor),
@@ -142,9 +140,9 @@ private Dictionary<EdmProperty, TypeUsage> FindStoreTypeUsages(EntityType entity
142140
Debug.Assert(entityType != null, "entityType == null");
143141

144142
var propertyToStoreTypeUsage = new Dictionary<EdmProperty, TypeUsage>();
145-
143+
146144
var types = Tools.GetTypeHierarchy(entityType);
147-
var entityTypeMappings =
145+
var entityTypeMappings =
148146
_model.ConceptualToStoreMapping.EntitySetMappings
149147
.SelectMany(s => s.EntityTypeMappings)
150148
.Where(t => types.Contains(t.EntityType))
@@ -153,7 +151,7 @@ private Dictionary<EdmProperty, TypeUsage> FindStoreTypeUsages(EntityType entity
153151
foreach (var property in entityType.Properties)
154152
{
155153
foreach (var entityTypeMapping in entityTypeMappings)
156-
{
154+
{
157155
var propertyMapping =
158156
(ScalarPropertyMapping)entityTypeMapping.Fragments.SelectMany(f => f.PropertyMappings)
159157
.FirstOrDefault(p => p.Property == property);
@@ -193,8 +191,8 @@ private EdmType GetStorePrimitiveType(ParameterDescriptor parameterDescriptor)
193191

194192
if (type == null)
195193
{
196-
throw new InvalidOperationException(string.Format(
197-
"No store EdmType with the name '{0}' could be found.", parameterDescriptor.StoreType));
194+
throw new InvalidOperationException(
195+
$"No store EdmType with the name '{parameterDescriptor.StoreType}' could be found.");
198196
}
199197

200198
return type;

CodeFirstStoreFunctionsTests/FunctionDiscoveryTests.cs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -439,6 +439,7 @@ public void FindFunctions_throws_for_TVFs_with_ResultTypes()
439439
.ToArray()).Message;
440440

441441
Assert.Contains("DbFunctionDetailsAttribute.ResultTypes", message);
442+
Assert.Contains("TVFWithResultTypes", message);
442443
}
443444

444445
[Fact]
@@ -621,7 +622,8 @@ public void FindFunctions_throws_if_udf_not_in_CodeFirstDatabaseSchema_namespace
621622
.FindFunctions().ToList()).Message;
622623

623624
Assert.Contains("'DbFunction'", message);
624-
Assert.Contains("'CodeFirstDatabaseSchema'", message);
625+
Assert.Contains("'CodeFirstDatabaseSchema'", message);
626+
Assert.Contains("'UdfNotInCodeFirstDatabaseSchema'", message);
625627
}
626628

627629
[Fact]
@@ -641,7 +643,8 @@ public void FindFunctions_throws_for_udfs_with_non_primitive_params()
641643
.FindFunctions().ToList()).Message;
642644

643645
Assert.Contains("'Model.TestEnumType'", message);
644-
Assert.Contains("'param'", message);
646+
Assert.Contains("'param'", message);
647+
Assert.Contains("'UdfWithNonPrimitiveParam'", message);
645648
}
646649

647650
[Fact]
@@ -688,6 +691,7 @@ public void FindFunctions_throws_for_ref_params()
688691
Assert.Contains("Input/Output", message);
689692
Assert.Contains("'param'", message);
690693
Assert.Contains("'ObjectParameter'", message);
694+
Assert.Contains("'StoredProcWithRefParam'", message);
691695
}
692696

693697
[Fact]
@@ -758,6 +762,7 @@ public void FindFunctions_throws_if_no_ParameterTypeAttribute_for_ObjectParamete
758762
Assert.Contains("ParameterTypeAttribute", message);
759763
Assert.Contains("ObjectParameter", message);
760764
Assert.Contains("'param'", message);
765+
Assert.Contains("'StoredProcWithObjectParamaterAndNoParameterTypeAttribute'", message);
761766
}
762767

763768
[Fact]

0 commit comments

Comments
 (0)